Giter Club home page Giter Club logo

Comments (6)

edwardneal avatar edwardneal commented on June 27, 2024 1

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.

m-gallesio avatar m-gallesio commented on June 27, 2024

Have you considered moving from manual .Dispose() to using statements?

from open-xml-sdk.

sszymons avatar sszymons commented on June 27, 2024

Have you considered moving from manual .Dispose() to using statements?

Of course. The original implementation uses a "using" statement. The example is just simplified.

from open-xml-sdk.

sszymons avatar sszymons commented on June 27, 2024

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.

twsouthwick avatar twsouthwick commented on June 27, 2024

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.

twsouthwick avatar twsouthwick commented on June 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.