NPOI 导出excel 分表

/// <summary>
/// 由DataTable导出Excel[超出65536自动分表]
/// </summary>
/// <param name="sourceTable">要导出数据的DataTable</param>
/// <returns>Excel工作表</returns>
public MemoryStream ExportDataTableToExcel(DataTable sourceTable)
{
HSSFWorkbook workbook = new HSSFWorkbook();
MemoryStream ms = new MemoryStream();
int dtRowsCount = sourceTable.Rows.Count;
int SheetCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(dtRowsCount) / 65536));
int SheetNum = 1;
int rowIndex = 1;
int tempIndex = 1; //标示
ISheet sheet = workbook.CreateSheet("sheet1" + SheetNum);
for (int i = 0; i < dtRowsCount; i++)
{
if (i == 0 || tempIndex == 1)
{
IRow headerRow = sheet.CreateRow(0);

foreach (DataColumn column in sourceTable.Columns)
headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
}
HSSFRow dataRow = (HSSFRow)sheet.CreateRow(tempIndex);
foreach (DataColumn column in sourceTable.Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(sourceTable.Rows[i][column].ToString());
}

if (tempIndex == 65535)
{
SheetNum++;
sheet = workbook.CreateSheet("sheet" + SheetNum);//这里就不ISheet,复制惹的祸
tempIndex = 0;
}
rowIndex++;
tempIndex++;


}


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

sheet = null;
// headerRow = null;
workbook = null;

return ms;
}

//控制台调用实例

static void Main(string[] args)
{

NPOIExcelHelper h = new NPOIExcelHelper();
h.ExportDataTableToExcel(s.test(), "ceshiceshi.xls", "ceshiceshi");

}

/// <summary>
/// 由DataTable导出Excel
/// </summary>
/// <param name="sourceTable">要导出数据的DataTable</param>
/// <param name="fileName">指定Excel工作表名称</param>
/// <returns>Excel工作表</returns>
public void ExportDataTableToExcel(DataTable sourceTable, string fileName, string sheetName)
{
//MemoryStream ms = ExportDataTableToExcel(sourceTable, sheetName) as MemoryStream;
//HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
//HttpContext.Current.Response.BinaryWrite(ms.ToArray());
//HttpContext.Current.Response.End();
//ms.Close();
//ms = null;
HSSFWorkbook wk = new HSSFWorkbook();
using (MemoryStream ms = ExportDataTableToExcel(sourceTable, sheetName))
{
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
byte[] data = ms.ToArray();
fs.Write(data, 0, data.Length);
wk.Write(fs);
fs.Flush();
}
}
}

//===下面是mvc 调用实例 

public FileResult ExportStu4()
{
System.IO.MemoryStream ms = new System.IO.MemoryStream();
NPOIExcelHelper n = new NPOIExcelHelper();
// ms = n.ExportDataTableToExcel(ds.Tables[0], "sheet1");
//ExportDataTableToExcel
ms = n.ExportDataTableToExcel(ds.Tables[0]);
string fileName = System.DateTime.Now.ToString();
return File(ms, "application/vnd.ms-excel", fileName + ".xls");
}

//=====================还有个 导出不分表的,调用实例同上=========================//

public MemoryStream ExportDataTableToExcel(DataTable sourceTable, string sheetName)
{
HSSFWorkbook workbook = new HSSFWorkbook();
MemoryStream ms = new MemoryStream();
ISheet sheet = workbook.CreateSheet(sheetName);
IRow headerRow = sheet.CreateRow(0);
// handling header.
foreach (DataColumn column in sourceTable.Columns)
headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);

// handling value.
int rowIndex = 1;

foreach (DataRow row in sourceTable.Rows)
{
HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex);

foreach (DataColumn column in sourceTable.Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
}

rowIndex++;
}

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

sheet = null;
headerRow = null;
workbook = null;

return ms;
}

//ps:听说NPOI 2.0版本的可以直接导出 07版本的excel ,那就无所谓分表不分表了,一个excel sheet表100多万数据基本上就满足需求了,改日有空把 NPOI 2.0版本的代码实例贴出来,供大家参考。

原文地址:https://www.cnblogs.com/yangjinwang/p/4350661.html