我需要将数据从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。无法使代码生效,可能是因为我在Microsoft Excel 12.0 object library
COM tab
对话框中找不到Add Reference
。
我正在使用Visual Studio 2010 Ultimate。
答案 0 :(得分:2)
如果未安装这些库,您可以从Microsoft下载它们:
对于特定办公室版本,请参见此处:
Office 2003 Update: Redistributable Primary Interop Assemblies
2007 Microsoft Office System Update: Redistributable Primary Interop Assemblies
Microsoft Office 2010: Primary Interop Assemblies Redistributable
安装正确的文件后,您应该可以在教程的“添加参考”部分找到该库
答案 1 :(得分:1)
有一天,我做了你需要的,但我在VB.Net中得到了它。
所以你只需要在该类中创建一个类库项目 使用我的方法,以便您可以通过C#引用它。
看一下这个链接(翻译成英文): http://foro.elhacker.net/net/aporte_exportar_datagridview_a_excel_pdf_y_html-t299075.0.html
你也可以从这里下载我的例子并重新使用我的方法: http://dl.dropbox.com/u/15208254/stackoverflow/ExportarData.rar
问候。
答案 2 :(得分:1)
您可以使用Excel Package Plus并导出数据而无需excel或任何其他外部依赖