java - 使用 Apache POI 将字符串 ArrayList 中的大量数据写入 Excel 文件

标签 java excel arraylist apache-poi

我有一个相当大的字符串ArrayList(大约超过500k 或更多的项目)。 有没有可能加快这个数组写入Excel文件的速度?现在需要一段时间,我不知道我是否做了我能做的一切(也许这是 .get 方法的问题?)。 请引用以下代码(请不要注意细节 - 该类仅用于测试目的而创建):

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Tests {

private static final String ADDRESS = "./result/file.xlsx";

public static void readAndWriteAData(int counterOfExecutions, List<String> listOfResults) throws IOException{

    File file = new File(ADDRESS);
    if(file.exists()&&!file.isDirectory()){
        file.delete();
    }

    @SuppressWarnings("resource")
    Workbook wb = new XSSFWorkbook();

    FileOutputStream fileOut = new FileOutputStream(ADDRESS);
    wb.write(fileOut);
    fileOut.close();

    Sheet sheet = wb.createSheet("1");
    Row row;
    Cell cell;
    int add = 0;
    System.out.println("Start of filling excel file");

    for(int i=0; i<counterOfExecutions;i++){
        row = sheet.createRow(i);

        for(int j=0; j<5; j++){
            cell = row.createCell(j);
            cell.setCellValue(listOfResults.get(j+add));
        }

        add+=5;
    }


    FileOutputStream fileOutputSecond = new FileOutputStream(file);
    wb.write(fileOutputSecond);
    fileOutputSecond.close();
    System.out.println("File "+ADDRESS+" has been created.");
}

public static void main(String[] args) throws IOException {

    System.out.println("Start of creating ArrayList");
    List<String> lista = new ArrayList<>();
    for(int i = 1 ; i<550000;i++){
        lista.add("nic "+i);
    }
    System.out.println("ArrayList has been filled");


    readAndWriteAData(100999, lista);
    System.out.println("The End");

}

}

提前非常感谢您的任何提示! :)

最佳答案

请尝试使用SXSSFWorkbook。这对于更好地使用它会更有效尝试查看 Apache POI 3.8 API

关于java - 使用 Apache POI 将字符串 ArrayList 中的大量数据写入 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35034101/

相关文章:

java - 删除列表元素 - 我在 Java 中实现最佳性能的方法

java - 不同 JVM 中的 Java 程序如何在无意中相互影响?

java spring BCryptPasswordEncoder "SecureRandom random"构造函数参数

excel - 删除重复项并向上移动单元格

vba - 将基于变量的单元格范围复制到另一个范围

java - java arraylist中的数字按升序排序

java - 这个 'String#format' 模式是什么意思?

java - 我如何在我的 Java 程序中使用 RegExp?

Excel:将工作簿对象从子传递到用户窗体

java - 清除数组时出现 ConcurrentModificationException