10

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!!

Theresa Forster
  • 1,914
  • 3
  • 19
  • 35
ketelagoreng
  • 109
  • 1
  • 1
  • 7

5 Answers5

7

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);
Gagravarr
  • 47,320
  • 10
  • 111
  • 156
  • I still get the same error, the original File's size is 25kb, but the output File's size just 17kb (with the same content or maybe a few different) – ketelagoreng May 22 '15 at 13:54
  • Make sure you're saving to a different file. POI doesn't (currently) support in-place writes, so it needs to be a different file – Gagravarr May 22 '15 at 16:10
  • String urii = fileUri.replace(".xls", "0.xls"); this line for change the uri and name file, please help meeeeeee..... :(( – ketelagoreng May 22 '15 at 16:37
  • I try the code in the java project, it works fine, but when I use this code to android project (using bluestack emulator) give error output (corrupt file) – ketelagoreng May 23 '15 at 03:14
  • How do the two files differ? Sadly it sounds like an Android bug though, if it works in regular Java... – Gagravarr May 23 '15 at 11:25
  • 1
    bug from emulator (maybe), using real android device doesn't give me error file output, I was using bluestack emulator – ketelagoreng May 23 '15 at 14:51
2

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.

Akash Rajbanshi
  • 1,553
  • 11
  • 23
  • yes..but to be able to be opened on MS Excel 2013 you need to use...XSSFWorkbook – Akash Rajbanshi May 22 '15 at 13:25
  • my input file is xls type and the output is xls type too.. Also, I have tried to upload the output file to google docs, and failed to upload (I think because the output file is corrupt) – ketelagoreng May 22 '15 at 13:29
  • @AkashRajbanshi I need to write the file type as xlsx for which I use FileOutputStream class. Can you suggest the way to write the file using the right workbook class – Suresh Jan 12 '18 at 14:54
0

Solved :

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

Thanks everyone :D

ketelagoreng
  • 109
  • 1
  • 1
  • 7
0

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

-1

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

Daniil Shevelev
  • 11,739
  • 12
  • 50
  • 73