Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After AddWorkbookPart() I recieved "Cannot retrieve parts of writeonly container" Exception from System.IO.Packaging #1809

Closed
Reules opened this issue Oct 13, 2024 · 5 comments
Assignees

Comments

@Reules
Copy link

Reules commented Oct 13, 2024

Describe the bug
Recently I updated OpenXML from 2.18.0 to 3.1.0.
The function AddWorkbookPart() didn't work anymore.
Always get the System.IO.IOException "Cannot retrieve parts of writeonly container".

Screenshots
Image

Anyone knows the problem?

@mikeebowen
Copy link
Collaborator

Hi @Reules, can you share the code that causes this error?

Thanks!

@mikeebowen
Copy link
Collaborator

mikeebowen commented Oct 16, 2024

Hi @Reules,

I am using v3.1.1 and the following code with AddWorkbookPart creates an xlsx with no errors. If this doesn't help with your issue, please share your repro code.

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

string xlsx = @"C:\some\dir\AddWorkbookPart.xlsx";

// Create a workbook document by supplying the file path.
// By default, AutoSave = true, Editable = true, and Type = xlsx.
using (var spreadsheetDocument = SpreadsheetDocument.Create(xlsx, SpreadsheetDocumentType.Workbook))
{
    // Add a WorkbookPart to the document.
    WorkbookPart workbookPart = spreadsheetDocument.AddWorkbookPart();
    workbookPart.Workbook = new Workbook();

    // Add a WorksheetPart to the WorkbookPart.
    WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
    worksheetPart.Worksheet = new Worksheet(new SheetData());

    // Add Sheets to the workbook.
    Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());

    // Append a new worksheet and associate it with the workbook.
    Sheet sheet = new Sheet()
    {
        Id = spreadsheetDocument?.WorkbookPart?.GetIdOfPart(worksheetPart),
        SheetId = 1,
        Name = "MyNewSheet"
    };

    sheets.Append(sheet);
}

@mikeebowen
Copy link
Collaborator

Hi @Reules, Are you somehow opening the file as write only?

@Reules
Copy link
Author

Reules commented Oct 17, 2024

Hi @mikeebowen Thanks for the reply, yes, before the AddWorkbookPart, i used
var package = Package.Open(fs, FileMode.Create, FileAccess.Write), which did't cause any problem with Openxml 2.18.0
Maybe this was fixed in Openxml 3.1.0
Thank you for your help

@mikeebowen
Copy link
Collaborator

In System.IO.Packaging.Package, the GetRelationshipsHelper method calls a method ThrowIfWriteOnly, which throws IOException(System.SR.CannotRetrievePartsOfWriteOnlyContainer) if it is write only. The git commit for this is from 9 years ago, so I don't know how it works in 2.18.0, but it shouldn't. View it on GitHub here: GetRelationshipsHelper method.

Since what you're seeing is the expected behavior, I will close this as by design.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants