【问题标题】:Write Excel file with Java用Java编写Excel文件
【发布时间】:2023-03-23 20:10:02
【问题描述】:

我用 Apache POI 创建了 Excel 文件。

在我的数据库中,我有一个 400 人的列表。我想把他们的名字和姓氏写到那个 Excel 文件中。

这是我的代码示例:

try {
    FileOutputStream fileOut = new FileOutputStream("C:/testExcelForJava/test.xls");
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet worksheet = workbook.createSheet("POI Worksheet");

    // index from 0,0... cell A1 is cell(0,0)
    HSSFRow row1 = worksheet.createRow((short) 0);

    HSSFCell cellA1 = row1.createCell((short) 0);
    cellA1.setCellValue("Ad");
    HSSFCellStyle cellStyle = workbook.createCellStyle();
    cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);
    cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    cellA1.setCellStyle(cellStyle);

    HSSFCell cellB1 = row1.createCell((short) 1);
    cellB1.setCellValue("Soyad");
    cellStyle = workbook.createCellStyle();
    cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);
    cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    cellB1.setCellStyle(cellStyle);

    List<Driver> driverList = Dao.driverDao.queryForAll();

    for(Driver driver :driverList){

        String name = driver.getName();
        String surname = driver.getSurname();
        String birthDay = driver.getBirthDay();

        cellA1.setCellValue(name);
        cellB1.setCellValue(surname);
    }

    workbook.write(fileOut);
    fileOut.flush();
    fileOut.close();
} catch(FileNotFoundException e){
    e.printStackTrace();
    log.error(e.getMessage());

} catch(IOException e){
    e.printStackTrace();
    log.error(e.getMessage());
}

当我查看我的 Excel 文件时,我只看到列表中的最后一个人。

我怎样才能写下我所有员工的信息?

谢谢

【问题讨论】:

  • 也循环遍历您的 Excel 单元格

标签: java excel apache-poi dao


【解决方案1】:

如果不完全阅读代码,似乎您正在覆盖循环中单元格 A1 和 B1 的值:

for(Driver driver :driverList){

    String name = driver.getName();
    String surname = driver.getSurname();
    String birthDay = driver.getBirthDay();

    cellA1.setCellValue(name); //the same cell as before!!!
    cellB1.setCellValue(surname); //the same cell as before!!!
}

我假设您也想更改循环中的单元格,例如将所有行和单元格创建代码也放入循环中,并在每次迭代中推进行索引。

【讨论】:

    【解决方案2】:

    写完name 和surname 后需要输入一个新行。你的问题是:

    HSSFRow row1 = worksheet.createRow((short) 0); // always the same row
    ....
    cellA1.setCellValue(name); // always the same cell
    cellB1.setCellValue(surname); // always the same cell
    

    你总是写到同一行的同一个单元格!建议的解决方案类似于您的代码。 请注意(感谢评论)CellStyle 只会被创建一次并被多次使用。

    HSSFCellStyle cellStyle = workbook.createCellStyle();
    cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);
    cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    
    Row row;
    Cell nameCell;
    Cell surNameCell;
    int rowNum = 0;
    for (Driver driver : driverList){
      row = worksheet.createRow(rowNum++); // here I'm incrementing the excel row
    
      nameCell = row.createCell((short) 0);
      nameCell .setCellValue("Ad");
      nameCell .setCellStyle(cellStyle);
    
      urNameCell= row1.createCell((short) 1);
      surNameCell.setCellValue("Soyad");
      surNameCell.setCellStyle(cellStyle);
    
      String name = driver.getName();
      String surname = driver.getSurname();
      String birthDay = driver.getBirthDay();
      nameCell.setCellValue(name);
      surNameCell.setCellValue(surname);
    }
    

    【讨论】:

    • 您正在覆盖循环中的 CellStyle。在循环之前创建 2 个单独的 CellStyles 并重用它们以使其正常工作。
    • 是的。我会解决的。 apache poi文档中也提到了这一点。
    【解决方案3】:

    我觉得这段代码……

    cellA1.setCellValue(name);
    cellB1.setCellValue(surname);
    

    ... 表示您正在尝试将所有内容写入单元格 A1 和 B1,因此始终会覆盖循环中的先前内容。

    【讨论】:

      猜你喜欢
      • 2018-10-31
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      相关资源
      最近更新 更多