Skip to content

Commit

Permalink
Merge pull request #1152 from Bykiev/FixBug1140
Browse files Browse the repository at this point in the history
Fix leaveOpen option of SXSSFWorkbook
  • Loading branch information
tonyqus authored Aug 11, 2023
2 parents 04c3eed + 690b1d7 commit 02cd2be
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
5 changes: 3 additions & 2 deletions ooxml/XSSF/Streaming/SXSSFWorkbook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ private XSSFSheet GetSheetFromZipEntryName(string sheetRef)
return null;
}

private void InjectData(FileInfo zipfile, Stream outStream)
private void InjectData(FileInfo zipfile, Stream outStream, bool leaveOpen)
{
// don't use ZipHelper.openZipFile here - see #59743
ZipFile zip = new ZipFile(zipfile.FullName);
Expand All @@ -443,6 +443,7 @@ private void InjectData(FileInfo zipfile, Stream outStream)
ZipOutputStream zos = new ZipOutputStream(outStream);
try
{
zos.IsStreamOwner = !leaveOpen;
zos.UseZip64 = UseZip64.Off;
//ZipEntrySource zipEntrySource = new ZipFileZipEntrySource(zip);
//var en = zipEntrySource.Entries;
Expand Down Expand Up @@ -773,7 +774,7 @@ public void Write(Stream stream, bool leaveOpen = false)

//Substitute the template entries with the generated sheet data files

InjectData(tmplFile, stream);
InjectData(tmplFile, stream, leaveOpen);
}
finally
{
Expand Down
45 changes: 36 additions & 9 deletions testcases/ooxml/XSSF/Streaming/SXSSFWorkbookTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ public void IfSettingSheetNameShouldChangeTheSheetNameAtTheSpecifiedIndex()
{
_objectToTest = new SXSSFWorkbook();
_objectToTest.CreateSheet("test1");
_objectToTest.SetSheetName(0,"renamed");
_objectToTest.SetSheetName(0, "renamed");

Assert.AreEqual("renamed", _objectToTest.GetSheetAt(0).SheetName);

}
Expand Down Expand Up @@ -172,7 +172,7 @@ public void IfCreatingASheetShouldCreateASheetInTheXssfWorkbookWithTheGivenName(
var sheet = _objectToTest.CreateSheet("test");

Assert.NotNull(sheet);
Assert.AreEqual("test",sheet.SheetName);
Assert.AreEqual("test", sheet.SheetName);
}

[Test]
Expand Down Expand Up @@ -235,7 +235,7 @@ public void IfGivenThePositionOfAnExistingSheetShouldRemoveThatSheet()
[Test]
public void IfAFontIsCreatedItShouldBeReturnedAndAddedToTheExistingWorkbook()
{
_objectToTest=new SXSSFWorkbook();
_objectToTest = new SXSSFWorkbook();
var font = _objectToTest.CreateFont();

Assert.NotNull(font);
Expand Down Expand Up @@ -277,10 +277,10 @@ public void IfWriting10x10CellsShouldWriteNumericValuesForCells()
var sheets = 1;
var rows = 10;
var cols = 10;
AddCells(_objectToTest, sheets,rows,cols,CellType.Numeric);
AddCells(_objectToTest, sheets, rows, cols, CellType.Numeric);
var savePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "numericTest.xlsx");
WriteFile(savePath, _objectToTest);

Assert.True(File.Exists(savePath));
File.Delete(savePath);
}
Expand All @@ -294,7 +294,7 @@ public void IfOpeningExistingWorkbookShouldWriteAllPreviouslyExistingColumns()
var cols = 10;
AddCells(_objectToTest, sheets, rows, cols, CellType.Numeric);
var savePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "numericTest.xlsx");
var reSavePath = Path.Combine(TestContext.CurrentContext.TestDirectory , "numericTest2.xlsx");
var reSavePath = Path.Combine(TestContext.CurrentContext.TestDirectory, "numericTest2.xlsx");
WriteFile(savePath, _objectToTest);

Assert.True(File.Exists(savePath));
Expand Down Expand Up @@ -462,13 +462,13 @@ private void AddCells(IWorkbook wb, int sheets, int rows, int columns, CellType
var row = sheet.CreateRow(k);
for (int i = 0; i < columns; i++)
{
WriteCellValue(row, type, i, i);
WriteCellValue(row, type, i, i);
}
}
}
}


private void WriteFile(string saveAsPath, SXSSFWorkbook wb)
{
//Passing SXSSFWorkbook because IWorkbook does not implement .Dispose which cleans ups temporary files.
Expand Down Expand Up @@ -507,5 +507,32 @@ private void WriteCellValue(IRow row, CellType type, int col, object val)
row.CreateCell(col).SetCellFormula("SUM(A1:A2)");
}
}

[Test]
public void StreamShouldBeLeavedOpen()
{
using (SXSSFWorkbook workbook = new SXSSFWorkbook())
{
ISheet sheet = workbook.CreateSheet("Sheet1");

// Write a large number of rows and columns to cause OutOfMemoryException
for (int rowNumber = 0; rowNumber < 10; rowNumber++) // Increase this number for more rows
{
IRow row = sheet.CreateRow(rowNumber);
for (int colNumber = 0; colNumber < 100; colNumber++) // Increase this number for more columns
{
ICell cell = row.CreateCell(colNumber);
cell.SetCellValue($"Row {rowNumber + 1}, Column {colNumber + 1}");
}
}

// Write the workbook data to a MemoryStream
using (var stream = new MemoryStream())
{
workbook.Write(stream, true);
Assert.IsTrue(stream.CanRead);
}
}
}
}
}

0 comments on commit 02cd2be

Please sign in to comment.