1

我正在尝试读取一个 excel 文件。该文件保存在我的工作空间中名为RoCom_DB的文件夹中,文件名为RoCom.xlsx.

我正在尝试使用以下代码读取文件:

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,因为它只支持最大 1 mb 的 excel,而且我的文件有可能增加大小。

谁能告诉我我做错了什么?任何帮助是极大的赞赏 。谢谢 。

4

2 回答 2

1

我让这个以一种非常奇怪的方式工作。.

1>首先我摆脱了POI.jar,而是使用了jxl.jar. 然后我的excel工作簿的格式又是xslx因为它是ms excel 2007,所以我将它转换为xls即excel(97-2003)格式。

2> 然后我使用以下命令将我的 excel 推送到 sdcard:

  • a> 首先让步(适用cmdrunwindows)

    b> 导航到您所在的adb.exe位置。(它将在 android-->sdk-->platform tools 内)

    c> 然后将您的 xls 复制到保存 adb.exe 的文件夹中。

    d> 现在运行 adb shell。将打开一个unix shell。转到: cd /mnt 并使用以下方法更改 sd 卡的权限:chmod 777 /sdcard

    e> 现在使用 : 命令返回批处理提示exit并输入 : adb push file.xls /mnt/sdcard/

    f> 然后进入/mnt/sdcard/使用cd并更改文件的权限使用: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;
   }

希望这将帮助那些在经历相同过程时遇到困难的人。干杯!

于 2013-07-22T10:46:34.737 回答
0

public File (String dirPath, String name)在路径和名称之间放置一个路径分隔符,因此您应该删除“RoCom_DB”之后的最后一个“/”。

您也可以存储此值

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

在一个字符串中并显示它以确保它的格式正确。

于 2013-07-21T17:32:01.230 回答