android studio中的.xlsx文件

时间:2015-11-03 21:07:12

标签: java android

我有一些代码在netbeans中工作,用于阅读.xlsx文件。我试图在Android Studio中实现它,但没有成功到达我的.xlsx文件:我已经把路径" C \ etc \ etc \"它不起作用。

我将文件复制到drawable文件夹并设置以下变量,但它仍然无效:

String filename = "renk hex.xlsx"; 

我应该把这个.xlsx文件放到哪里?

我在代码中没有错误,当我打电话给负责阅读此文件的班级时,genymotion上的应用程序停止工作。

这是我的代码:

      String filename = "renk hex.xlsx";
    List sheetData = new ArrayList();         // dataItem


    FileInputStream fis = null;
    try {
        fis = new FileInputStream(filename);
        XSSFWorkbook workbook = new XSSFWorkbook(fis);
       XSSFSheet sheet = workbook.getSheetAt(0);
        Iterator rows = sheet.rowIterator();
        while (rows.hasNext()) {
            XSSFRow row = (XSSFRow) rows.next();
            Iterator cells = row.cellIterator();
            List data = new ArrayList();
            while (cells.hasNext()) {
                XSSFCell cell = (XSSFCell) cells.next();
                data.add(cell);
            }

            sheetData.add(data);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

日志:

                Process: hesh.ballc, PID: 3774
java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3823)
        at android.view.View.performClick(View.java:4438)
        at android.view.View$PerformClick.run(View.java:18422)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)

谢谢

1 个答案:

答案 0 :(得分:0)

将其放入assets文件夹(src / main / assets)并按此阅读

InputStream is = context.getAssets().open(filename);

或者在' raw'文件夹和读取如下:

getResources().openRawResource(R.raw.filename);