System.IO.IOException: 'Entries cannot be opened multiple times in Update mode
Issue with the OpenXml library
System.IO.Compression.dll!System.IO.Compression.ZipArchiveEntry.OpenInUpdateMode()
System.IO.Compression.dll!System.IO.Compression.ZipArchiveEntry.Open() System.IO.Packaging.dll!System.IO.Packaging.ZipStreamManager.Open(System.IO.Compression.ZipArchiveEntry zipArchiveEntry, System.IO.FileMode streamFileMode, System.IO.FileAccess streamFileAccess) System.IO.Packaging.dll!System.IO.Packaging.ZipPackagePart.GetStreamCore(System.IO.FileMode streamFileMode, System.IO.FileAccess streamFileAccess) System.IO.Packaging.dll!System.IO.Packaging.PackagePart.GetStream(System.IO.FileMode mode, System.IO.FileAccess access) DocumentFormat.OpenXml.dll!DocumentFormat.OpenXml.Packaging.OpenXmlPart.GetStream(System.IO.FileMode mode, System.IO.FileAccess access) SpreadsheetLight.SLDocument.WriteDocumentProperties()
Description We receive the following exception : System.IO.IOException: 'Entries cannot be opened multiple times in Update mode .
Information
- .NET Target: .NET Core
- DocumentFormat.OpenXml Version: (ie 2.12.3)
I had a similar issue when I called .GetStream() twice without closing the previous one. Could you share the code example where you call .GetStream().
Hi!! This is actually not my code, but an open source project – SpreadsheetLight. This worked fine until pre .net core. Here is the code:
private void WriteDocumentProperties() { if (xl.CoreFilePropertiesPart == null) { xl.AddCoreFilePropertiesPart(); }
using (XmlWriter xw = XmlWriter.Create(xl.CoreFilePropertiesPart.GetStream(FileMode.Create, FileAccess.Write)))
{
xw.WriteStartDocument(true);
xw.WriteStartElement("cp", "coreProperties", "http://schemas.openxmlformats.org/package/2006/metadata/core-properties");
xw.WriteAttributeString("xmlns", "dc", null, "http://purl.org/dc/elements/1.1/");
xw.WriteAttributeString("xmlns", "dcmitype", null, "http://purl.org/dc/dcmitype/");
xw.WriteAttributeString("xmlns", "dcterms", null, "http://purl.org/dc/terms/");
xw.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
if (this.DocumentProperties.Category.Length > 0)
{
xw.WriteElementString("cp", "category", null, this.DocumentProperties.Category);
}
if (this.DocumentProperties.ContentStatus.Length > 0)
{
xw.WriteElementString("cp", "contentStatus", null, this.DocumentProperties.ContentStatus);
}
if (this.DocumentProperties.Created.Length > 0)
{
xw.WriteStartElement("dcterms", "created", null);
xw.WriteAttributeString("xsi", "type", null, "dcterms:W3CDTF");
xw.WriteString(this.DocumentProperties.Created);
xw.WriteEndElement();
}
if (this.DocumentProperties.Creator.Length > 0)
{
xw.WriteElementString("dc", "creator", null, this.DocumentProperties.Creator);
}
if (this.DocumentProperties.Description.Length > 0)
{
xw.WriteElementString("dc", "description", null, this.DocumentProperties.Description);
}
if (this.DocumentProperties.Identifier.Length > 0)
{
xw.WriteElementString("dc", "identifier", null, this.DocumentProperties.Identifier);
}
if (this.DocumentProperties.Keywords.Length > 0)
{
xw.WriteElementString("cp", "keywords", null, this.DocumentProperties.Keywords);
}
if (this.DocumentProperties.Language.Length > 0)
{
xw.WriteElementString("dc", "language", null, this.DocumentProperties.Language);
}
if (this.DocumentProperties.LastModifiedBy.Length > 0)
{
xw.WriteElementString("cp", "lastModifiedBy", null, this.DocumentProperties.LastModifiedBy);
}
if (this.DocumentProperties.LastPrinted.Length > 0)
{
xw.WriteElementString("cp", "lastPrinted", null, this.DocumentProperties.LastPrinted);
}
// we're modifying, so we're ignoring the existing one
xw.WriteStartElement("dcterms", "modified", null);
xw.WriteAttributeString("xsi", "type", null, "dcterms:W3CDTF");
xw.WriteString(DateTime.UtcNow.ToString(SLConstants.W3CDTF));
xw.WriteEndElement();
if (this.DocumentProperties.Revision.Length > 0)
{
xw.WriteElementString("cp", "revision", null, this.DocumentProperties.Revision);
}
if (this.DocumentProperties.Subject.Length > 0)
{
xw.WriteElementString("dc", "subject", null, this.DocumentProperties.Subject);
}
if (this.DocumentProperties.Title.Length > 0)
{
xw.WriteElementString("dc", "title", null, this.DocumentProperties.Title);
}
if (this.DocumentProperties.Version.Length > 0)
{
xw.WriteElementString("cp", "version", null, this.DocumentProperties.Version);
}
xw.WriteEndElement();
xw.WriteEndDocument();
xw.Close();
}
}
Sent from Mailhttps://go.microsoft.com/fwlink/?LinkId=550986 for Windows 10
From: Adam @.> Sent: 16 March 2021 21:26 To: @.> Cc: @.>; @.> Subject: Re: [OfficeDev/Open-XML-SDK] System.IO.IOException: 'Entries cannot be opened multiple times in Update mode (#909)
I had a similar issue when I called .GetStream() twice without closing the previous one. Could you share the code example where you call .GetStream().
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/OfficeDev/Open-XML-SDK/issues/909#issuecomment-800385779, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AJ7O4ISKZFQJUV5KJJ2Y5U3TD55S7ANCNFSM4ZIZDEHQ.
Can you include a full repro? ie something that can be run as-is without adding namespaces/initializing packages/etc?
Hi!! Please check : https://github.com/himanshukodwani/SampleReportingErroReproduction.git
Sent from Mailhttps://go.microsoft.com/fwlink/?LinkId=550986 for Windows 10
From: Taylor @.> Sent: 17 March 2021 02:16 To: @.> Cc: @.>; @.> Subject: Re: [OfficeDev/Open-XML-SDK] System.IO.IOException: 'Entries cannot be opened multiple times in Update mode (#909)
Can you include a full repro? ie something that can be run as-is without adding namespaces/initializing packages/etc?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/OfficeDev/Open-XML-SDK/issues/909#issuecomment-800592525, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AJ7O4IVVCBFJEEQ5SJQNL4TTD67RZANCNFSM4ZIZDEHQ.
There is openCoreFilePropertiesPart stream:
using (XmlWriter xw = XmlWriter.Create(xl.CoreFilePropertiesPart.GetStream(FileMode.Create, FileAccess.Write)))
{
xw.WriteStartDocument(true);
xw.WriteStartElement("cp", "coreProperties", "http://schemas.openxmlformats.org/package/2006/metadata/core-properties");
xw.WriteAttributeString("xmlns", "dc", null, "http://purl.org/dc/elements/1.1/");
xw.WriteAttributeString("xmlns", "dcmitype", null, "http://purl.org/dc/dcmitype/");
//...
It should be closed, for example, like this:

@himanshukodwani Did that work for you? As much as possible, please wrap all disposables with a using so you don't run into this.
Hello, It seems to have corrected the error but there was some corrupt output file problem so I have forwarded your reply to the developer of the spreadsheetlight library. Thanks a lot for your prompt response.
Get Outlook for Androidhttps://aka.ms/ghei36
From: Taylor Southwick @.> Sent: Thursday, March 18, 2021 7:04:45 AM To: OfficeDev/Open-XML-SDK @.> Cc: himanshukodwani @.>; Mention @.> Subject: Re: [OfficeDev/Open-XML-SDK] System.IO.IOException: 'Entries cannot be opened multiple times in Update mode (#909)
@himanshukodwanihttps://github.com/himanshukodwani Did that work for you? As much as possible, please wrap all disposables with a using so you don't run into this.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/OfficeDev/Open-XML-SDK/issues/909#issuecomment-801550320, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AJ7O4ITNXOLUOMUNECQRUPDTEFKDLANCNFSM4ZIZDEHQ.
Sounds good. I'll close this issue for now. Please reopen if needed or log a new issue if the corruption is related.
@himanshukodwani any chance you got feedback from the lib author ?
I recently used an amazingly effective MS tool for upgrading a WPF .Net Framework app to .Net 6. Everything worked perfect-perfect except for all of my (too many to contemplate rewriting) reports I created using OpenXML.
doc.Save causes: [System.IO.IOException: 'Entries cannot be opened multiple times in Update mode
File.Copy(sourceWordTemplate, tempReportPath, true); using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(tempReportPath, true)) { MainDocumentPart mainPart = wordDoc.MainDocumentPart; XmlDocument doc = new XmlDocument(); XmlDocument xnRow = new XmlDocument(); doc.Load(mainPart.GetStream());
// Building of the report which includes replacing text in the source doc,
// looping through, removing and adding table nodes and rows etc...
// nowhere in the report building is the word Stream used.
doc.Save(mainPart.GetStream(FileMode.Create, FileAccess.Write));
wordDoc.Close();
} Any ideas?
I recently used an amazingly effective MS tool for upgrading a WPF .Net Framework app to .Net 6. Everything worked perfect-perfect except for all of my (too many to contemplate rewriting) reports I created using OpenXML.
doc.Save causes: [System.IO.IOException: 'Entries cannot be opened multiple times in Update mode
File.Copy(sourceWordTemplate, tempReportPath, true); using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(tempReportPath, true)) { MainDocumentPart mainPart = wordDoc.MainDocumentPart; XmlDocument doc = new XmlDocument(); XmlDocument xnRow = new XmlDocument(); doc.Load(mainPart.GetStream());
// Building of the report which includes replacing text in the source doc, // looping through, removing and adding table nodes and rows etc... // nowhere in the report building is the word Stream used. doc.Save(mainPart.GetStream(FileMode.Create, FileAccess.Write)); wordDoc.Close();} Any ideas?
Found any resolution so far? I am stuck in the same place
Nope, I just pushed it to the bottom of my todo list, hoping for a resolution. It occurred to me that since this closed maybe nobody is actually trying to solve it. When the rubber hits the road, my thought was to download the source and see if I could debug through and fix it myself. If I do, I'll let you know. If you figure it out first, please let me know, Thanks...
Please see the response in #1188 as well as the comments above. All stream usage should be wrapped with usings. If not, you will see issues like this pop up. It may have worked on .NET Framework, but is still breaking the pattern/contract of streams.
ALWAYS use a using with streams :)
Thanks @twsouthwick, I fixed the issue, fortunately the library was in our control and needless to say plenty of streams were left unclosed. Wrapping them around using worked like a charm
Looks like there is a fork of SpreadsheetLight made specifically for .NET 6 that solves the problem. SpreadsheetLight.Cross.Platform version 3.5.1.
With some recent changes I've made, I believe we can handle this behind the scenes as an opt-in (still better to correctly dispose usings). If anyone is interested, replacing the IPackageFeature with a custom one that will reuse streams if opened should do the trick. The APIs are still internal, but happy to take a PR that allows this as an opt in.
Looks like there is a fork of SpreadsheetLight made specifically for .NET 6 that solves the problem. SpreadsheetLight.Cross.Platform version 3.5.1.
How does it fix it? Looks like it's using the SDK as a dependency so it'll have the same issue
If you are trying to GetStream() you need to close it after your function end his job before you call it again once.
Stream stream = stylesPart.GetStream(FileMode.OpenOrCreate, FileAccess.Write);
newStyles.Save(new StreamWriter(stream));
stream.Close();
Looks like there is a fork of SpreadsheetLight made specifically for .NET 6 that solves the problem. SpreadsheetLight.Cross.Platform version 3.5.1.
It works, I'm using net 8.0 and I installed the library in that version for return a xlsx document modified from a memoryStream as a FileContentResult response in a asp.net API, and that fixed the issue
note: better to use the following pattern:
using Stream stream = stylesPart.GetStream(FileMode.OpenOrCreate, FileAccess.Write);
newStyles.Save(new StreamWriter(stream));
to ensure even with exceptions things get closed out