使用 Apache POI 导入 CSV 数据 [英] Importing CSV data with Apache POI

查看:101
本文介绍了使用 Apache POI 导入 CSV 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Apache POI 高效导入 CSV 数据?如果我想将一个非常大的 CSV 文件存储在我的 Excel 电子表格中,那么我不认为逐个单元格是导入的最佳方式......?

How can I efficiently import CSV data with Apache POI? If I have a very large CSV file that I would like to store in my Excel spreadsheet, then I don't imagine that going cell-by-cell is the best way to import...?

推荐答案

Apache POI 从未设计为调用 CSV 文件.虽然可以在 Excel 中打开 CSV 文件,但 Excel 有自己的阅读器,可以自动导入.这是假设您的 CSV 具有 .csv 而不是 .txt 后缀.如果它具有 .txt 后缀,请将其另存为 .csv. 然后您要做的就是右键单击 CSV 并使用 Excel 打开.Presto,CSV 已导入 Excel.

Apache POI was never designed to call on CSV files. While a CSV File may be opened in Excel, Excel has its own reader that does an auto import. This is assuming that your CSV has the .csv instead of the .txt suffix. If it has the .txt suffix, save it as a .csv. All then you have to do is right click on the CSV and Open With Excel. Presto, the CSV has been imported into Excel.

我假设您想将 txt 文件中的数据解析为 Excel 文件.如果是这种情况,我建议您使用像 SuperCSV 这样的库,而不是试图让 POI 做某事它从来没有被设计用来做.它会在解析数据时将其全部加载到您选择的 Bean、Map 或 List 中,然后您可以以您选择的格式将其写回 .csv 文件或使用 JDBC-ODBC Bridge 或 Apache POI 将其直接写入 .XLS 格式.添加一个额外的步骤,但您可以完全控制数据.

I am assuming that you are wanting to parse the data from a txt file into the Excel File. If that is the case I would suggest you use a Library like SuperCSV instead of trying to get POI to do something it was never designed to do. It will load it all into a Bean, Map or List of your choice as it parses the data and then you can either write it back in the format you chose into a .csv file or use a JDBC-ODBC Bridge or Apache POI to write it directly into and .XLS format. Adds an extra step, but then you have complete control of the data.

SuperCSV 带有 Apache2 许可证,因此它应该适用于您选择用它做的任何事情.

SuperCSV carries the Apache2 License, so it should be good for anything you choose to do with it.

或者只是使用 Java 中的 split 函数,将 CSV 解析为数组,然后将数组加载到带有 POI 的 .xls 中.

Or just use the split function in Java and parse up the CSV into arrays and load the arrays into .xls with POI.

这篇关于使用 Apache POI 导入 CSV 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆