Issue while reading an excel file in android

2019-08-06 08:27发布

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 code :

public String readComplexExcelFile(Context context){

       try{
           // Creating Input Stream 
           File file = new File(Environment.getExternalStorageDirectory()
                   + "/Android/data/" + getApplicationContext().getPackageName()
                   + "/RoCom_DB/", "RoCom.xlsx");

           FileInputStream myInput = new FileInputStream(file);

           // Create a POIFSFileSystem object 
           POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);

           // Create a workbook using the File System 
           HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);

           // Get the first sheet from workbook 
           HSSFSheet mySheet = myWorkBook.getSheetAt(0);

           /** We now need something to iterate through the cells.**/
           Iterator<Row> rowIter = mySheet.rowIterator();

           while(rowIter.hasNext()){
               HSSFRow myRow = (HSSFRow) rowIter.next();
               Iterator<Cell> cellIter = myRow.cellIterator();
               while(cellIter.hasNext()){
                   HSSFCell myCell = (HSSFCell) cellIter.next();
                   Log.d("", "Cell Value: " +  myCell.toString());
                   Toast.makeText(context, "cell Value: " + myCell.toString(), Toast.LENGTH_SHORT).show();
               }
           }
       }catch (Exception e){
           e.printStackTrace();
          }
       return "";
   }

The problem is every time i try to read the file, i get a File not found exception . I have used the necessary poi-3.7.jar for this one and kept this piece of code in my manifest.xml :

<uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

I don't want to use the excel from assets directory as it only supports excel upto 1 mb and my file has the potential to increase in size .

Can anybody tell me what i am doing wrong ? Any help is greatly appreciated . Thanks .

标签: android excel
2条回答
相关推荐>>
2楼-- · 2019-08-06 08:52

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){
       String requiredContents = "";
       try{

           File inputWorkbook = new File(Environment.getExternalStorageDirectory()+"/myFile.xls");
           Workbook w;

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

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

           /** We now need something to iterate through the cells.**/
           for (int j = 0; j < sheet.getColumns(); j++) {
               for (int i = 0; i < sheet.getRows(); i++) {
                 Cell cell = sheet.getCell(j, i);
                 if(cell.getContents().equalsIgnoreCase(userInput)){
                     Cell cellCorrespond = 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 !

查看更多
不美不萌又怎样
3楼-- · 2019-08-06 09:05

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.

查看更多
登录 后发表回答