【问题标题】:How to write column by column data in Excel using Apache POI?如何使用 Apache POI 在 Excel 中逐列写入数据?
【发布时间】:2021-10-29 04:59:38
【问题描述】:

我想逐列写入 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);
    }
}

【问题讨论】:

标签: java excel apache-poi fileoutputstream xssf


【解决方案1】:

这应该可以完成工作。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2017-04-18
    • 2014-12-05
    • 1970-01-01
    相关资源
    最近更新 更多