简体   繁体   English

如何在不修改旧文件的情况下使用 Openxml 读取模板 excel 文件并保存为不同的文件?

[英]how to read template excel file and save as different file using Openxml without modifying the old file?

I have a requirement in which let say we have one excel template in which some fields needs to fill up with their corresponding values.我有一个要求,假设我们有一个 excel 模板,其中一些字段需要填写相应的值。 After writing the data in that excel file, we want to save that as different file without modifying the existing template and close the excel.在 excel 文件中写入数据后,我们希望在不修改现有模板的情况下将其保存为不同的文件并关闭 excel。

I am using OpenXML SDK to read and edit excel files in DOT Net Core.我正在使用 OpenXML SDK 在 DOT Net Core 中读取和编辑 excel 文件。 I have figure out one way to solve this requirement by first copying the template into new file then editing that template using OpenXML and save it.我已经找到了一种解决此要求的方法,首先将模板复制到新文件中,然后使用 OpenXML 编辑该模板并保存它。

Note: doc.SaveAs("newfile.xltx") // This will create new file but also makes the changes in the existing template.注意: doc.SaveAs("newfile.xltx") // 这将创建新文件,但也会对现有模板进行更改。 So this cannot be used.所以这个不能用。

File.Copy("template.xltx", "newfile.xltx");

using (SpreadsheetDocument document = SpreadsheetDocument.Open("newfile.xltx", true))
{
    WorkbookPart wbPart = document.WorkbookPart;
    
    //Do work here to fill in fields.
    
    //This will save the changes made in newfile.xltx without changing the template.
    wbPart.Workbook.Save();
}

I would love to know if there is any other way of doing it.我很想知道是否有其他方法可以做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM