使用 Apache poi 将字符串数组转换为 xlsx?

问题描述 投票:0回答:1

我想从字符串数组创建 xlsx。 我尝试适应 Convert csv to xls/xlsx using Apache poi? 解决方案,但不清楚如何正确放置所有内容?

public void createXlsx(String[] resultSet) {
    SXSSFWorkbook workbook = new SXSSFWorkbook();
    SXSSFSheet sheet = workbook.createSheet("Sheet");
    AtomicReference<Integer> row = new AtomicReference<>(0);
    Stream.iterate(0, i -> i + 1).limit(resultSet.length)
            .forEach(i -> sheet.createRow(row.getAndSet(row.get() + 1)).createCell(i).setCellValue(resultSet[i]));
    try {
        OutputStream out = Files.newOutputStream(Path.of("/Documents/test.xlsx"), CREATE_NEW);
        workbook.write(out);
        out.flush();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

java spring performance apache-poi sxssf
1个回答
0
投票

用循环替换流解决了这个问题

SXSSFRow headerRow = sheet.createRow(0);
    for (int i = 0; i < resultSet.length; i++) {
        headerRow.createCell(i).setCellValue(resultSet[i]);
    }
© www.soinside.com 2019 - 2024. All rights reserved.