Skip to content Skip to sidebar Skip to footer

Issue While Reading An Excel File In Android

I am trying to read an excel file. The file is kept in my work space in a folder called RoCom_DB and the file name is RoCom.xlsx . I am trying to read the file using the following

Solution 1:

I got this one to work in quite a strange way. .

1> First of all i got rid of POI.jar and instead used jxl.jar. Then again my excel work book was in the format of xslx becuase it was in ms excel 2007, so i converted it to xls i.e. excel(97-2003) format.

2> Then i pushed my excel to sdcard using the following commands :

  • a> First give cmd in run(for windows)

    b> Navigate to where your adb.exe is present.(It will be inside android-->sdk-->platform tools)

    c> Then copy your xls to that folder where adb.exe is kept.

    d> Now run adb shell. Will open a unix shell. Go to : cd /mnt and change permission of sd card using : chmod 777 /sdcard

    e> Now return to batch prompt using : exit command and type : adb push file.xls /mnt/sdcard/

    f> Then go inside /mnt/sdcard/ using cd and change permission for file using : chmod 777 file.xls

3> Now that all important things are done , i wrote the following piece of code to work this out :

public String readComplexExcelFile(Context context, String userInput){
       StringrequiredContents="";
       try{

           FileinputWorkbook=newFile(Environment.getExternalStorageDirectory()+"/myFile.xls");
           Workbook w;

           // Create a workbook using the File System
           w = Workbook.getWorkbook(inputWorkbook);

           // Get the first sheet from workbook Sheetsheet= w.getSheet(0);

           /** We now need something to iterate through the cells.**/for (intj=0; j < sheet.getColumns(); j++) {
               for (inti=0; i < sheet.getRows(); i++) {
                 Cellcell= sheet.getCell(j, i);
                 if(cell.getContents().equalsIgnoreCase(userInput)){
                     CellcellCorrespond= sheet.getCell(j+1, i);
                     requiredContents = cellCorrespond.getContents();
                     break;
                 }

               }
             }


       }catch (Exception e){
           e.printStackTrace();
          }
       return requiredContents;
   }

Hopefully, this will help people who are stuck without much luck while going through the same process. Cheers !

Solution 2:

public File (String dirPath, String name) places a path separator between the path and the name, so you should remove the last '/' after 'RoCom_DB'.

You might also store this value

Environment.getExternalStorageDirectory()
               + "/Android/data/" + getApplicationContext().getPackageName()
               + "/RoCom_DB"

in a string and display it to make sure it is correctly formed.

Post a Comment for "Issue While Reading An Excel File In Android"