簡體   English   中英

使用Apache POI向Excel文件寫入來自字符串數組列表的大量數據

[英]Writing to an Excel file using Apache POI a huge data from ArrayList of Strings

我有一個非常大的字符串數組列表(大約超過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

請參考下面

如何加快Excel的讀寫速度

可能這解決了相同的問題; 記錄器配置對我有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM