Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 from file

here is the code

FileInputStream fis = null;
    try {
        fis = new FileInputStream(fileUri); //not error at fileUri
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String urii = fileUri.replace(".xls", "0.xls"); //not error
    File fisx = new File(urii);

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

    Sheet sheet = workbook.getSheetAt(0);

    Row row = sheet.getRow(0);

    Cell cell = row.getCell(0);

    String p = cell.getStringCellValue();

    TextView a = (TextView) findViewById(R.id.txtUri);

    cell.setCellValue(new String("popo"));
    String x = cell.getStringCellValue();

    TextView b = (TextView) findViewById(R.id.txtFile);

    a.setText(p);
    b.setText(x);

    OutputStream fos = null;

    fos = new FileOutputStream(fisx);
    workbook.write(fos); //main problem
    fos.flush();
    fos.close();

Thanks for your help!!

like image 959
ketelagoreng Avatar asked May 22 '15 12:05

ketelagoreng


People also ask

Why does Excel keep saying my file is corrupted?

The main causes for "the file is corrupted and cannot be opened" in Microsoft Excel or Office are: The changes of settings in Microsoft Office after upgrading or reinstallation. Protection of your computer against the files come from another computer. The Excel or Word file is corrupted.

How do you fix Excel Cannot open the file format file extension is not valid corrupted on Mac?

You can restore the Excel file format, or the file extension is not valid from previous versions. Select the damaged file. Right-click it and click "Properties" > "Previous Version". A list of previous versions will appear; you need to select the option you are interested in and click "Restore" to recover.

What to do if XLSX file is not opening?

Open Excel and, on the taskbar, select File. Then select Options -> Export -> Change File Type. You need to directly change the file extension depending on the version of Excel installed. Check if the error “Excel cannot open the file” is resolved.


2 Answers

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);
like image 109
Gagravarr Avatar answered Oct 09 '22 05:10

Gagravarr


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.

like image 33
Akash Rajbanshi Avatar answered Oct 09 '22 03:10

Akash Rajbanshi