File Excel From Apache POI Cant Open by Ms Excel (corrupt)

后端 未结 5 617
囚心锁ツ
囚心锁ツ 2020-12-19 01:56

I don\'t know why the file I write using POI cant be opened by Ms Excel 2013, but the file is still readable by POI. (cell value can be changed)

this is the error fr

5条回答
  •  清酒与你
    2020-12-19 02:09

    There are two issues with your code. Firstly this:

    FileInputStream fis = null;
    try {
        fis = new FileInputStream(fileUri);
    

    As explained in the Apache POI Docs, don't use an InputStream if you have a File!

    Secondly, this:

     Workbook workbook = null;
     workbook = new HSSFWorkbook(fis);
    

    That will only work for .xls files, not for .xlsx ones. Instead, you need to use WorkbookFactory which identifies the type and gives you the right workbook for the format

    So, change your code to be

    File file = new File(fileUri);
    Workbook workbook = WorkbookFactory.create(file);
    

提交回复
热议问题