使用导出另存为的Microsoft.Office.Interop.Excel误差为原来的.xlsx [英] Exporting to .xlsx using Microsoft.Office.Interop.Excel SaveAs Error
问题描述
我在写一个模块使用数据表导出到Excel的过程的Microsoft.Office.Interop.Excel
,但在认真开始之前,我想要得到的很基础工作:打开文件,另存为,并关闭
我已经成功地打开和保存的.xls扩展名的文件,但与扩展的.xlsx确实节约不行。它写入.xlsx文件,但是当我尝试打开它,我收到以下错误:
Excel无法打开文件SomeFile。 XLSX,因为文件格式无效。确认文件没有被损坏,该文件扩展名匹配的文件格式。
块引用>
我用它来打开代码,保存并关闭文件是:
Excel.Application excelApplication =新Excel.Application();
//excelApplication.Visible = TRUE;
//动态excelWorkBook = excelApplication.Workbooks.Add();
Excel.Workbook excelWorkBook = excelApplication.Workbooks.Add();
//Excel.Worksheet wkSheetData = excelWorkBook.ActiveSheet;
INT的rowIndex = 1; INT colIndex = 1;
excelApplication.Cells [rowIndex位置,colIndex] =文本字段;
//这工作。
excelWorkBook.SaveAs(C:\\MyExcelTestTest.xls,Excel.XlFileFormat.xlWorkbookNormal,
System.Reflection.Missing.Value,System.Reflection.Missing.Value,假的,假的,
Excel.XlSaveAsAccessMode.xlShared,假的,假的,System.Reflection.Missing.Value,
System.Reflection.Missing.Value,System.Reflection.Missing.Value);
//这不!?
excelWorkBook.SaveAs(C:\\MyExcelTestTest.xlsx,Excel.XlFileFormat.xlWorkbookNormal,
System.Reflection.Missing.Value,System.Reflection.Missing.Value,假的,假的,
Excel.XlSaveAsAccessMode.xlShared,假的,假的,System.Reflection.Missing.Value,
System.Reflection.Missing.Value,System.Reflection.Missing.Value);
excelWorkBook.Close(Missing.Value,Missing.Value,Missing.Value);
我也试过文件格式
Excel.XlFileFormat.xlExcel12
在Excel.XlFileFormat.xlWorkbookNormal
但这并不会写,而不是投掷收到COMException:
<块引用>从HRESULT
例外:0x800A03EC
块引用>
任何帮助解决这一将最欣赏
编辑:我现在也尝试:
excelWorkBook.SaveAs(C:\\ \\\MyExcelTestTest,Excel.XlFileFormat.xlExcel12,
System.Reflection.Missing.Value,System.Reflection.Missing.Value,假的,假的,
Excel.XlSaveAsAccessMode.xlShared,假的,假的, System.Reflection.Missing.Value,
System.Reflection.Missing.Value,System.Reflection.Missing.Value);
解决方案这是你如何相关的文件保存为Excel12 (.xlsx)格式文件...这是不是你会用直觉地认为即
Excel.XlFileFormat.xlExcel12
,但Excel.XlFileFormat.xlOpenXMLWorkbook
。实际的C#命令是excelWorkbook.SaveAs(strFullFilePathNoExt,Excel.XlFileFormat.xlOpenXMLWorkbook,Missing.Value,
失踪.value的,假的,假的,Excel.XlSaveAsAccessMode.xlNoChange,
Excel.XlSaveConflictResolution.xlUserResolution,真实,
Missing.Value,Missing.Value,Missing.Value);
我希望这可以帮助别人,将来别人。
Missing.Value
在的System.Reflection
命名空间。I am in the process of writing a module to export a DataTable to Excel using
Microsoft.Office.Interop.Excel
but before starting in earnest I want to get the very basics working: open file, save as, and close.I have succeeded in opening and saving a file with the .xls extension, but saving with the .xlsx extension does not work. It writes the .xlsx file, but when I try to open it I get the following error:
Excel cannot open the file 'SomeFile.xlsx' because the file format is not valid. Verify that file has not been corrupted and that the file extension matched the format of the file.
The code I use to open, save and close the files is:
Excel.Application excelApplication = new Excel.Application(); //excelApplication.Visible = true; //dynamic excelWorkBook = excelApplication.Workbooks.Add(); Excel.Workbook excelWorkBook = excelApplication.Workbooks.Add(); //Excel.Worksheet wkSheetData = excelWorkBook.ActiveSheet; int rowIndex = 1; int colIndex = 1; excelApplication.Cells[rowIndex, colIndex] = "TextField"; // This works. excelWorkBook.SaveAs("C:\\MyExcelTestTest.xls", Excel.XlFileFormat.xlWorkbookNormal, System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlShared, false, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value); // This does not!? excelWorkBook.SaveAs("C:\\MyExcelTestTest.xlsx", Excel.XlFileFormat.xlWorkbookNormal, System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlShared, false, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value); excelWorkBook.Close(Missing.Value, Missing.Value, Missing.Value);
I have also tried the file format
Excel.XlFileFormat.xlExcel12
in place ofExcel.XlFileFormat.xlWorkbookNormal
but this does not even write instead throwing the COMException:Exception from HRESULT: 0x800A03EC
Any help resolving this would be most appreciated.
Edit: I have now also tried:
excelWorkBook.SaveAs("C:\\MyExcelTestTest", Excel.XlFileFormat.xlExcel12, System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlShared, false, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
解决方案This is how you save the relevant file as a Excel12 (.xlsx) file... It is not as you would intuitively think i.e. using
Excel.XlFileFormat.xlExcel12
butExcel.XlFileFormat.xlOpenXMLWorkbook
. The actual C# command wasexcelWorkbook.SaveAs(strFullFilePathNoExt, Excel.XlFileFormat.xlOpenXMLWorkbook, Missing.Value, Missing.Value, false, false, Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlUserResolution, true, Missing.Value, Missing.Value, Missing.Value);
I hope this helps someone else in the future.
Missing.Value
is found in theSystem.Reflection
namespace.这篇关于使用导出另存为的Microsoft.Office.Interop.Excel误差为原来的.xlsx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!