openxml spreadsheat save-as

前端 未结 6 1668
无人共我
无人共我 2020-12-03 14:28

I have an Excel 2007 spreadsheet that I edit with the OpenXML SDK 2. I remove some rows etc. I would like to know how to save that Spreadsheetdocument to another filename.

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-03 15:07

    I don't know what version this functionality was introduced, but I use OpenXmlPackage.SaveAs, where
    SpreadsheetDocument, is a subclass of OpenXmlPackage.

    This function returns the new document, so you can switch to the copied one and apply your changes to it:

    void FromTemplate()
    {
      using var template = SpreadsheetDocument.Open("Template.xlsx", isEditable: true);
      using var output = (SpreadsheetDocument)template.SaveAs(path);
    
      //no longer need to keep handle open
      template.Dispose();
    
      //apply your changes to package
    }
    

提交回复
热议问题