更改标题中任何单元格的值时,Excel文件已损坏(列标题)

时间:2017-02-27 16:15:40

标签: java excel apache-poi

我尝试使用JAVA POI读取和使用Xslx文件,编辑并保存。但是当我更改第0行(Header)上的任何单元格值时,输出文件会被破坏。

Excel可以通过修复表来自/xl/tables/table.xm恢复文件。

    FileInputStream file = new FileInputStream("inventorywithbin431.xlsx");
    XSSFWorkbook workbook = (XSSFWorkbook) WorkbookFactory.create(file);
    XSSFSheet rawdata = workbook.getSheetAt(0);

    String[] headers = new String[] { "ItemCodeNoFRU","Company","ItemCode","ItemName","ItemGroup","PartNumber","Warehouse","Segment","Bin","WareHouseQty","OnOrder","IsCommited","BinQty","Cost","BinExtCost"};

    //Set Header
    for(int i=0;i<14;i++)
        rawdata.getRow(0).getCell(i).setCellValue(headers[i]);

    file.close();
    //write changes
    try ( 
        FileOutputStream output_file = new FileOutputStream("inventorywithbin431.xlsx")) {
        workbook.write(output_file);
        output_file.flush();
        output_file.close();
    } 

0 个答案:

没有答案