在2007年打开excel时出错

本文关键字:出错 excel 2007年 | 更新日期: 2023-09-27 17:54:02

我有一个c# WPF应用程序,它使用Excel互操作库来生成和打开Excel表格。到目前为止,这在安装了Office 2003的XP机器上运行良好。但我最近把它迁移到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。你能让我知道任何可能的变通/解决方案吗?

谢谢,迈克

在2007年打开excel时出错

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

我自己在升级到Windows 7时也遇到了同样的问题。也许是我在64位环境下运行的WPF代码突然受到了冲击……我不确定!

我的解决方案有点激烈。我完全重写了我的WPF应用程序的"导出到Excel"代码,使用OpenXML库,而不是使用VSTO库(它总是给我的。net应用程序增加了一定程度的不稳定性)。

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

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

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

没有比这更简单的了!

希望对你有帮助。

迈克