dotnet / Open-XML-SDK

Open XML SDK by Microsoft
https://www.nuget.org/packages/DocumentFormat.OpenXml/
MIT License
4.02k stars 546 forks source link

Changing Document Type Renames Workbook.xml to Workbook2.xml #92

Closed meshrajk closed 7 years ago

meshrajk commented 8 years ago

I’m using the below code to convert .xlsx to .xlsm. But for some reason, it is changing “xl\workbook.xml” to “xl\workbook2.xml” making the workbook unavailable moving forward for other libraries like EPPlus or ClosedXML. We use these for more modification of the workbook/vbaproject etc.

Is it by design or its a bug? Is there any alternate way we can convert document to macroenabled?

 void SaveXMLAsXLSM()
 {
 if (_extention.Equals(“.xlsm”)) return;

byte[] byteArray = File.ReadAllBytes(_excelPath);
 using (MemoryStream stream = new MemoryStream())
 {
 stream.Write(byteArray, 0, (int)byteArray.Length);
 using (DocumentFormat.OpenXml.Packaging.SpreadsheetDocument spreadsheetDoc = DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open(stream, true))
 {
 // Change from template type to Macro Enabled Workbook type
 spreadsheetDoc.ChangeDocumentType(DocumentFormat.OpenXml.SpreadsheetDocumentType.MacroEnabledWorkbook);
 }

DeleteFile(_xlsmPath);
 File.WriteAllBytes(_xlsmPath, stream.ToArray());
 }
 DeleteFile(_excelPath);
 _excelPath = _xlsmPath;

_extention = “.xlsm”;
 }
tarunchopra commented 8 years ago

Hello meshrajk : Can you please enlighten as to what do you mean by this 'making the workbook unavailable moving forward for other libraries like EPPlus or ClosedXML' ? I believe that the new file has no mention of workbook.xml hence I'm not sure why the libraries you mentioned are having issues.

meshrajk commented 7 years ago

Once this "xl\workbook.xml" is changed to "xl\workbook2.xml", any other libraries are still looking for "xl\workbook.xml" in the package and they error out when the "xl\workbook.xml" is not found in the package.

tarunchopra commented 7 years ago

@meshrajk : I'd say that the issue here is with the apps who are still looking for xl\workbook.xml whereas we have already updated all the relationships in the document with xl\workbook2.xml. If you still need this support, we would request you to fork the project and make a change to suit your requirement.

Asbjoedt commented 2 years ago

I get what is being said here. I am not encountering any issues per se, but is there any reason why changing from .xlsm to .xlsx (or vice versa) should result in renaming workbook.xml to workbook2.xml? What I mean but this question is, that this can be confusing for someone manually/semantically reading the XML, unless they find this issue here explaining this is acceptable behaviour.