我是Android的新手还在创建我的第一个应用程序。我试图从静态excel文件中读取(数据不会改变)并进行一些计算。我已经浏览了各种在线资源,但是并没有指定将我的excel文件存储到哪里(也许它太过白痴了)但不幸的是我不知道。
我做了什么:
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;
}
当onlclick我将使用readExcelFile调用(ScoreActivity.this,“myExcel.xlsx”);
有人可以提供建议吗
答案 0 :(得分:1)
如果您希望将文件与应用程序一起发送,则可以将其放在assets
或res/raw
文件夹中,然后从那里读取。否则,你可以把它放在SD卡上,但我看到你已经这样做了。