2007年打开excel时出错

发布于 2024-11-03 12:23:13 字数 1396 浏览 3 评论 0原文

我有一个 C# WPF 应用程序,它使用 Excel 互操作库来生成和打开 Excel 工作表。到目前为止,它在装有 Office 2003 的 XP 计算机上运行良好。但我最近将其迁移到运行 Excel 2007 的 Windows 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和Windows 7上失败。 Office 2007。请告诉我任何可能的解决方法/解决方案吗?

谢谢, -麦克风

I have a C# WPF application which uses Excel interop libraries to generate and open excel sheets. This works fine till now on an XP machine with Office 2003. But I have recently migrated this to windows 2007 machine which has Excel 2007 running on it. Now my excel exports does't work anymore. It throws an error like the below:

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)

I use the code below to open my excel file..

 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);
        }

This has no trouble working in Office 2003 and XP, but for some reason fails on Win7 & Office 2007. Please can you let me know of any possible workarounds/solutions for this?

Thanks,
-Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北风几吹夏 2024-11-10 12:23:13

我认为您可能正在使用 excel 2003 的早期绑定,这就是它不适用于 excel 2007 的原因。基本上,您需要做的是重新编译应用程序,同时引用 excel 2007 的正确互操作库(Microsoft.Interop.Office.Excel 版本) 1.6,可以在安装了Office 2007的计算机上的“添加引用”->“COM”中找到)。
您可以通过 google 搜索早期和晚期绑定来获取更多相关信息。以下是帮助您入门的链接:http://peltiertech.com/Excel/EarlyLateBinding.html

I think you may be using early binding with excel 2003 and this is why it doesnt work with excel 2007. Basically what you need to do is recompile your application while referencing the proper interop libraries for excel 2007 (Microsoft.Interop.Office.Excel version 1.6, can be found in "Add reference"->"COM" on a computer with Office 2007 installed).
You can google early and late binding to get more information about it. Here is a link to get you started: http://peltiertech.com/Excel/EarlyLateBinding.html

温馨耳语 2024-11-10 12:23:13

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

我的解决方案有点激进。我完全重写了 WPF 应用程序的“导出到 Excel”代码,以使用 OpenXML 库,而不是使用 VSTO 库(这总是给我的 .Net 应用程序增加一定程度的不稳定)。

我已经记录了我所做的事情,并且这里提供了所有源代码以及演示:
http://www.mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm

链接到我的库后,导出数据集或数据表就像添加一行代码一样简单。

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

没有比这更简单的了!

希望这有帮助。

麦克风

I saw the same problem myself, when I upgrade to Windows 7. Perhaps it's the sudden shock to my WPF code of running in a 64-bit environment... I'm not sure !

My solution was a little drastic. I rewrote my WPF app's "Export to Excel" code completely, to use the OpenXML libraries, rather than using the VSTO libraries (which have always added a level of instability to my .Net apps).

I've documented what I've done, and all source code, plus a demo, is provided here:
http://www.mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm

Once you've linked in my library, exporting a DataSet or DataTable is as simple as adding one line of code.

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

It doesn't get much simpler than that !

Hope this helps.

Mike

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文