我有一个从Excel读取的应用程序。问题是我只能读取xlsx文件。我无法读取xls文件。这是我的代码:

FileInputStream file = new FileInputStream(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/"+nomeExcel));
                        HSSFWorkbook workbook = new HSSFWorkbook(file);
                        HSSFSheet sheet = workbook.getSheetAt(0);

                        int rowCtr = 34;
                        Row myRow = sheet.getRow(rowCtr++);
                        while (myRow != null) {
                            Cell nif = myRow.getCell(0);
                            Cell marcaexploracao = myRow.getCell(1);
                            Cell numerochip = myRow.getCell(2);
                            Cell marcaauricular = myRow.getCell(3);
                            Cell datanascimento = myRow.getCell(4);

                            bd.addAnimais(numerochip.toString().replace("\u00a0",""),marcaexploracao.toString().trim(),marcaauricular.toString(),datanascimento.toString(),nif.toString(),0,"","","","",0);
                            myRow = sheet.getRow(rowCtr++);
                        }

                    }catch (Exception e){e.printStackTrace(); }

                    Toast.makeText(getApplicationContext(),"Excel importado",Toast.LENGTH_SHORT).show();


                }
            });

最佳答案

您可以使用Workbook方法创建WorkbookFactory.create,如下所示:

InputStream inp = new FileInputStream(new FileInputStream("/path/to/file.xls"));
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);

10-07 20:29
查看更多