vb.net 在 vb 中以 .xlsx 格式保存文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20469524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save a file in .xlsx format in vb
提问by user2322507
I am using the latest Microsoft.Office.Interop.Excel dll
我正在使用最新的 Microsoft.Office.Interop.Excel dll
The system currently saves the file in .xlsformat.
I have used the following code but I am getting an error.
系统当前以.xls格式保存文件。
我使用了以下代码,但出现错误。
App = CreateObject("Excel.Application")
xlsTemplate= App.Workbooks.Open(TemplateFile) ' template is in .xls format
TargetPath = Some target path.xlsx
Excel.Workbook.SaveCopyAs(TargetPath)
Excel.Workbook = App.Workbooks.Open(TargetPath) ' code breaks here
SomeFunction(Excel.Workbook)
ExcelBook.SaveAs(ExcelBook.FullName, Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook, System.Reflection.Missing.Value, System.Reflection.Missing.Value, False, False, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlUserResolution, True, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value)
ExcelBook.Saved = True
Error:Excel cannot open the file '.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
错误:Excel 无法打开文件“.xlsx”,因为文件格式或文件扩展名无效。验证文件未损坏并且文件扩展名与文件格式匹配。
回答by Josh Harris
I am responding to this thread as current answers were unclear to me - which forced me to do more research.
我正在回复这个帖子,因为我不清楚当前的答案——这迫使我做更多的研究。
Changing the extension in the name does not change the file format. To save the excel document in your desired format use the below parameters within your Microsoft.Office.Interop.Excel to save your workbook (VB.NET):
更改名称中的扩展名不会更改文件格式。要将 Excel 文档保存为所需格式,请在 Microsoft.Office.Interop.Excel 中使用以下参数来保存工作簿 (VB.NET):
wb.SaveAs("C:\MyFolder\MyWorkbook.xlsx",XlFileFormat.xlOpenXMLWorkbook)
You can modify the second parameter (File Format) to specify what version you want to save the document.
您可以修改第二个参数(文件格式)以指定要保存文档的版本。
- XlFileFormat.xlOpenXMLWorkbook = xlOpenXMLWorkbook (without macro's in 2007-2013, xlsx)
- XlFileFormat.xlOpenXMLWorkbookMacroEnabled = xlOpenXMLWorkbookMacroEnabled (with or without macro's in 2007-2013, xlsm)
- XlFileFormat.xlExcel12 = xlExcel12 (Excel Binary Workbook in 2007-2013 with or without macro's, xlsb)
- XlFileFormat.xlExcel8 = xlExcel8 (97-2003 format in Excel 2007-2013, xls)
- XlFileFormat.xlOpenXMLWorkbook = xlOpenXMLWorkbook(2007-2013 年没有宏,xlsx)
- XlFileFormat.xlOpenXMLWorkbookMacroEnabled = xlOpenXMLWorkbookMacroEnabled(在 2007-2013 年有或没有宏,xlsm)
- XlFileFormat.xlExcel12 = xlExcel12(2007-2013 年的 Excel 二进制工作簿,带或不带宏,xlsb)
- XlFileFormat.xlExcel8 = xlExcel8(Excel 2007-2013 中的 97-2003 格式,xls)
回答by Patrick Stephansen
Changing the extension doesn't automatically change the format of the file. You're just misnaming a .xls file as .xlsx. When you try to open the file again, Excel is expecting a .xlsx file, but getting a file with the .xls format. SaveCopyAsdoesn't give the option to change the file format. Try SaveAsand use the FileFormatargument to specify that you want a .xlsx file.
更改扩展名不会自动更改文件的格式。您只是将 .xls 文件误命名为 .xlsx。当您尝试再次打开该文件时,Excel 需要一个 .xlsx 文件,但获得一个 .xls 格式的文件。SaveCopyAs不提供更改文件格式的选项。尝试SaveAs使用FileFormat参数来指定您想要一个 .xlsx 文件。

