使用C#从winforms导出到Excel的问题

时间:2022-11-17 20:58:12

I am creating a small winforms application for distribution to a few people. I would like the users to be able to export the contents of a DataGridView to a file that Excel can read. I would like the file to be a excel file and not csv or xml. In the past when I have created an "export to excel" function I have always had to include the "Microsoft.Office.Interop.Excel.dll" as part of the distribution. I would like to just send the users the exe file nothing else. is there a way that I could get away from this second file?

我正在创建一个小型winforms应用程序,以便分发给少数人。我希望用户能够将DataGridView的内容导出到Excel可以读取的文件中。我希望该文件是excel文件而不是csv或xml。在过去,当我创建了“export to excel”函数时,我总是必须将“Microsoft.Office.Interop.Excel.dll”作为分发的一部分。我想只是向用户发送exe文件。有没有办法可以摆脱这第二个文件?

update:

to combine the assemblys I used ILmerge with the following Cmd Line ilmerge /target:winexe /out:PpShiftReport.exe OperatorShiftReport.exe Microsoft.Office.Interop.Excel.dll

合并我使用ILmerge和以下Cmd行ilmerge / target的程序集:winexe /out:PpShiftReport.exe OperatorShiftReport.exe Microsoft.Office.Interop.Excel.dll

2 个解决方案

#1


Use ILMerge to embed the interop DLL into your EXE.

使用ILMerge将互操作DLL嵌入到EXE中。

#2


If you want to create .xls files you'll need to either use the interop assembly or find another library that can create xls files (I don't know of any offhand). There's no way around the licensing issue but if you want .xls files then you can safely assume the client has excel installed on their machine? (otherwise how would they open them?)

如果你想创建.xls文件,你需要使用interop程序集或找到另一个可以创建xls文件的库(我不知道任何副手)。没有办法解决许可问题,但如果你想要.xls文件,那么你可以放心地假设客户端在他们的机器上安装了excel吗? (否则他们将如何打开它们?)

To avoid this I usually just export to CSV. Remember to handle quoting (double quotes twice to include a literal double quote).

为避免这种情况,我通常只导出为CSV。请记住处理引用(双引号两次以包含文字双引号)。

#1


Use ILMerge to embed the interop DLL into your EXE.

使用ILMerge将互操作DLL嵌入到EXE中。

#2


If you want to create .xls files you'll need to either use the interop assembly or find another library that can create xls files (I don't know of any offhand). There's no way around the licensing issue but if you want .xls files then you can safely assume the client has excel installed on their machine? (otherwise how would they open them?)

如果你想创建.xls文件,你需要使用interop程序集或找到另一个可以创建xls文件的库(我不知道任何副手)。没有办法解决许可问题,但如果你想要.xls文件,那么你可以放心地假设客户端在他们的机器上安装了excel吗? (否则他们将如何打开它们?)

To avoid this I usually just export to CSV. Remember to handle quoting (double quotes twice to include a literal double quote).

为避免这种情况,我通常只导出为CSV。请记住处理引用(双引号两次以包含文字双引号)。