xlsx 通过 apache poi 读取
xlsx reading through apache poi
我只需要从我的 sd 卡中 select 一个 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);
}
这是接收 selected .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]
提前感谢您的帮助.....
摆脱捕获 - e.printStackTrace(),这可能隐藏了您获得的实际异常。你最好回顾一下如何正确记录这些。
并查看 https://github.com/andruhon/android5xlsx,它为在 Android 上使用 Apache POI 时出现的问题提供了一些解决方法。
我只需要从我的 sd 卡中 select 一个 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);
}
这是接收 selected .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]
提前感谢您的帮助.....
摆脱捕获 - e.printStackTrace(),这可能隐藏了您获得的实际异常。你最好回顾一下如何正确记录这些。
并查看 https://github.com/andruhon/android5xlsx,它为在 Android 上使用 Apache POI 时出现的问题提供了一些解决方法。