NPOI有数据长度限制,当超过65536会造成内存溢出,所以用这种方式创建SHEET1,SHEET2,...来解决这个问题!
int rowIndex = 1; // Starting Row (0 = Header)
int sheetIndex = 1; // Starting sheet is always set to "Sheet1"
const int maxRows = 65536; // Max rows p/sheet in Excel 2003
// Start loop of details to write to sheet
foreach (DataRow row in DataTableToExport.Rows)
{
// Check if max rows hit, if so start new sheet and copy headers from current sheet.
if(rowIndex % maxRows == 0)
{
// Auto size columns on current sheet
for (int h = 0; h < headerRow.LastCellNum; h++)
{
sheet.AutoSizeColumn(h);
}
// Increment sheet counter
sheetIndex++;
// Create new sheet
sheet = workbook.CreateSheet("Sheet" + sheetIndex);
// Create header on new sheet
HSSFRow additionalHeaderRow = sheet.CreateRow(0);
// Copy headers from first sheet
for (int h = 0; h < headerRow.LastCellNum; h++)
{
HSSFCell additionalHeaderColumn = additionalHeaderRow.CreateCell(h);
additionalHeaderColumn.CellStyle = headerRow.GetCell(h).CellStyle;
additionalHeaderColumn.SetCellValue(headerRow.GetCell(h).RichStringCellValue);
}
rowIndex = 1;
}
// Create new detail row in sheet
HSSFRow dataRow = sheet.CreateRow(rowIndex);
// Loop the columns from the DataRow and add using dataRow.CreateCell(#)....
}