在android中读取excel文件时出现问题

时间:2013-07-21 16:33:33

标签: android excel

我正在尝试阅读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,因为它只支持excel高达1 mb,而且我的文件有可能增加。

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

2 个答案:

答案 0 :(得分:1)

我让这个以相当奇怪的方式工作。

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

2 - ;然后我使用以下命令将我的excel推送到SD卡:

  • A&GT;首先在cmd(对于Windows)中提供run

    B个导航到adb.exe所在的位置。(它会在里面 android - &gt; sdk - &gt;平台工具)

    c取代;然后将xls复制到保存adb.exe的文件夹中。

    d取代;现在运行adb shell。将打开一个unix shell。转到:cd / mnt和 使用:chmod 777 /sdcard

    更改SD卡的权限

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

    f是氟烃基;然后使用/mnt/sdcard/进入cd并更改权限 使用文件:chmod 777 file.xls

3&GT;现在所有重要的事情都已完成,我编写了以下代码来解决这个问题:

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

希望这可以帮助那些在经历相同过程时陷入困境的人。干杯!

答案 1 :(得分:0)

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

您也可以存储此值

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

在一个字符串中并显示它以确保它正确形成。