Quantcast
Channel: NPOI
Viewing all articles
Browse latest Browse all 1621

Commented Unassigned: Problem with .xlsx file for saving same workbook second time [13401]

$
0
0
Hi Team, I am developing .xls and .xlsx excel file using NPOI 2.1.3 beta version. I have a requirement of saving the same excel workbook multiple times.
With .xls file working good but .xlsx resulted corrupted excel.
But first time saved .xlsx file is working good, Second time saved causes the corrupted excel.
Comments: ** Comment from web user: mmerrill **

I have also experienced this issue using Win 7 64-bit, .Net 4.5. Originally I used NPOI 2.0.6. I also tested 2.1.3 beta. I was investigating this issue and happened to find this post. I include how I have worked around this issue and then my findings. I have not gotten around to digging into the NPOI code yet.

For files that need no modification I use the following work around, writing to a MemoryStream, then byte array and that array to a file:

``` C#
XSSFWorkbook wb = new XSSFWorkbook();
wb.CreateSheet();

byte[] bytes;

using (MemoryStream stream = new MemoryStream())
{
wb.Write(stream);
bytes = stream.ToArray();
}

for (int i = 0; i < 3; i++)
{
string fPath = @"C:\temp\NPOIExcel" + i + ".xlsx";
File.WriteAllBytes(fPath, bytes);
}
```


And now on to my investigation. I had narrowed down the problem by creating a new workbook with one empty sheet using the following code:

``` C#
XSSFWorkbook wb = new XSSFWorkbook();
wb.CreateSheet();

for (int i = 0; i < 3; i++)
{
string fPath = @"C:\temp\NPOIExcel" + i + ".xlsx";

using (FileStream sw = File.Create(fPath))
{
wb.Write(sw);
}
}
```

Only the first file opens and so I unzipped each .xlsx file revealing only the first contains the docProps\core.xml file.

Also, each call of

``` C#
wb.Write(sw);
```

appears to append to the original object within the NPOI library (keep in mind I have not looked at the code as of posting). Let me use the contents of docProps\app.xml as an example

**First .xlsx file**
``` XML
<?xml version="1.0"?>
<Properties xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes" xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties">
...
</Properties>
```

**Second .xlsx file** (the first file contents are duplicated)
``` XML
<?xml version="1.0"?>
<Properties xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes" xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties">
...
</Properties><?xml version="1.0"?>
<Properties xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes" xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties">
...
</Properties>
```

These same symptoms are found in the other XML files with two exceptions: the XML file lives in a '_res' folder or the [Content_Types].xml file.


Viewing all articles
Browse latest Browse all 1621

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>