如何使用NPOI读取文件
我发现 NPOI 非常适合用 C# 编写 Excel 文件。
但我想用 C# 打开、读取和修改 Excel 文件。
我该怎么做?
I found NPOI is very good to write Excel files with C#.
But I want to open, read and modify Excel files in C#.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
简单阅读下面的示例:
顺便说一下:在 NPOI 网站 这里 的下载部分有示例package - 一组 C# 示例。如果你还没有尝试过的话。 :)
Simple read example below:
By the way: on NPOI website here in Download section there is example package - a pack of C# examples. Try it, if you haven't yet. :)
依赖 Workbook 工厂来实例化工作簿对象可能会有所帮助,因为工厂方法将为您检测 xls 或 xlsx。参考:http://apache-poi.1045710.n5.nabble.com/How-to-check-for-valid-excel-files-using-POI-without-checking-the-file-extension- td2341055.html
如果您不确定工作表的名称,但确定索引(从 0 开始),则可以像这样抓取工作表:
然后,您可以使用接受的代码提供的代码遍历行mj82的回答
It might be helpful to rely on the Workbook factory to instantiate the workbook object since the factory method will do the detection of xls or xlsx for you. Reference: http://apache-poi.1045710.n5.nabble.com/How-to-check-for-valid-excel-files-using-POI-without-checking-the-file-extension-td2341055.html
If you're not sure of the Sheet's name but you are sure of the index (0 based), you can grab the sheet like this:
You can then iterate through the rows using code supplied by the accepted answer from mj82
我发现 NPOI 对于处理 Excel 文件非常有用,这是我的实现(注释是西班牙语,对此感到抱歉):
此方法打开 Excel(xls 或 xlsx)文件并将其转换为 DataTable。
第二种方法则相反,将 DataTable 保存到 Excel 文件中,是的,它可以是 xls 或新的 xlsx,您可以选择!
使用这 2 种方法,您可以打开 Excel 文件,将其加载到数据表中,进行修改并将其保存回 Excel 文件中。
希望你们觉得这很有用。
I find NPOI very usefull for working with Excel Files, here is my implementation (Comments are in Spanish, sorry for that):
This Method Opens an Excel (both xls or xlsx) file and converts it into a DataTable.
This Second method does the oposite, saves a DataTable into an Excel File, yeah it can either be xls or the new xlsx, your choise!
With this 2 methods you can Open an Excel file, load it into a DataTable, do your modifications and save it back into an Excel file.
Hope you guys find this usefull.
正如 Janoulle 指出的,如果您使用 WorkbookFactory,您不需要检测它是哪个扩展,它会为您做这件事。我最近必须实现一个使用 NPOI 读取 Excel 文件并将电子邮件地址导入 SQL 数据库的解决方案。我的主要问题是,我可能会从不同的客户那里收到大约 12 种不同的 Excel 布局,因此我需要一些可以快速更改而无需太多代码的东西。我最终使用了 Npoi.Mapper,这是一个很棒的工具!强烈推荐!
这是我的完整解决方案:
我对 NPOI + Npoi.Mapper 非常满意(来自 Donny Tian)作为 Excel 导入解决方案,我写了一篇关于它的博客文章,详细介绍了上面的代码。如果您愿意,可以在这里阅读:导入 Excel 文件的最简单方法。该解决方案最好的一点是它可以在无服务器的 Azure/云环境中完美运行,这是其他 Excel 工具/库无法实现的。
As Janoulle pointed out, you don't need to detect which extension it is if you use the WorkbookFactory, it will do it for you. I recently had to implement a solution using NPOI to read Excel files and import email addresses into a sql database. My main problem was that I was probably going to receive about 12 different Excel layouts from different customers so I needed something that could be changed quickly without much code. I ended up using Npoi.Mapper which is an awesome tool! Highly recommended!
Here is my complete solution:
I am so happy with NPOI + Npoi.Mapper (from Donny Tian) as an Excel import solution that I wrote a blog post about it, going in to more detail about this code above. You can read it here if you wish: Easiest way to import excel files. The best thing about this solution is that it runs perfectly in a serverless azure/cloud environment which I couldn't get with other Excel tools/libraries.
由于您要求阅读并修改 xls 文件,我已更改@mj82 的答案以满足您的需求。
HSSFWorkbook
没有Save
方法,但它有Write
到流。Since you've asked to read and modify the xls file I have changed @mj82's answer to correspond your needs.
HSSFWorkbook
does not haveSave
method, but it does haveWrite
to a stream.如果您不想使用 NPOI.Mapper,那么我建议您查看此解决方案 - 它可以将 excel 单元格读取为各种类型,并且还有一个简单的导入帮助程序: https://github.com/hidegh/NPOI.Extensions
If you don't want to use NPOI.Mapper, then I'd advise you to check out this solution - it handles reading excel cell into various type and also has a simple import helper: https://github.com/hidegh/NPOI.Extensions