我正在嘗試讀取excel文件。該文件保存在我的工作空間中,名爲RoCom_DB
,文件名爲RoCom.xlsx
。在Android中讀取Excel文件時出現問題
我嘗試使用下面的代碼來讀取文件:
public String readComplexExcelFile(Context context){
try{
// Creating Input Stream
File file = new File(Environment.getExternalStorageDirectory()
+ "/Android/data/" + getApplicationContext().getPackageName()
+ "/RoCom_DB/", "RoCom.xlsx");
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<Row> rowIter = mySheet.rowIterator();
while(rowIter.hasNext()){
HSSFRow myRow = (HSSFRow) rowIter.next();
Iterator<Cell> cellIter = myRow.cellIterator();
while(cellIter.hasNext()){
HSSFCell myCell = (HSSFCell) cellIter.next();
Log.d("", "Cell Value: " + myCell.toString());
Toast.makeText(context, "cell Value: " + myCell.toString(), Toast.LENGTH_SHORT).show();
}
}
}catch (Exception e){
e.printStackTrace();
}
return "";
}
的問題是每次我嘗試讀取文件時,我得到一個File not found exception
。我已經使用了必要的poi-3.7.jar
這一個,並保持這一段代碼在我manifest.xml
:
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
我不想從assets
目錄使用Excel,因爲它僅支持Excel高達1 MB和我的文件中有可能會增加規模。
有人能告訴我我做錯了什麼嗎?任何幫助是極大的讚賞 。謝謝 。
謝謝,我會盡力回覆你。 –