diff --git a/src/Sql2Xls/Excel/Adapters/ExcelExportAdapter.cs b/src/Sql2Xls/Excel/Adapters/ExcelExportAdapter.cs index 8639ce6..0e17a79 100644 --- a/src/Sql2Xls/Excel/Adapters/ExcelExportAdapter.cs +++ b/src/Sql2Xls/Excel/Adapters/ExcelExportAdapter.cs @@ -611,6 +611,10 @@ private SheetData CreateSheetData(SpreadsheetDocument document, Worksheet worksh int rowIndex = 0; int numOfRows = dataTable.Rows.Count; + //TODO Switch to RowProxy ? + //https://github.com/pre-alpha-final/openxml-memory-usage-hack/blob/master/docs/compare_code.png + //https://github.com/dotnet/Open-XML-SDK/issues/807 + var headerRow = new Row { RowIndex = (uint)rowIndex + 1 }; for (int colIndex = 0; colIndex < WorksheetColumns.Count; colIndex++) { diff --git a/src/Sql2Xls/Excel/Adapters/ExcelExportSAXAdapter.cs b/src/Sql2Xls/Excel/Adapters/ExcelExportSAXAdapter.cs index c295d24..884b655 100644 --- a/src/Sql2Xls/Excel/Adapters/ExcelExportSAXAdapter.cs +++ b/src/Sql2Xls/Excel/Adapters/ExcelExportSAXAdapter.cs @@ -8,16 +8,20 @@ namespace Sql2Xls.Excel.Adapters; - public class ExcelExportSAXAdapter : ExcelExportAdapter { private readonly ILogger _logger; + + private readonly Cell _cellObject; + private readonly InlineString _inlineStringObject; - private OpenXmlWriter xlWorksheetPartXmlWriter; + private OpenXmlWriter _xlWorksheetPartXmlWriter; public ExcelExportSAXAdapter(ILogger logger) : base(logger) { _logger = logger; + _cellObject = new Cell(); + _inlineStringObject = new InlineString(); } public override SpreadsheetDocument Open() @@ -36,10 +40,11 @@ public override SpreadsheetDocument Open() xlWorkbook = CreateWorkbook(xlWorkbookPart, sheetInfo); xlWorksheetPart = CreateWorksheetPart(xlWorkbookPart); - xlWorksheetPartXmlWriter = CreateWorksheetPreSAX(xlDocument, xlWorksheetPart); + _xlWorksheetPartXmlWriter = OpenXmlWriter.Create(xlWorksheetPart); + CreateWorksheetPreSAX(xlDocument, xlWorksheetPart, _xlWorksheetPartXmlWriter); xlSheetData = new SheetData(); - xlWorksheetPartXmlWriter.WriteStartElement(xlSheetData); + _xlWorksheetPartXmlWriter.WriteStartElement(xlSheetData); __STATE = STATE_OPEN; @@ -48,7 +53,7 @@ public override SpreadsheetDocument Open() protected override void AddHeaderRow(int rowIndex) { - CreateHeaderRow(xlWorksheetPartXmlWriter, rowIndex, true); + CreateHeaderRow(_xlWorksheetPartXmlWriter, rowIndex, true); __STATE = STATE_IMPORT; } @@ -92,7 +97,7 @@ public override void AddDataRecord(IDataRecord dataRecord) } } - CreateRowFromRecordSAX(xlWorksheetPartXmlWriter, dataRecord, currentRow++, true); + CreateRowFromRecordSAX(_xlWorksheetPartXmlWriter, dataRecord, currentRow++, true); } private void CreateRowFromRecordSAX(OpenXmlWriter openXmlWriter, IDataRecord record, int rowIndex, bool preCacheSharedString = true) @@ -130,17 +135,17 @@ public override void Close() { if (xlDocument != null) { - if (xlWorksheetPartXmlWriter != null) + if (_xlWorksheetPartXmlWriter != null) { - xlWorksheetPartXmlWriter.WriteEndElement(); //SheedData + _xlWorksheetPartXmlWriter.WriteEndElement(); //SheedData } CreateSharedStringTable(xlDocument, xlSharedStringTablePart, sharedStringsCache, xlSharedStringsCount); - if (xlWorksheetPartXmlWriter != null) + if (_xlWorksheetPartXmlWriter != null) { - CreateWorksheetPostSAX(xlWorksheetPartXmlWriter); - xlWorksheetPartXmlWriter = null; + CreateWorksheetPostSAX(_xlWorksheetPartXmlWriter); + _xlWorksheetPartXmlWriter = null; } xlDocument.Dispose(); @@ -167,6 +172,7 @@ protected override Workbook CreateWorkbook(WorkbookPart workbookPart, List @@ -267,9 +272,7 @@ private OpenXmlWriter CreateWorksheetPreSAX(SpreadsheetDocument document, Worksh CustomWidth = BooleanValue.FromBoolean(true) }); - openXmlWriter.WriteEndElement(); - - return openXmlWriter; + openXmlWriter.WriteEndElement(); } private void CreateWorksheetPostSAX(OpenXmlWriter openXmlWriter) @@ -288,15 +291,18 @@ private void CreateWorksheetPostSAX(OpenXmlWriter openXmlWriter) HeaderFooter headerFooter = new HeaderFooter(); openXmlWriter.WriteElement(headerFooter); - openXmlWriter.WriteEndElement(); - openXmlWriter.Close(); + openXmlWriter.WriteEndElement(); } protected override void CreateWorksheet(SpreadsheetDocument document, WorksheetPart worksheetPart, DataTable dataTable) { - xlWorksheetPartXmlWriter = CreateWorksheetPreSAX(document, worksheetPart); - CreateSheetDataSAX(xlWorksheetPartXmlWriter, dataTable); - CreateWorksheetPostSAX(xlWorksheetPartXmlWriter); + using var xmlWriter = OpenXmlWriter.Create(xlWorksheetPart); + { + CreateWorksheetPreSAX(document, worksheetPart, xmlWriter); + CreateSheetDataSAX(xmlWriter, dataTable); + CreateWorksheetPostSAX(xmlWriter); + xmlWriter.Close(); + } } private void CreateSheetDataSAX(OpenXmlWriter openXmlWriter, DataTable dataTable) @@ -323,7 +329,7 @@ private void CreateSheetDataSAX(OpenXmlWriter openXmlWriter, DataTable dataTable { CreateCellFromDataTypeSAX(openXmlWriter, colIndex, rowIndex, dsrow[colIndex]); } - openXmlWriter.WriteEndElement(); + openXmlWriter.WriteEndElement(); rowIndex++; } @@ -416,7 +422,7 @@ private void CreateSharedStringCellSAX(OpenXmlWriter openXmlWriter, int columnIn new OpenXmlAttribute(string.Empty, "s", string.Empty, styleIndex.ToString()) }; - openXmlWriter.WriteStartElement(new Cell(), openXmlAttributes); + openXmlWriter.WriteStartElement(_cellObject, openXmlAttributes); openXmlWriter.WriteElement(new CellValue(isValueSharedString ? value : GetSharedStringItem(value))); openXmlWriter.WriteEndElement(); } @@ -430,8 +436,8 @@ private void CreateInlineStringCellSAX(OpenXmlWriter openXmlWriter, int columnIn new OpenXmlAttribute(string.Empty, "s", string.Empty, styleIndex.ToString()) }; - openXmlWriter.WriteStartElement(new Cell(), openXmlAttributes); - openXmlWriter.WriteStartElement(new InlineString()); + openXmlWriter.WriteStartElement(_cellObject, openXmlAttributes); + openXmlWriter.WriteStartElement(_inlineStringObject); openXmlWriter.WriteElement(new Text { Text = value }); openXmlWriter.WriteEndElement(); openXmlWriter.WriteEndElement(); @@ -460,7 +466,7 @@ private void CreateDateCellSAX(OpenXmlWriter openXmlWriter, int columnIndex, int new OpenXmlAttribute(string.Empty, "s", string.Empty, styleIndex.ToString()) }; - openXmlWriter.WriteStartElement(new Cell(), openXmlAttributes); + openXmlWriter.WriteStartElement(_cellObject, openXmlAttributes); openXmlWriter.WriteElement(new CellValue(value)); openXmlWriter.WriteEndElement(); } @@ -474,7 +480,7 @@ private void CreateStringCellSAX(OpenXmlWriter openXmlWriter, int columnIndex, i new OpenXmlAttribute(string.Empty, "s", string.Empty, styleIndex.ToString()) }; - openXmlWriter.WriteStartElement(new Cell(), openXmlAttributes); + openXmlWriter.WriteStartElement(_cellObject, openXmlAttributes); openXmlWriter.WriteElement(new CellValue(value)); openXmlWriter.WriteEndElement(); }