Excel file gets corrupted when i change the value of any cell in the header (Columns Title)

大城市里の小女人 提交于 2019-12-12 03:43:50

问题


I'm trying to read and Xslx file using JAVA POI, edit it and save it. But when i change any cell value on row#0 (Header) the output file gets corrupted.

Excel can recover the file by repairing, Table from /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();
    } 

来源:https://stackoverflow.com/questions/42490566/excel-file-gets-corrupted-when-i-change-the-value-of-any-cell-in-the-header-col

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!