C # NPOI large amount of data to export multiple sheet of Excel

NPOI earlier versions of Excel export version is 2003 excel, a sheet store up to 65,536 records, more than an error, later versions can export 2007 excel, but for compatibility with 2003, we generally choose to export 2003 excel. The solution is to export the data in several sheet, the following core code.

I am here is NPOI_1.2.4.0, you can go online to download.

        using NPOI.SS.UserModel;
        using NPOI.HSSF.UserModel;

        /// <summary>
        /// DataTable转换成Excel文档流
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public static MemoryStream DataTableToExcelStream(DataTable table)
        {
            MemoryStream ms = new MemoryStream();

            using (table)
            {

                using (IWorkbook workbook = new HSSFWorkbook()) //HSSFWorkbook
                {

                    // handling value.
                    int rowIndex = 1;

                    ISheet sheet=null;
                    IRow headerRow=null;
                    int sheetCount = 0;
                    int sheetRowCount = 65536; //每个sheet最大数据行数
                    if (table.Rows.Count == 0) //如果没有数据,创建一个只有title的sheet
                    {
                        sheet = workbook.CreateSheet("sheet" + sheetCount.ToString());

                        //创建列头
                        headerRow = sheet.CreateRow(0);
                        // handling header.
                        foreach (DataColumn column in table.Columns)
                            headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);//If Caption not set, returns the ColumnName value
                    }
                    else
                    {
                        for (int i = 0; i < table.Rows.Count; i++)
                        {

                            if (i % (sheetRowCount - 1) == 0) //因单个sheet最多存储65536条记录,故分sheet存储数据  -1的原因是代码创建的列头占据了一行
                            {
                                sheetCount++;
                                rowIndex = 1;
                                sheet = workbook.CreateSheet("sheet" + sheetCount.ToString());
                                headerRow = sheet.CreateRow(0);
                                // handling header.
                                foreach (DataColumn column in table.Columns)
                                    headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);//If Caption not set, returns the ColumnName value
                            }

                            IRow dataRow = sheet.CreateRow(rowIndex);

                            foreach (DataColumn column in table.Columns)
                            {
                                dataRow.CreateCell(column.Ordinal).SetCellValue(table.Rows[i][column].ToString());
                            }

                            rowIndex++;

                            //每个sheet最后一条数据调整列宽
                            if ((i % (sheetRowCount -2) == 0 && i / (sheetRowCount - 2) >= 1)||i==table.Rows.Count-1)
                            {
                                AutoSizeColumns(sheet);
                            }

                        }
                    }

                    workbook.Write(ms);
                    ms.Flush();
                    ms.Position = 0;
                }

            }
            return ms;
        }

 

Guess you like

Origin blog.csdn.net/taoerchun/article/details/91489879