【发布时间】:2015-02-21 11:55:27
【问题描述】:
我对 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"); 调用时;
谁能给点建议
【问题讨论】:
-
将文件放入 ExternalStorage 并提供路径。
-
我不明白把它放在 ExternalStorage 中。我不应该放入 res 文件夹或资产文件夹吗?