如何在Excel中使用Apache POI写逐列数据?



我想写一列一列的Excel文件,但我不知道如何做到这一点。有可能做到吗?我查看了文档,发现没有方法可以逐列编写。下面是我的代码:

private void writeColumnByColumn() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
String[] strings = {"a", "b", "c"};
Row row = sheet.createRow(0);
for (int i = 0; i < 3; i++) {
// here i want to write "a" in the first column, "b" in the second and "c" in the third
}

FileOutputStream outputStream = new FileOutputStream("ok.xlsx");
try (outputStream) {
workbook.write(outputStream);
}
}

应该可以了。

private void writeColumnByColumn() throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
String[] strings = {"a", "b", "c"};
Row row = sheet.createRow(0);
for (int i = 0; i < 3; i++) {
Cell cell = row.createCell(i);
cell.setCellValue(strings[i]);
} 

FileOutputStream outputStream = new FileOutputStream("ok.xlsx");
try (outputStream) {
workbook.write(outputStream);
}
}

最新更新