Comments (6)
This is because of behaviour in ZipArchive. This behaviour needs to change, but by way of explanation: although calling SpreadsheetDocument.Save will persist the relevant parts into the ZipArchive, the ZipArchive won't be persisted into its stream. There's a PR to add ZipArchive.Flush to do this, dotnet/runtime#24149.
ZipArchive won't flush its content into the stream until the ZipArchive instance is disposed of. This happens when the Package is disposed of, which in turn happens when the SpreadsheetDocument is disposed of.
In the sample, you're disposing of the stream, then the SpreadsheetDocument, so when the ZipArchive tries to flush its content into the stream, it throws the ObjectDisposedException you've found. If you dispose of the SpreadsheetDocument before the stream, I think it'll work.
The complete fix would be for ZipArchive to implement Flush, then for ZipPackage to use this so that the Dispose behaviour can be reviewed (at the very least, so it doesn't throw if you Dispose straight after calling SpreadsheetDocument.Save.) This needs to wait for the PR linked above though.
from open-xml-sdk.
Have you considered moving from manual .Dispose()
to using
statements?
from open-xml-sdk.
Have you considered moving from manual
.Dispose()
tousing
statements?
Of course. The original implementation uses a "using" statement. The example is just simplified.
from open-xml-sdk.
This is because of behaviour in ZipArchive. This behaviour needs to change, but by way of explanation: although calling SpreadsheetDocument.Save will persist the relevant parts into the ZipArchive, the ZipArchive won't be persisted into its stream. There's a PR to add ZipArchive.Flush to do this, dotnet/runtime#24149.
ZipArchive won't flush its content into the stream until the ZipArchive instance is disposed of. This happens when the Package is disposed of, which in turn happens when the SpreadsheetDocument is disposed of.
In the sample, you're disposing of the stream, then the SpreadsheetDocument, so when the ZipArchive tries to flush its content into the stream, it throws the ObjectDisposedException you've found. If you dispose of the SpreadsheetDocument before the stream, I think it'll work.
The complete fix would be for ZipArchive to implement Flush, then for ZipPackage to use this so that the Dispose behaviour can be reviewed (at the very least, so it doesn't throw if you Dispose straight after calling SpreadsheetDocument.Save.) This needs to wait for the PR linked above though.
I got it. Thank you! It looks like we cannot do anything in Open XML SDK implementation. Can this issue stay open?
from open-xml-sdk.
I don't think this is the ziparchive - you have the dispose statements the wrong direction. This is part of why using
statements are so much better. Try:
Stream stream = new MemoryStream();
var spreadsheetDocument = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook);
spreadsheetDocument.Save();
- stream.Dispose();
spreadsheetDocument.Dispose();
+ stream.Dispose();
The ziparchive may play into it, but disposing out of order like this is guessing at what the implementation should be doing. Even if "spreadsheetDocument.Save()` flushed everything, the dispose may do additional cleanup and map expect the underlying package/stream to still be available.
from open-xml-sdk.
I'm going to close this as it's by design here. Feel free to reopen if this doesn't resolve it.
from open-xml-sdk.
Related Issues (20)
- Manual approval required for workflow run 10169279217: Approval for publishing to Nuget.org HOT 3
- Documentation Request: Explain how to "Splitting up (shredding) a file into multiple files, and combining multiple files into a single file." HOT 1
- I am using the OpenXML SDK to read the PPT template for content replacement. After stress testing, I found that the memory usage is too high and the number of Gen0, Gen1, and Gen2 object collections is too frequent. HOT 5
- DocumentFormat.OpenXml.Office2016.Drawing.Charts.UniqueID creates the wrong case in the resulting XML
- Add c16:uniqueId for CT_DPt and CT_LegendEntry
- `pictureOptions` and `bubble3D` were removed from some complex types in O15 but not the SDK
- Performance degradation for WordprocessingDocument (v3.1.0 vs v2.20.0 vs v2.5.0) HOT 1
- Calling Worksheet.Save() makes rest of changes to worksheet not apply in 3.1 HOT 3
- Some source files use <remark> tags rather than <remarks> in their documentation comments
- Bug with Open XML SDK
- EndnotesPart.AddHyperlinkRelationship not generating proper endnotes.xml.rels
- Open XML SDK bug?
- Unable to Create Custom Icon Set for Conditional Formatting
- Office2010.Excel - ConditionalFormatting example
- Bug with Open XML SDK HOT 1
- Cloning sheet doesn't clone Drawing part
- A basic OpenXml created Workbook breaks in 'Spreadsheet Compare'
- External hyperlinks (`Relationship`) with a space character within its address breaks Excel 2007. HOT 1
- Rich text support in power point
- FontName becomes OpenXmlUnknownElement
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from open-xml-sdk.