0

我正在尝试在 Apache POI 中打开一个文件,该文件位于内部存储中,我正在尝试使用 getFilesDir() 访问它,如下所示:

File file = new File(fname);
OPCPackage opcPackage = OPCPackage.open(context.getApplicationContext().getFilesDir()+"/"+fname);
XSSFWorkbook workbook = new XSSFWorkbook(opcPackage);

它不工作!我不知道出了什么问题。我也尝试过 context.getFilesDir() 但无济于事。我是一个相对新手,所以很明显我做错了什么。其余代码也在下面,欢迎批评!

try {
  File file = new File(fname);
  OPCPackage opcPackage = OPCPackage.open(context.getApplicationContext().getFilesDir()+"/"+fname);
  XSSFWorkbook workbook = new XSSFWorkbook(opcPackage);

  // Get the first sheet from workbook 
  XSSFSheet mySheet = workbook.getSheetAt(0);

  Iterator rowIter = mySheet.rowIterator();

  while (rowIter.hasNext()) {
    HSSFRow myRow = (HSSFRow) rowIter.next();
    Iterator cellIter = myRow.cellIterator();

    while(cellIter.hasNext()){
      HSSFCell myCell = (HSSFCell) cellIter.next();
      String TAG = "ExelLog";
      Log.d(TAG , "Cell Value: " +  myCell.toString());
      System.out.println("Cell Value: " +  myCell.toString());
      Toast.makeText(context, "cell Value: " + myCell.toString(), Toast.LENGTH_SHORT).show();
     }
   }
 } catch (Exception e) {
   System.out.println("error1");
   e.printStackTrace();
 }

谢谢!:)

4

0 回答 0