Convert XLSM to XLSX

后端 未结 2 1349
遇见更好的自我
遇见更好的自我 2020-12-11 07:56

I\'m using the EPPLUS library to read data from Excel to create another file. Unfortunately it does not support the .XLSM extension file. Is there a nice way to convert .X

2条回答
  •  清歌不尽
    2020-12-11 08:32

    Using the Open XML SDK, like in amurra's answer, but in addition to changing doc type, VbaDataPart and VbaProjectPart should be removed, otherwise Excel will show error a file is corrupted.

    using (var inputStream = File.OpenRead("C:\\temp\\test.xlsm"))
    using (var outStream = new MemoryStream()) {
        inputStream.CopyTo(outStream);
        using (var doc = SpreadsheetDocument.Open(outStream, true)) {
            doc.DeletePartsRecursivelyOfType();
            doc.DeletePartsRecursivelyOfType();
            doc.ChangeDocumentType(DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook);
        }
        File.WriteAllBytes("C:\\temp\\test.xlsx", outStream.ToArray());
    }
    

提交回复
热议问题