NPOI導出DataTable數據到excel表格


   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);

            }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM