打开XML SDK - 保存模板文件(.xltx到.xlsx)

时间:2022-06-24 16:57:42

I have the following code to open Excel template file and save it as .xlsx file and I get the error below when I try to open the new file. Please help to resolve this.

我有以下代码打开Excel模板文件并将其保存为.xlsx文件,当我尝试打开新文件时,我收到以下错误。请帮忙解决这个问题。

Excel cannot open the file ‘sa123.xlsx’ because the file format or the extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

Excel无法打开文件'sa123.xlsx',因为文件格式或扩展名无效。验证文件是否已损坏,以及文件扩展名是否与文件格式匹配。

        string templateName = "C:\\temp\\sa123.xltx";
        byte[] docAsArray = File.ReadAllBytes(templateName);
        using (MemoryStream stream = new MemoryStream())
        {
            stream.Write(docAsArray, 0, docAsArray.Length);    // THIS performs doc copy
            File.WriteAllBytes("C:\\temp\\sa123.xlsx", stream.ToArray());    
        }

2 个解决方案

#1


7  

In order to do this you will need to use the Open XML SDK 2.0. Below is a snippet of code that worked for me when I tried it:

为此,您需要使用Open XML SDK 2.0。下面是我尝试时为我工作的一段代码:

byte[] byteArray = File.ReadAllBytes("C:\\temp\\sa123.xltx");
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(SpreadsheetDocumentType.Workbook);
    }
    File.WriteAllBytes("C:\\temp\\sa123.xlsx", stream.ToArray()); 
}

What this code does is it takes your template file and opens it into a SpreadsheetDocument object. The type of this object is Template, but since you want it as a Workbook you call the ChangeDocumentType method to change it from a Template to a Workbook. This will work since the underlying XML is the same between a .xltx and a .xlsx file and it was just the type that was causing you an issue.

这段代码的作用是获取模板文件并将其打开到SpreadsheetDocument对象中。此对象的类型是模板,但由于您希望将其作为工作簿,因此可以调用ChangeDocumentType方法将其从模板更改为工作簿。这将起作用,因为.xltx和.xlsx文件之间的基础XML是相同的,并且它只是导致您出现问题的类型。

#2


1  

Excel sees the .xlsx extension and tries to open it as a worksheet file. But it isn't. It's a template file. When you have a template open in Excel and save it as a .xlsx file, it converts it to the worksheet format. What you are doing is the same as changing the extension in the filename. Try it in Windows Explorer and you will get the same result.

Excel看到.xlsx扩展名并尝试将其作为工作表文件打开。但事实并非如此。这是一个模板文件。在Excel中打开模板并将其另存为.xlsx文件时,会将其转换为工作表格式。您正在做的与更改文件名中的扩展名相同。在Windows资源管理器中尝试它,您将得到相同的结果。

I believe you should be able to accomplish what you want by using the Excel Object Model. I have not used this though.

我相信你应该能够通过使用Excel对象模型来完成你想要的任务。我没有用过这个。

#1


7  

In order to do this you will need to use the Open XML SDK 2.0. Below is a snippet of code that worked for me when I tried it:

为此,您需要使用Open XML SDK 2.0。下面是我尝试时为我工作的一段代码:

byte[] byteArray = File.ReadAllBytes("C:\\temp\\sa123.xltx");
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(SpreadsheetDocumentType.Workbook);
    }
    File.WriteAllBytes("C:\\temp\\sa123.xlsx", stream.ToArray()); 
}

What this code does is it takes your template file and opens it into a SpreadsheetDocument object. The type of this object is Template, but since you want it as a Workbook you call the ChangeDocumentType method to change it from a Template to a Workbook. This will work since the underlying XML is the same between a .xltx and a .xlsx file and it was just the type that was causing you an issue.

这段代码的作用是获取模板文件并将其打开到SpreadsheetDocument对象中。此对象的类型是模板,但由于您希望将其作为工作簿,因此可以调用ChangeDocumentType方法将其从模板更改为工作簿。这将起作用,因为.xltx和.xlsx文件之间的基础XML是相同的,并且它只是导致您出现问题的类型。

#2


1  

Excel sees the .xlsx extension and tries to open it as a worksheet file. But it isn't. It's a template file. When you have a template open in Excel and save it as a .xlsx file, it converts it to the worksheet format. What you are doing is the same as changing the extension in the filename. Try it in Windows Explorer and you will get the same result.

Excel看到.xlsx扩展名并尝试将其作为工作表文件打开。但事实并非如此。这是一个模板文件。在Excel中打开模板并将其另存为.xlsx文件时,会将其转换为工作表格式。您正在做的与更改文件名中的扩展名相同。在Windows资源管理器中尝试它,您将得到相同的结果。

I believe you should be able to accomplish what you want by using the Excel Object Model. I have not used this though.

我相信你应该能够通过使用Excel对象模型来完成你想要的任务。我没有用过这个。