我需要将数据从 DataGridView(在我的 WinForms 应用程序中)导出到 excel 文件。我用谷歌搜索了一些关于这个主题的教程,但我很难找到任何可用的解决方案。
我从 this tutorial 开始.但是当我尝试将 datagridview 导出到 excel 文件时,我得到了 Unhandled COMException:
Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new
Microsoft.Office.Interop.Excel.ApplicationClass();
我也试过 this tutorial , 但结果相同。
我找到并阅读的另一篇文章是 this one .无法使代码工作,可能是因为我在 Add Reference 对话框的 COM 选项卡 中找不到 Microsoft Excel 12.0 对象库。
我使用的是 Visual Studio 2010 Ultimate。
请您参考如下方法:
如果没有安装这些库,您可以从 Microsoft 下载它们:
具体的office版本,看这里:
Office 2003 Update: Redistributable Primary Interop Assemblies
2007 Microsoft Office System Update: Redistributable Primary Interop Assemblies
Microsoft Office 2010: Primary Interop Assemblies Redistributable
安装正确的库后,您应该能够在教程的“添加引用”部分找到该库
