NPOI导出DataTable数据到excel表格

本文详细介绍了如何使用NPOI库将数据表格转换为Excel文件,并在内存中创建和保存Excel文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   DataTable dt = gr.ResultDataTable;
        try
        {
            //内存中创建一个Execl
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.CreateSheet("目录");
            //创建表头
            HSSFRow row = sheet.CreateRow(0);
            //第一列
            HSSFCell cell1 = row.CreateCell(0, HSSFCell.CELL_TYPE_STRING);
            cell1.SetCellValue("省份");
            //第二列
            cell1 = row.CreateCell(1, HSSFCell.CELL_TYPE_STRING);
            cell1.SetCellValue("");
            //第三列
            cell1 = row.CreateCell(2, HSSFCell.CELL_TYPE_STRING);
            cell1.SetCellValue("县(区)");
            //第四列
            cell1 = row.CreateCell(3, HSSFCell.CELL_TYPE_STRING);
            cell1.SetCellValue("地址");
            //第五列
            cell1 = row.CreateCell(4, HSSFCell.CELL_TYPE_STRING);
            cell1.SetCellValue("姓名");
            //第六列
            cell1 = row.CreateCell(5, HSSFCell.CELL_TYPE_STRING);
            cell1.SetCellValue("手机号码");
            //第七列
            cell1 = row.CreateCell(5, HSSFCell.CELL_TYPE_STRING);
            cell1.SetCellValue("索要时间");
            //第八列
            cell1 = row.CreateCell(5, HSSFCell.CELL_TYPE_STRING);
            cell1.SetCellValue("邮编");
            cell1 = row.CreateCell(5, HSSFCell.CELL_TYPE_STRING);
            cell1.SetCellValue("邮箱");
            //生成数据;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                row = sheet.CreateRow(i + 1);
                //第一列
                cell1 = row.CreateCell(0, HSSFCell.CELL_TYPE_STRING);
                cell1.SetCellValue(dt.Rows[i]["provice"].ToString());
                //第二列
                cell1 = row.CreateCell(1, HSSFCell.CELL_TYPE_STRING);
                cell1.SetCellValue(dt.Rows[i]["city"].ToString());
                // 第三列
                cell1 = row.CreateCell(2, HSSFCell.CELL_TYPE_STRING);
                cell1.SetCellValue(dt.Rows[i]["area"].ToString());// "ADDRESS", "CONSIGNEE", "MOBILE", "CREATE_TIME", "postalcode" 
                //第四列
                cell1 = row.CreateCell(3, HSSFCell.CELL_TYPE_STRING);
                cell1.SetCellValue(dt.Rows[i]["ADDRESS"].ToString());
                //第五列
                cell1 = row.CreateCell(4, HSSFCell.CELL_TYPE_STRING);
                cell1.SetCellValue(dt.Rows[i]["CONSIGNEE"].ToString());
                //第六列
                cell1 = row.CreateCell(6, HSSFCell.CELL_TYPE_STRING);
                cell1.SetCellValue(dt.Rows[i]["MOBILE"].ToString());
                //第七列
                cell1 = row.CreateCell(7, HSSFCell.CELL_TYPE_STRING);
                cell1.SetCellValue(dt.Rows[i]["CREATE_TIME"].ToString());
                //第八列
                cell1 = row.CreateCell(8, HSSFCell.CELL_TYPE_STRING);
                cell1.SetCellValue(dt.Rows[i]["postalcode"].ToString());
                //第九列
                cell1 = row.CreateCell(8, HSSFCell.CELL_TYPE_STRING);
                cell1.SetCellValue(dt.Rows[i]["EMAIL"].ToString());


            }
            string path =  @"E:\NPOI.xls"; //GetFullPath("Catalogue.xls");
            //保存文件
            using (FileStream fs = new FileStream(path , FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                workbook.Write(fs);

            }

转载于:https://www.cnblogs.com/gylspx/archive/2012/06/01/sssdasd.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值