Java 从内部存储ApachePOI检索excel文件

Java 从内部存储ApachePOI检索excel文件,java,android,excel,apache-poi,adt,Java,Android,Excel,Apache Poi,Adt,我尝试在Apache POI中打开一个文件,该文件位于内部存储器中,我尝试使用GetFileDir()访问它,如下所示: File file = new File(fname); OPCPackage opcPackage = OPCPackage.open(context.getApplicationContext().getFilesDir()+"/"+fname); XSSFWorkbook workbook = new XSSFWorkbook(opcPackage); 它不起作用了!

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

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();
 }

谢谢!:)

它怎么不起作用?这是一个错误吗?如果没有更多关于失败的细节,我们无法帮助…问题就在最初阶段。它不会给出错误,但LogCat中有很多警告。这就是为什么我最初使用couts(system.out.printlns)来跟踪错误。如果行文件File=新文件(fname)之前的I system.out;在尝试之后,它可以工作,但在XSSFWorkbook工作簿=新XSSFWorkbook(opcPackage);)之后无法system.out;。希望你能帮忙谢谢。你确定这是指向正确的地方吗<代码>上下文.getApplicationContext().getFilesDir()+“/”+fnameakokskis。。。错误在此行OPCPackage OPCPackage=OPCPackage.open(context.getApplicationContext().getFilesDir()+“/”+fname);不,我很确定现在这条路不对。问题是我不知道正确的路径是什么。这是我试图访问该文件(我做了大量阅读,这就是我所想到的)它还发出了一个错误,上面写着“打开跟踪文件时出错:没有这样的文件或目录(2)”,上次出现这个错误时,我只是增加了模拟器的SD卡内存,并修复了它。但现在我认为这与这条道路有关。