简体   繁体   English

使用Apache POI在Android Studio中打开文件并在项目中打开文件

[英]Open File in Android Studio with file in project using Apache POI

I have placed an xlsx file named menuA as you may see in the photo below: 我已经放置了一个名为menuA的xlsx文件,如下图所示:

菜单A.xlsx

But I fear that I am not accessing it correctly using FileInputStream 但是我担心我无法使用FileInputStream正确访问它

FileInputStream file = null;
        try {
            file = new FileInputStream(new File("menuA.xlsx"));
        } catch (Exception e) {
            e.printStackTrace();
        }


    XSSFWorkbook workbook=null;
    try {
        workbook = new XSSFWorkbook(file);
    } catch (IOException e) {
        e.printStackTrace();
    }

    //create a sheet object
    XSSFSheet sheet = workbook.getSheetAt(0);

    //that is for evaluate the cell type
    FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();

I have moved from NetBeans to Android Studio, where my code works perfectly in Android Studio. 我已经从NetBeans迁移到Android Studio,在这里我的代码可以在Android Studio中完美运行。

You can put the same file in the assets folder (The place to put static resources for easy access) and access those assets as following 您可以将相同的文件放入资产文件夹(放置静态资源以方便访问的位置),然后按以下方式访问这些资产

InputStream is = getAssets().open("menuA.xlsx");

EDIT: 编辑:

Since the library that you are using FileInputStream , You can copy the file over to the application's private data storage, which is getExternalFilesDir(null) . 由于使用的是FileInputStream库,因此可以将文件复制到应用程序的私有数据存储中,该存储为getExternalFilesDir(null) and access the file from there. 并从那里访问文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM