如何从Android中的特定存储中读取Excel文件?

时间:2016-11-24 05:32:49

标签: java android excel apache-poi

我在为我的大学项目工作。我搜索了很多,但不知道为什么excel不读。我在Android Studio中添加了this jar文件。遵循this教程。其实我很困惑。但是我已经添加了一些日志和祝酒词。他们什么都没有。我调试了它,事实上在调试的情况下,它什么也没显示。您能否建议我阅读和编写Excel或任何代码段的好方法?

代码如下:

    if (!isExternalStorageAvailable() || isExternalStorageReadOnly())
    {
        Log.w("FileUtils", "Storage not available or read only");
        return;
    }

    try{
        // Creating Input Stream
        sharedPreferences = MyApplication.preferences;
        final int projectID = sharedPreferences.getInt("count", 0);
        int taskID = 0;
        final String count = sharedPreferences.getString("project", projectFile);
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"
                + mainFolder + "/" + count,"/task.xls");
        Log.e("full file",file.toString());
        FileInputStream myInput = new FileInputStream(file);
        Log.e("full file",myInput.toString());
        // Create a POIFSFileSystem object
        POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
        Log.e("full file",myFileSystem.toString());
        // Create a workbook using the File System
        HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
        Log.e("full file",myWorkBook.toString());

 //       Workbook wb = WorkbookUtil.create(new File("MyExcel.xls"));

        // Get the first sheet from workbook
        HSSFSheet mySheet = myWorkBook.getSheetAt(0);
        Log.e("full file",mySheet.toString());
        /** We now need something to iterate through the cells.**/
        Iterator<Row> rowIter = mySheet.rowIterator();

        HSSFRow nameRow = mySheet.getRow(1);
        String name = nameRow.getCell(0).toString();
        projectname.setText(name);
        Log.e("full file",name.toString());


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

    return;

日志在此行之后停止工作:

 FileInputStream myInput = new FileInputStream(file);

日志文件是:

11-24 12:02:46.993 9177-9196/? E/linker: "/system/bin/app_process32":  ignoring 2-entry DT_PREINIT_ARRAY in shared library!
11-24 12:02:48.055 9177-9267/? E/fb4a(:<default>):0lk: E1124  12:02:48.000000 -1703212752   xplat/liger/src/proxygen/facebook/httpclient/java/src/cpp/HTTPClient.cpp:590]    failed to get socket address from :53
11-24 12:02:48.062 9177-9267/? E/fb: failed to get socket address from :53
11-24 12:02:50.857 8986-8986/razon.language E/razon.language.Home$6: selected file /storage/emulated/0/Language/Project3/task.xls
11-24 12:02:50.857 8986-8986/razon.language E/moved: Moved To /Language/Project3/
11-24 12:02:50.862 8986-8986/razon.language E/full file: /storage/emulated/0/Language/Project3/task.xls
11-24 12:02:50.862 8986-8986/razon.language E/full file:  java.io.FileInputStream@2f04e7c

1 个答案:

答案 0 :(得分:0)

我是基于你的最后一次堆栈追踪写的,

  

org.apache.poi.poifs.filesystem.OfficeXmlFileException:提供的   数据似乎在Office 2007+ XML中。你在呼唤这部分   POI处理OLE2 Office文档。你需要打电话给   POI的不同部分来处理这些数据(例如XSSF而不是HSSF)

正如跟踪消息所示,您尝试从中读取数据的文件具有Office 2007及更高版本支持的格式。 POI有一个名为XSSF(see the very basic in first paragraph)的组件,可用于处理此类文档。

因此,从技术上讲,您需要将HSSFWorkbook, HSSFSheet, HSSFRow, HSSFCell等替换为XSSFWorkbook, XSSFSheet, XSSFRow, XSSFCell等等。至于以下内容,

POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);

POIFSFileSystem(或类似地,NPOIFSFileSystem)仅用于.xls文档。 .xlsx文档的等效项为OPCPackageexample)。它直接由File构建,因此您的代码应如下所示。

OPCPackage pkg = OPCPackage.open(file);

希望它有所帮助。