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

后端 未结 5 582
囚心锁ツ
囚心锁ツ 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:03

    You are calling getSheetAt(0) but you did not create any sheet before (workbook.createSheet(“name”)

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-19 02:16

    Solved :

    by using real android device instead of bluestack emulator, I dont know why, but it works!!

    Thanks everyone :D

    0 讨论(0)
  • 2020-12-19 02:17

    The solution is to use the .xls extension and NOT .xlsx, as outlined in this answer

    0 讨论(0)
  • 2020-12-19 02:23

    The major problem that i see here is:

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

    Instead you have to use:

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

    TO be readable by MS EXCEL 2013.

    0 讨论(0)
提交回复
热议问题