如何使用 OpenXML 将 .xltm 文件保存为 .xlsm?

How to save a .xltm file to .xlsm with OpenXML?

我一直在使用 OpenXmlPackage.SaveAs 将我的模板文件保存到 excel(.xltx 到 .xlsx)但是当涉及到启用宏的 .xltm 时,文件在保存到时会损坏.xlsm

我一直在阅读此 API 的帮助部分和各种在线线程,但我找不到任何涵盖此内容的内容,因此我无法使其正常工作。

//open the excel using openxml sdk  
using (SpreadsheetDocument doc = SpreadsheetDocument.CreateFromTemplate(fileOpen))
{
    doc.SaveAs(fileClose);
}

"Excel cannot open the file 'filename.xlsx' because the file format for the file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."

有人对此有想法吗?提前致谢。

这样就可以了。

byte[] byteArray = File.ReadAllBytes(sDocpath + inputPath);
using (MemoryStream stream = new MemoryStream())
{
   stream.Write(byteArray, 0, (int)byteArray.Length);
   using (SpreadsheetDocument spreadsheetDoc = SpreadsheetDocument.Open(stream, true))
     {
     // Change from template type to workbook type
     spreadsheetDoc.ChangeDocumentType(DocumentFormat.OpenXml.SpreadsheetDocumentType.MacroEnabledWorkbook);
     }
   File.WriteAllBytes(dirPath + outputPath, stream.ToArray());
}