我只需要从我的SD卡中选择一个XLSX文件,然后通过apache poi阅读它。这是我打算使用SD卡并选择xlsx文件
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
以下是接收所选.xlsx文件路径的代码,这里是生成的错误我认为它在调用工作簿但我无法清除它
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_FROM_FILE && resultCode == RESULT_OK) {
// File file = new File(Environment.getExternalStorageDirectory(), data.getData().toString());
Uri path =data.getData();
ContentResolver cr=this.getContentResolver();
File fpath=new File(path.toString());
try {
//imputStream
InputStream myfile=cr.openInputStream(path);
Log.e("myapp","here");
//Workbooh
Workbook wrk=new HSSFWorkbook(myfile);
//sheet
Sheet firstsheet=wrk.getSheetAt(0);
//RowIterator
Iterator<Row> iterator=firstsheet.iterator();
while(iterator.hasNext())
{
Row next=iterator.next();
//CellIterator
Iterator<Cell> cellIterator=next.cellIterator();
while(cellIterator.hasNext())
{
Cell cell=cellIterator.next();
switch (cell.getCellType())
{
case Cell.CELL_TYPE_STRING:
Toast.makeText(getApplicationContext(),cell.getStringCellValue()+" ",Toast.LENGTH_SHORT).show();
break;
case Cell.CELL_TYPE_NUMERIC:
Toast.makeText(getApplicationContext(),cell.getNumericCellValue()+" ",Toast.LENGTH_SHORT).show();
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我在下面给出的错误列表
04-26 12:18:17.067 2736-3126/? E/Watchdog: !@Sync 1540 [04-26 12:18:17.069]
04-26 12:18:47.067 2736-3126/? E/Watchdog: !@Sync 1541 [04-26 12:18:47.072]
04-26 12:19:17.067 2736-3126/? E/Watchdog: !@Sync 1542 [04-26 12:19:17.073]
04-26 12:19:47.072 2736-3126/? E/Watchdog: !@Sync 1543 [04-26 12:19:47.075]
04-26 12:20:17.072 2736-3126/? E/Watchdog: !@Sync 1544 [04-26 12:20:17.076]
提前感谢您的帮助.....
答案 0 :(得分:2)
摆脱catch - e.printStackTrace(),这可能会隐藏你得到的实际异常。您最好查看如何正确记录这些内容。
并查看https://github.com/andruhon/android5xlsx,它为在Android上使用Apache POI时的问题提供了一些解决方法。