2016-04-26 268 views
0

我只需要从我的SD卡中选择一个XLSX文件并通过apache poi阅读它。这是我到SD卡上调用的意图,选择XLSX文件xlsx通过apache poi阅读

 Intent intent = new Intent(); 
     intent.setType("*/*"); 
     intent.setAction(Intent.ACTION_GET_CONTENT); 
     startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE); 
    } 

这里是接收此选择的.xlsx文件的路径代码是错误发生,我认为它在调用的工作簿,但我不能明确它

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == PICK_FROM_FILE && resultCode == RESULT_OK) { 


     // File file = new File(Environment.getExternalStorageDirectory(), data.getData().toString()); 
     Uri path =data.getData(); 
     ContentResolver cr=this.getContentResolver(); 
     File fpath=new File(path.toString()); 


     try { 
      //imputStream 
      InputStream myfile=cr.openInputStream(path); 
      Log.e("myapp","here"); 
      //Workbooh 
      Workbook wrk=new HSSFWorkbook(myfile); 


      //sheet 
      Sheet firstsheet=wrk.getSheetAt(0); 

      //RowIterator 
      Iterator<Row> iterator=firstsheet.iterator(); 
      while(iterator.hasNext()) 
      { 
       Row next=iterator.next(); 

       //CellIterator 
       Iterator<Cell> cellIterator=next.cellIterator(); 
       while(cellIterator.hasNext()) 
       { 
        Cell cell=cellIterator.next(); 

        switch (cell.getCellType()) 
        { 
         case Cell.CELL_TYPE_STRING: 
          Toast.makeText(getApplicationContext(),cell.getStringCellValue()+" ",Toast.LENGTH_SHORT).show(); 
          break; 
         case Cell.CELL_TYPE_NUMERIC: 
          Toast.makeText(getApplicationContext(),cell.getNumericCellValue()+" ",Toast.LENGTH_SHORT).show(); 
        } 
       } 

      } 


     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 


    } 


    } 

我的错误列表

04-26 12:18:17.067 2736-3126/? E/Watchdog: [email protected] 1540 [04-26 12:18:17.069] 
04-26 12:18:47.067 2736-3126/? E/Watchdog: [email protected] 1541 [04-26 12:18:47.072] 
04-26 12:19:17.067 2736-3126/? E/Watchdog: [email protected] 1542 [04-26 12:19:17.073] 
04-26 12:19:47.072 2736-3126/? E/Watchdog: [email protected] 1543 [04-26 12:19:47.075] 
04-26 12:20:17.072 2736-3126/? E/Watchdog: [email protected] 1544 [04-26 12:20:17.076] 

感谢下面给出了提前帮助.....

回答

2

摆脱catch - e.printStackTrace(),这可能隐藏你得到的实际异常。您最好审查如何正确记录这些信息。

然后看看https://github.com/andruhon/android5xlsx,它提供了一些在Android上使用Apache POI时出现的问题的解决方法。

相关问题