-1

I am very new to android still creating my first application. I am trying to achieve reading from a static excel file(data will not change) and do some computation. I have gone through various online resources but however all does not specify where to store my excel file into(maybe it is too idiotproof) but unfortunately i do not know.

What i have done:

private static void readExcelFile(Context context, String filename) {

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

    try {
        // Creating Input Stream
        File file = new File(context.getExternalFilesDir(null), filename);
        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 rowIter = mySheet.rowIterator();

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

    return;
}

public static boolean isExternalStorageReadOnly() {
    String extStorageState = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
        return true;
    }
    return false;
}

public static boolean isExternalStorageAvailable() {
    String extStorageState = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
        return true;
    }
    return false;
}

and when onlclick i will call by using readExcelFile(ScoreActivity.this, "myExcel.xlsx");

Can someone please advice

10e5x
  • 909
  • 4
  • 15
  • 27

1 Answers1

1

If you want your file to be shipped along with your application, then you could put it in assets or res/raw folders, and then read it from there. Otherwise, you can put it on sdcard, but I see you are already doing this.

Andy Res
  • 15,963
  • 5
  • 60
  • 96
  • oh ya this is what i wanted to ask. thanks. Which should i choose to put into? I do not want my file to be in the sdcard. i am unable to do that while developing right? how can i change my code so that i can put in assets or res/raw – 10e5x Dec 23 '14 at 10:49
  • 1
    Inside the `res` directory, create a folder called `raw` and put your excel there. This answer shows how to read a file from `res/raw`: http://stackoverflow.com/a/2856501/1271435. If you put in the `assets` folder, then you can read it like this: http://stackoverflow.com/questions/9544737/read-file-from-assets. The differences between `res/raw` and `assets` are explained here: http://stackoverflow.com/questions/5583608/difference-between-res-and-assets-directories. But I don't think at this stage you should bother where to put them. – Andy Res Dec 23 '14 at 11:17