NPOI内存溢出

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(#)....
}
原文地址:https://www.cnblogs.com/automation/p/2864861.html