我想从字符串数组创建 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);
}
}
用循环替换流解决了这个问题
SXSSFRow headerRow = sheet.createRow(0);
for (int i = 0; i < resultSet.length; i++) {
headerRow.createCell(i).setCellValue(resultSet[i]);
}