Convert String Array to xlsx using Apache poi?

94 Views Asked by At

I want to create xlsx from string array. I tried to adapt Convert csv to xls/xlsx using Apache poi? solution, but not clear how to place everything correctly?

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);
    }
}

enter image description here

1

There are 1 best solutions below

0
3oJIoTou On BEST ANSWER

Replacing a stream with a loop solved that issue

SXSSFRow headerRow = sheet.createRow(0);
    for (int i = 0; i < resultSet.length; i++) {
        headerRow.createCell(i).setCellValue(resultSet[i]);
    }