Apache POI:excel文件中的内容损坏

时间:2017-12-29 14:29:37

标签: java excel apache-poi

我正在写一个写入Excel文件的方法。在调用之前,我创建了一个工作簿和一个工作表。代码执行时没有任何错误,但是当打开创建的Excel文件时,我收到消息:我们发现某些内容存在问题......

我的方法如下:

public void writeToCell(int rowNumber, int cellNumber, Double content) {
    Row row = sheet.createRow(rowNumber);
    Cell cell = row.createCell(cellNumber);

    cell.setCellValue(content);

    try (FileOutputStream outputStream = new FileOutputStream(month + ".xlsx", true)) {
        workbook.write(outputStream);
        outputStream.flush();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

这就是我调用方法的方法:

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet(month);

writeToCell(25, 4, 0.0);
writeToCell(25, 6, 23.32);

1 个答案:

答案 0 :(得分:1)

您不应该明确地将数据附加到Excel工作簿,这也是@Axel在评论中指出的

Microsoft.PowerShell.Archive\Expand-Archive -Path $Path -Destination $Dest

代替

try (FileOutputStream outputStream = new FileOutputStream(month + ".xlsx", true)) 

旁注,

try (FileOutputStream outputStream = new FileOutputStream(month + ".xlsx")) 

writeToCell(25, 4, 0.0); writeToCell(25, 6, 23.32); 的最后一次调用将覆盖同一第25行的先前值。因为,您在每次通话中都会创建新的writeToCell

Row
相关问题