Skip to content

Commit

Permalink
Memory optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
SWidzSii committed Oct 25, 2023
1 parent 7bffc3a commit 337dc1f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
4 changes: 4 additions & 0 deletions src/Sql2Xls/Excel/Adapters/ExcelExportAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
{
Expand Down
62 changes: 34 additions & 28 deletions src/Sql2Xls/Excel/Adapters/ExcelExportSAXAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@

namespace Sql2Xls.Excel.Adapters;


public class ExcelExportSAXAdapter : ExcelExportAdapter
{
private readonly ILogger<ExcelExportSAXAdapter> _logger;

private readonly Cell _cellObject;
private readonly InlineString _inlineStringObject;

private OpenXmlWriter xlWorksheetPartXmlWriter;
private OpenXmlWriter _xlWorksheetPartXmlWriter;

public ExcelExportSAXAdapter(ILogger<ExcelExportSAXAdapter> logger) : base(logger)
{
_logger = logger;
_cellObject = new Cell();
_inlineStringObject = new InlineString();
}

public override SpreadsheetDocument Open()
Expand All @@ -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;

Expand All @@ -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;
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand All @@ -167,6 +172,7 @@ protected override Workbook CreateWorkbook(WorkbookPart workbookPart, List<Sheet
{
KeyValuePair.Create("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships")
};

Workbook workbook = new Workbook();
openXmlWriter.WriteStartElement(workbook, openXmlAttributes, namespaceDeclarations);

Expand Down Expand Up @@ -204,9 +210,8 @@ protected override Workbook CreateWorkbook(WorkbookPart workbookPart, List<Sheet
return workbook;
}

private OpenXmlWriter CreateWorksheetPreSAX(SpreadsheetDocument document, WorksheetPart worksheetPart)
{
OpenXmlWriter openXmlWriter = OpenXmlWriter.Create(worksheetPart);
private void CreateWorksheetPreSAX(SpreadsheetDocument document, WorksheetPart worksheetPart, OpenXmlWriter openXmlWriter)
{
openXmlWriter.WriteStartDocument(true);

var openXmlAttributes = new List<OpenXmlAttribute>
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -323,7 +329,7 @@ private void CreateSheetDataSAX(OpenXmlWriter openXmlWriter, DataTable dataTable
{
CreateCellFromDataTypeSAX(openXmlWriter, colIndex, rowIndex, dsrow[colIndex]);
}
openXmlWriter.WriteEndElement();
openXmlWriter.WriteEndElement();
rowIndex++;
}

Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down

0 comments on commit 337dc1f

Please sign in to comment.