VSTO C# - 如何编辑 .xltx 文件而不将其另存为新文件

VSTO C# - How to edit a .xltx file and don't save it as new file

我不明白为什么这不适用于 XLTX 文件。我的代码在处理 XLSX 文件时效果很好。

    /// <summary>
    /// Copies the template file and renames the new one.
    /// </summary>
    /// <param name="sourceFilePath"></param>
    private void CopyTemplateFile(string sourceFilePath)
    {
        string strSheetName = "";
        DialogResult result;

        result = MyDialog.ShowDialog("New File - Dialog", "Bitte Name eingeben", "Neue Datei erstellen?", "=FOR1+RBT1", "Ok", "Cancel", ref strSheetName);

        if (result == DialogResult.Yes)     
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel Template (*.xltx)|*.xltx|Excel File (*.xlsx)|*.xlsx";
            sfd.FileName = strSheetName;
            DialogResult ergebnis = sfd.ShowDialog();

            if (ergebnis == DialogResult.OK)
            {
                string path = sfd.FileName;                                                 
                //string tmp = path.Substring(path.LastIndexOf('\')+1);
                System.IO.File.Copy(sourceFilePath, path);


                Excel.Application app = new Excel.Application();
                Excel.Workbook wb = app.Workbooks.Open(path, ReadOnly: false);
                Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets.get_Item(1);
                ws.Name = strSheetName;                                                 
                ws.Range[Statics.ANLAGE_ORT_CELL].Cells.Value2 = "'" + strSheetName;                        
                string stAnlage = strSheetName.Substring(1);                            
                string[] abc = stAnlage.Split('+');                                     
                ws.Range["A50"].Cells.Value2 = abc[0];                                  
                ws.Range["B50"].Cells.Value2 = abc[1];                                  
                wb.SaveAs(path);
                wb.Close();
                app.Quit();
                Marshal.ReleaseComObject(app);
            }
        }
    }

我正在使用 Microsoft.Office.Interop.Excel,sourceFilePath 的打开文件对话框,path 的保存文件对话框。如果使用 XLTX,则不会保存 sheet.name 和单元格值的更改。 也许你们中有人知道这个问题。

问题出在这一行:

wb.SaveAs(path);

根据 Workbook.SaveAs method 的文档,您 can/should 给出以下参数:

  • FileName
    一个字符串,指示要保存的文件的名称。您可以包含完整路径;如果不这样做,Microsoft Excel 会将文件保存在当前文件夹中。
  • FileFormat
    保存文件时使用的文件格式。有关有效选项的列表,请参阅 XlFileFormat 枚举。对于现有文件,默认格式是最后指定的文件格式;对于新文件,默认使用的 Excel 版本的格式。

实际上两者都是可选的,但是如果您省略 FileName Excel 将使用标准文件名和 "current folder" 谁知道这是哪一个,如果您省略FileFormat 然后 Excel 将使用最后指定的文件格式或新文件的标准格式,即普通 xlsx(在 Excel 的最新版本中)。

另一件事是您使用的文件扩展名 .xltx.xlsx.xlsm 必须符合正确的文件格式。如果它不 Excel 抛出一个错误。这实际上是您 运行 在收到错误时进入的内容:

COMException: Additional information: This extension cannot be used with the selected file type. Change the file extension in the 'File Name' text box, or choose a different file type by changing the 'Save As' selection.

因为您只指定了 FileName 并且选择了 .xltx 作为文件扩展名,但是根据 XlFileFormat enumeration 中的列表,默认文件格式通常是 xlOpenXMLWorkbook

因此,如果您在该列表中查找可以使用扩展名 .xltx 的文件格式,那么您只会找到一种有效的文件格式,即 xlOpenXMLTemplate。所以这就是您需要指定的内容:

'example for path/filename you got from your dialog box
path = "C:\YourPath\YourFileName.xltx"

wb.SaveAs(FileName:=path, FileFormat:=xlOpenXMLTemplate);