如何将数据导出到 Excel 文件
我有一个 Excel 文件,其中包含数据。我想将其中的一些特定行写入我通过代码创建的另一个 Excel 文件中。顺便说一下,我在列表中有这些行的索引。我怎样才能做到这一点?
I have an Excel file with data in it. I want to write some specific rows of it to another Excel file that I created by code. By the way I have the indexes of these rows in a list. How can i do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
MS 提供 OpenXML SDK V 2.5 - 请参阅 https:/ /msdn.microsoft.com/en-us/library/bb448854(v=office.15).aspx
这可以读取+写入 MS Office 文件(包括 Excel)...
另一个选项请参见http://www.codeproject.com/KB/office/OpenXML.aspx
如果您需要更多像渲染、公式等。然后有不同的商业库,如 Aspose 和 Flexcel...
MS provides the OpenXML SDK V 2.5 - see https://msdn.microsoft.com/en-us/library/bb448854(v=office.15).aspx
This can read+write MS Office files (including Excel)...
Another option see http://www.codeproject.com/KB/office/OpenXML.aspx
IF you need more like rendering, formulas etc. then there are different commercial libraries like Aspose and Flexcel...
以上代码直接摘自csharp.net请采纳上网站看看。
The above code is taken directly off csharp.net please take a look on the site.
您是否听说过 NPOI,这是一个无需安装 Microsoft Office 即可读取/写入 Office 格式的 .NET 库。没有 COM+,就没有互操作。 Github 页面
这是我的 Excel 导出类
Have you ever hear NPOI, a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop. Github Page
This is my Excel Export class
您可以使用 ExcelDataReader 读取现有的
Excel
文件:收集所有数据后如果需要,您可以尝试我的 SwiftExcel 库将其导出到新的
Excel
文件:用于安装这两个库的 Nuget 命令:
You can use ExcelDataReader to read existing
Excel
file:After you collected all data needed you can try my SwiftExcel library to export it to the new
Excel
file:Nuget commands to install both libraries:
我使用互操作打开 Excel 并在数据完成后修改列宽。如果您使用互操作将数据吐入新的 Excel 工作簿(如果这是您想要的),那么速度会非常慢。相反,我生成了
.CSV
,然后在 Excel 中打开.CSV
。这有其自身的问题,但我发现这是最快的方法。首先,转换
.CSV
:然后,将其导出到 Excel:
您还必须安装一些东西...
右键单击解决方案资源管理器中的解决方案,然后选择“管理 NuGet”包裹。” - 添加
Microsoft.Office.Interop.Excel
如果您按照 interop 希望的方式创建项目,它现在可能实际上可以工作。如果它仍然不起作用,我必须在不同的类别中创建一个新项目。在新建>下项目,选择Visual C# > Windows 桌面 >控制台应用程序。否则,互操作工具将无法工作。
如果我忘记了什么,这是我的“使用”语句:
I used interop to open Excel and to modify the column widths once the data was done. If you use interop to spit the data into a new Excel workbook (if this is what you want), it will be terribly slow. Instead, I generated a
.CSV
, then opened the.CSV
in Excel. This has its own problems, but I've found this the quickest method.First, convert the
.CSV
:Then, export it to Excel:
You'll have to install some stuff, too...
Right click on the solution from solution explorer and select "Manage NuGet Packages." - add
Microsoft.Office.Interop.Excel
It might actually work right now if you created the project the way interop wants you to. If it still doesn't work, I had to create a new project in a different category. Under New > Project, select Visual C# > Windows Desktop > Console App. Otherwise, the interop tools won't work.
In case I forgot anything, here's my 'using' statements:
在使用 C# 将数据导出到 Excel 电子表格时,我也遇到了类似的问题。我尝试了许多不同的方法来处理外部 DLL,但没有成功。
对于导出功能,您不需要使用任何处理外部 DLL 的东西。相反,只需维护响应的标头和内容类型。
这是一篇我觉得很有帮助的文章。本文讨论如何使用 ASP.NET 将数据导出到 Excel 电子表格。
I was also struggling with a similar issue dealing with exporting data into an Excel spreadsheet using C#. I tried many different methods working with external DLLs and had no luck.
For the export functionality you do not need to use anything dealing with the external DLLs. Instead, just maintain the header and content type of the response.
Here is an article that I found rather helpful. The article talks about how to export data to Excel spreadsheets using ASP.NET.
使用 .NET 的 Aspose.Cells 库,您可以轻松导出特定行的数据以及从一个 Excel 文档到另一个 Excel 文档的列。以下代码示例展示了如何使用 C# 语言执行此操作。
以下博客文章详细介绍了如何将不同来源的数据导出到 Excel 文档。
https://blog.conholdate.com /2020/08/10/导出数据到excel-in-csharp/
With Aspose.Cells library for .NET, you can easily export data of specific rows and columns from one Excel document to another. The following code sample shows how to do this in C# language.
The following blog post explains in detail how to export data from different sources to an Excel document.
https://blog.conholdate.com/2020/08/10/export-data-to-excel-in-csharp/