我需要使用OpenXmlSDK将行附加到xls / xlsx文档的末尾。
有is similar个问题,但建议的方法是在内存中分配整张表,这会为我的情况抛出Out Of Memory。
我跟着SAX approach,但它也会产生内存不足。
有没有办法xlsx
文件追加?我在OpenXmlWriter
中找不到这样的选项。
private static void OpenXMLSDKAppendRows(string fileName, int numRows, int numCols)
{
{
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, true))
{
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
using (OpenXmlWriter openXmlWriter = OpenXmlWriter.Create(worksheetPart))
{
Row row = new Row();
Cell cell = new Cell();
CellValue value = new CellValue("Test");
cell.AppendChild(value);
openXmlWriter.WriteStartElement(new Worksheet());
openXmlWriter.WriteStartElement(new SheetData());
for (int rowIndex = 0; rowIndex < numRows; rowIndex++)
{
openXmlWriter.WriteStartElement(row);
for (int colIndex = 0; colIndex < numCols; colIndex++)
{
openXmlWriter.WriteElement(cell);
}
openXmlWriter.WriteEndElement();
}
openXmlWriter.WriteEndElement();
openXmlWriter.WriteEndElement();
openXmlWriter.Close();
}
}
}
}
由于LGPL许可证,我无法使用EPPlus
。