[英]Exporting DataGridView data to Excel file in C#
I need to export data from DataGridView (in my WinForms application) to excel file. 我需要将数据从DataGridView(在WinForms应用程序中)导出到excel文件。 I googled some tutorials on the subject, but I'm having hard time getting any of the available solutions to work.
我在该主题上搜索了一些教程,但是我很难获得任何可用的解决方案。
I started with this tutorial . 我从本教程开始。 But when I try to export datagridview to excel file i get Unhandled COMException in:
但是,当我尝试将datagridview导出到excel文件时,出现以下异常:
Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new
Microsoft.Office.Interop.Excel.ApplicationClass();
I've also tried this tutorial , but with the same result. 我也尝试过本教程 ,但结果相同。
Another of the articles I've found and read was this one . 我找到并阅读的另一篇文章是这篇文章。 Couldn't get code to work, probably because I can't find
Microsoft Excel 12.0 object library
in the COM tab
of Add Reference
dialog. 无法使代码正常工作,可能是因为我在“
Add Reference
对话框的“ COM tab
中找不到Microsoft Excel 12.0 object library
。
I'm using Visual Studio 2010 Ultimate. 我正在使用Visual Studio 2010 Ultimate。
If those libraries are not installed, you can download them from Microsoft: 如果未安装这些库,则可以从Microsoft下载它们:
For Specific office versions, see here: 有关特定的办公版本,请参见此处:
Office 2003 Update: Redistributable Primary Interop Assemblies Office 2003更新:可再发行主互操作程序集
2007 Microsoft Office System Update: Redistributable Primary Interop Assemblies 2007 Microsoft Office System更新:可再发行主互操作程序集
Microsoft Office 2010: Primary Interop Assemblies Redistributable Microsoft Office 2010:可重新分发的主要互操作程序集
Once you install the correct one, you should be able to find the library in the 'Add reference' section of the tutorials 安装正确的库之后,您应该可以在教程的“添加参考”部分中找到该库。
One day I did what you need but I got it in VB.Net 有一天我做了你需要的,但是我在VB.Net上得到了
So you just need to create a Class Library project and in that class use my method so you can reference it through C#. 因此,您只需要创建一个类库项目,并在该类中使用我的方法,即可通过C#进行引用。
Take a look at this link (translate it to English): http://foro.elhacker.net/net/aporte_exportar_datagridview_a_excel_pdf_y_html-t299075.0.html 看一下此链接(将其翻译成英文): http : //foro.elhacker.net/net/aporte_exportar_datagridview_a_excel_pdf_y_html-t299075.0.html
and also you can download my example from here and re-use my method: http://dl.dropbox.com/u/15208254/stackoverflow/ExportarData.rar 而且您也可以从此处下载我的示例并重新使用我的方法: http : //dl.dropbox.com/u/15208254/stackoverflow/ExportarData.rar
Regards. 问候。
您可以使用Excel Package Plus并导出数据,而无需excel或任何其他外部依赖
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.