2013-07-21 49 views
1

我正在嘗試讀取excel文件。該文件保存在我的工作空間中,名爲RoCom_DB,文件名爲RoCom.xlsx在Android中讀取Excel文件時出現問題

我嘗試使用下面的代碼來讀取文件:

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 ""; 
    } 

的問題是每次我嘗試讀取文件時,我得到一個File not found exception。我已經使用了必要的poi-3.7.jar這一個,並保持這一段代碼在我manifest.xml

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

我不想從assets目錄使用Excel,因爲它僅支持Excel高達1 MB和我的文件中有可能會增加規模。

有人能告訴我我做錯了什麼嗎?任何幫助是極大的讚賞 。謝謝 。

回答

1

我得到這個以相當奇怪的方式工作。 。

1>首先,我擺脫了POI.jar,而是使用jxl.jar。然後我的excel工作書再次以xslx的格式出現,因爲它是在ms excel 2007中,所以我將它轉換爲xls即excel(97-2003)格式。

2>然後,我把我的Excel中使用下面的命令到SD卡:

  • 一>首先讓在runcmd(對於Windows)

    B>導航到您adb.exe存在。 (它會在 android - > sdk - >平臺工具)

    c>然後將你的xls複製到保存adb.exe的那個文件夾中。

    d>現在運行adb shell。將打開一個unix shell。轉到:CD/MNT,並使用SD卡的 變更許可:chmod 777 /sdcard

    E>使用現在回到批提示:exit命令和類型: adb push file.xls /mnt/sdcard/

    ˚F>然後使用cd和變化裏面去/mnt/sdcard/使用文件權限 :chmod 777 file.xls

3>現在所有重要的事情都做了,我寫了下面一段代碼來解決這一問題:

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; 
    } 

希望這可以幫助那些在經歷同樣的過程時沒有太大好運的人。乾杯!

0

public File (String dirPath, String name)在路徑和名稱之間放置了一個路徑分隔符,因此您應該刪除'RoCom_DB'後面的最後一個'/'。

您也可以存儲在一個字符串此值

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

,並顯示它,以確保它是正確形成。

+0

謝謝,我會盡力回覆你。 –