在2007年打开Excel时出错

我有一个C#WPF应用程序,它使用Excel互操作库来生成和打开Excel表。 这工作很好,直到现在在一台XP的机器与Office 2003.但我最近迁移到Windows 2007机器上运行的Excel 2007中运行。 现在我的excel出口不再工作了。 它抛出一个错误,如下所示:

System.Runtime.InteropServices.COMException (0x800A03EC): The document is corrupt and cannot be opened. To try and repair it, use the Open and Repair command in the Open dialog box and select Extract Data when prompted. at Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad) 

我使用下面的代码打开我的Excel文件..

  private void OpenSavedData(string fileName) { var excelApp = new Application(); excelApp.Workbooks.Open( fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); excelApp.Visible = true; Marshal.ReleaseComObject(excelApp); } 

这在Office 2003和XP中工作并不困难,但由于某些原因,在Win7和Office 2007中失败了。请您让我知道任何可能的解决方法/解决scheme吗?

谢谢,迈克

当我升级到Windows 7时,我自己也看到了同样的问题。也许这是我的WPF代码在64位环境中运行的突然震动…我不确定!

我的解决scheme有点激烈。 我完全重写了我的WPF应用程序的“导出到Excel”代码,以使用OpenXML库,而不是使用VSTO库(它们总是将不稳定性添加到我的.Net应用程序中)。

我已经logging了我所做的,所有源代码以及一个演示,在这里提供: http : //www.mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm

一旦链接到我的库中,导出DataSet或DataTable就像添加一行代码一样简单。

 CreateExcelFile.CreateExcelDocument(myDataSet, "C:\\MikesExcelFile.xlsx"); 

它并没有比这更简单!

希望这可以帮助。

麦克风

我想你可能会使用早期绑定与Excel 2003,这就是为什么它不适用于Excel 2007.基本上你需要做的是重新编译你的应用程序,同时引用适当的互操作库为Excel 2007(Microsoft.Interop.Office.Excel版本1.6,可以在安装了Office 2007的计算机上的“添加参考” – >“COM”中find)。 你可以谷歌早期和晚期绑定,以获得更多的信息。 这是一个链接,让你开始: http : //peltiertech.com/Excel/EarlyLateBinding.html