我正在尝试在C#ASP.NET Web应用程序中使用封闭XML。我有一个数据集,其中包含30多个数据表。我想将每个数据表作为单独的工作表传递,然后创建Excel。
每个数据表具有10000行和255列。 我收到错误消息:
{System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
at ClosedXML.Excel.XLRangeBase.Cell(XLAddress cellAddressInRange)
at ClosedXML.Excel.XLRangeBase.Cell(Int32 row, Int32 column)
at ClosedXML.Excel.XLCell.SetValue[T](T value, Int32 ro, Int32 co)
at ClosedXML.Excel.XLCell.InsertTable[T](IEnumerable`1 data, String tableName, Boolean createTable)
at ClosedXML.Excel.XLCell.InsertTable(DataTable data, String tableName, Boolean createTable)
at ClosedXML.Excel.XLCell.InsertTable(DataTable data)
at ClosedXML.Excel.XLWorksheets.Add(DataTable dataTable, String sheetName)
at ClosedXML.Excel.XLWorksheets.Add(DataTable dataTable)
我尝试使用下面的代码
XLWorkbook wb = new XLWorkbook();
foreach (DataTable dt in ds.Tables)
{
wb.Worksheets.Add(dt);
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename= " + filename1 + ".xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
wb.Dispose();
该行中发生错误
wb.Worksheets.Add(dt);
添加第一个数据表,然后循环移至第二个数据表。在第二种情况下,我得到了错误。
在Closed Xml中可以使用的行数和列数是否有限制?另外,还有其他方法可以实现将数据导出到excel吗?