C# NPOI导出Excel System.OutOfMemoryException 异常
publicstaticMemoryStreamExport(DataTabledtSource,stringstrHeaderText,stringstrFileNam...
public static MemoryStream Export(DataTable dtSource, string strHeaderText, string strFileName)
{
HSSFWorkbook workbook = new HSSFWorkbook();
ISheet sheet1 = workbook.CreateSheet("sheet1");
//填充表头
IRow dataRow = sheet1.CreateRow(0);
foreach (DataColumn column in dtSource.Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
}
//填充内容
for (int i = 0; i < dtSource.Rows.Count; i++)
{
dataRow = sheet1.CreateRow(i + 1);
for (int j = 0; j < dtSource.Columns.Count; j++)
{
dataRow.CreateCell(j).SetCellValue(dtSource.Rows[i][j].ToString());
}
}
MemoryStream ms = null;
//保存
using (ms = new MemoryStream())
{
workbook.Write(ms);
}
workbook = null;
return ms;
}
调用:
void Download(string xx, DataTable dt)
{
MemoryStream ms = NPOIHelper.Export(dt, xx, xx);
if (ms != null)
{
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=" + xx + ".xls"));
Response.BinaryWrite(ms.ToArray());
ms.Close();
ms.Dispose();
}
}
出错:
“/”应用程序中的服务器错误。
引发类型为“System.OutOfMemoryException”的异常。 展开
{
HSSFWorkbook workbook = new HSSFWorkbook();
ISheet sheet1 = workbook.CreateSheet("sheet1");
//填充表头
IRow dataRow = sheet1.CreateRow(0);
foreach (DataColumn column in dtSource.Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
}
//填充内容
for (int i = 0; i < dtSource.Rows.Count; i++)
{
dataRow = sheet1.CreateRow(i + 1);
for (int j = 0; j < dtSource.Columns.Count; j++)
{
dataRow.CreateCell(j).SetCellValue(dtSource.Rows[i][j].ToString());
}
}
MemoryStream ms = null;
//保存
using (ms = new MemoryStream())
{
workbook.Write(ms);
}
workbook = null;
return ms;
}
调用:
void Download(string xx, DataTable dt)
{
MemoryStream ms = NPOIHelper.Export(dt, xx, xx);
if (ms != null)
{
Response.AddHeader("Content-Disposition", string.Format("attachment; filename=" + xx + ".xls"));
Response.BinaryWrite(ms.ToArray());
ms.Close();
ms.Dispose();
}
}
出错:
“/”应用程序中的服务器错误。
引发类型为“System.OutOfMemoryException”的异常。 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询