如何使用 Apache POI 在 Excel 中逐列写入数据?
How to write column by column data in Excel using 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);
}
}
我想逐列写入 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);
}
}