Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}
}
}