Skip to content

Commit

Permalink
Merge pull request #200 from msvprogs/master
Browse files Browse the repository at this point in the history
Optimizations and bugfixes of SXSSF logic
  • Loading branch information
tonyqus authored Apr 4, 2020
2 parents 1dbd3d0 + 085f7f4 commit c741f5d
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 140 deletions.
6 changes: 3 additions & 3 deletions ooxml/XSSF/Streaming/SXSSFCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,8 @@ public void SetCellValue(DateTime? value)
return;
}

SetCellValue(value.Value);
bool date1904 = ((SXSSFWorkbook)Sheet.Workbook).XssfWorkbook.IsDate1904();
SetCellValue(DateUtil.GetExcelDate(value.Value, date1904));
}

public void SetCellValue(double value)
Expand Down Expand Up @@ -821,8 +822,7 @@ private String ConvertCellValueToString(CellType cellType)

public void SetCellValue(DateTime value)
{
bool date1904 = Sheet.Workbook.IsDate1904();
SetCellValue(DateUtil.GetExcelDate(value, date1904));
SetCellValue((DateTime?)value);
}
}
}
6 changes: 2 additions & 4 deletions ooxml/XSSF/Streaming/SXSSFRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ object IEnumerator.Current
}

public void Dispose()
{
{
}

public IEnumerator<ICell> GetEnumerator()
Expand Down Expand Up @@ -411,9 +411,7 @@ object IEnumerator.Current
}

public void Dispose()
{
throw new NotImplementedException();
}
{ }

public IEnumerator<ICell> GetEnumerator()
{
Expand Down
48 changes: 36 additions & 12 deletions ooxml/XSSF/Streaming/SXSSFSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ public IRow CreateRow(int rownum)
{
try
{
FlushRows(_randomAccessWindowSize);
flushRows(_randomAccessWindowSize, false);
}
catch (IOException ioe)
{
Expand Down Expand Up @@ -1243,6 +1243,25 @@ public bool Dispose()
return _writer.Dispose();
}
/**
* Specifies how many rows can be accessed at most via getRow().
* The exeeding rows (if any) are flushed to the disk while rows
* with lower index values are flushed first.
*/
private void FlushRows(int remaining, bool flushOnDisk)
{
KeyValuePair<int, SXSSFRow>? lastRow = null;
var flushedRowsCount = 0;
while (_rows.Count > remaining)
{
flushedRowsCount++;
lastRow = flushOneRow();
}
if (remaining == 0)
allFlushed = true;

if (lastRow != null && flushOnDisk)
_writer.FlushRows(flushedRowsCount, lastRow.Value.Key, lastRow.Value.Value.LastCellNum);
}
* Are all rows flushed to disk?
*/
public bool AllRowsFlushed
Expand All @@ -1262,20 +1281,24 @@ public int LastFlushedRowNumber
return lastFlushedRowNumber;
}
}
/**
* Specifies how many rows can be accessed at most via getRow().
* The exeeding rows (if any) are flushed to the disk while rows
* with lower index values are flushed first.
*/
private void FlushRows(int remaining)

public void FlushRows()
{
while (_rows.Count > remaining) FlushOneRow();
if (remaining == 0) allFlushed = true;
FlushRows(0, true);
}

public void FlushRows()
private KeyValuePair<int, SXSSFRow>? flushOneRow()
{
FlushRows(0);
if (_rows.Count == 0)
return null;

var firstRow = _rows.FirstOrDefault();
// Update the best fit column widths for auto-sizing just before the rows are flushed
// _autoSizeColumnTracker.UpdateColumnWidths(row);
_writer.WriteRow(firstRow.Key, firstRow.Value);
_rows.Remove(firstRow.Key);
lastFlushedRowNumber = firstRow.Key;
return firstRow;
}

private void FlushOneRow()
Expand All @@ -1299,7 +1322,8 @@ private void FlushOneRow()
public Stream GetWorksheetXMLInputStream()
{
// flush all remaining data and close the temp file writer
FlushRows(0);
flushRows(0, true);

_writer.Close();
return _writer.GetWorksheetXmlInputStream();
}
Expand Down
15 changes: 2 additions & 13 deletions ooxml/XSSF/Streaming/SXSSFWorkbook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ private void InjectData(FileInfo zipfile, Stream outStream)
}
else
{
CopyStream(inputStream, zos);
inputStream.CopyTo(zos);
}
inputStream.Close();
}
Expand All @@ -483,17 +483,6 @@ private void InjectData(FileInfo zipfile, Stream outStream)
}
}

private static void CopyStream(Stream inputStream, Stream outputStream)
{
var chunk = new byte[1024];
int count;

while ((count = inputStream.Read(chunk, 0, chunk.Length)) > 0)
{
outputStream.Write(chunk, 0, count);
}
}

private static void CopyStreamAndInjectWorksheet(Stream inputStream, Stream outputStream, Stream worksheetData)
{
StreamReader inReader = new StreamReader(inputStream, Encoding.UTF8);
Expand Down Expand Up @@ -604,7 +593,7 @@ private static void CopyStreamAndInjectWorksheet(Stream inputStream, Stream outp
outWriter.Flush();
}
//Copy the worksheet data to "out".
CopyStream(worksheetData, outputStream);
worksheetData.CopyTo(outputStream);
outWriter.Write("</sheetData>");
outWriter.Flush();
//Copy the rest of "in" to "out".
Expand Down
Loading

0 comments on commit c741f5d

Please # to comment.