使用OpenXmlSDK c#将行附加到excel文档的末尾

时间:2016-05-30 17:14:40

标签: c# excel openxml-sdk

我需要使用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

0 个答案:

没有答案