Changing Document Type Renames Workbook.xml to Workbook2.xml

Home Forums Open-Xml-Sdk Changing Document Type Renames Workbook.xml to Workbook2.xml

This topic contains 1 reply, has 2 voices, and was last updated by  Eric White 7 years, 11 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #3378

    meshrajk
    Participant

    Hi,

    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”;
    }`

    • This topic was modified 7 years, 11 months ago by  meshrajk. Reason: Marked Code as code, added new tag MacroEnabledWorkbook
    #3392

    Eric White
    Keymaster

    An application that consumes or produces Open XML should never care what the name is of the workbook part. Identification of parts is never done via the URI; instead it is always based on content types and relationship types. It is a bug in these other libraries / applications if they stop working based on the actual URI of any part.

    I understand that this doesn’t help us – we need to use libraries and applications that may contain bugs. However, I do not anticipate that such a change will be coming to the Open-Xml-Sdk.

    Have you raised this issue with the maintainers of those other projects?

    Alternatively, you could use System.IO.Compression to go into the XLSM and change the name of the workbook part. It would not be hard. However, you would need to be careful to do it correctly.

    Are you fully familiar with the Open Packaging Conventions? If not, watch screen-casts 11 and 12 in the following series:

    Introduction to Open XML

    Cheers, Eric

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.