Java 使用 Apache POI 导入 CSV 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/721859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:43:11  来源:igfitidea点击:

Importing CSV data with Apache POI

javaexcelcsvapache-poi

提问by Jake

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 数据?如果我想将一个非常大的 CSV 文件存储在我的 Excel 电子表格中,那么我不认为逐个单元格是导入的最佳方式......?

回答by Stan Scott

The most efficient way to add information to your Excel spreadsheet is by using an array. This code:

将信息添加到 Excel 电子表格的最有效方法是使用数组。这段代码:

Sheets(1).Range("A1").Resize(uBound(myArray),uBound(myArray,2)).Value = myArray

populates the sheet range from a two dimensional array, where the lower bound is 1.

从二维数组填充工作表范围,其中下限为 1。

In your situation, I would read the CSV file into an array first, and THEN use syntax like the above to place it on the worksheet.

在您的情况下,我会先将 CSV 文件读入数组,然后使用上述语法将其放在工作表上。

Stan Scott

斯坦·斯科特

回答by WolfmanDragon

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.

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

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 SuperCSVinstead 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.

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

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

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

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

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

回答by Arundev

Use apache commons-csv for CSV files. Update your pom.xml with following dependency

对 CSV 文件使用 apache commons-csv。使用以下依赖项更新您的 pom.xml

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-csv</artifactId>
    <version>1.5</version>
</dependency>

You can find useful good exaples from - click here

你可以找到有用的好例子 -点击这里

回答by mannedear

We write the java code based on the file extension. Here we are dealing with .csv extension and here is the Apache POI code to handle it. We split the content and store in Array and then use it down the line.

我们根据文件扩展名编写java代码。这里我们处理 .csv 扩展名,这里是处理它的 Apache POI 代码。我们将内容拆分并存储在 Array 中,然后在线下使用它。

CSVParser parser = new CSVParser(new FileReader("D:/your_file.csv"), CSVFormat.DEFAULT);
List<CSVRecord> list = parser.getRecords();
for (CSVRecord record : list) {
    String[] arr = new String[record.size()];
    int i = 0;
    for (String str : rec) {
        arr[i++] = str;
    }
}  
parser.close();    

Related Maven Dependency:

相关的 Maven 依赖:

<dependency>
    <groupId>commons-collections</groupId>
    <artifactId>commons-collections</artifactId>
    <version>3.2.2</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-csv</artifactId>
    <version>1.5</version>
</dependency>
<dependency>
    <groupId>com.opencsv</groupId>
    <artifactId>opencsv</artifactId>
    <version>4.2</version>
</dependency>