• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

163
Vistas
How to read both xls and xlsx with Apache Poi library?

I have an app that reads from an excel. The problem is that I only can read xlsx files. I cant read xls files. This is my code:

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


                }
            });
almost 3 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can create Workbook with WorkbookFactory.create method like that:

InputStream inp = new FileInputStream(new FileInputStream("/path/to/file.xls"));
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
almost 3 years ago · Santiago Trujillo Denunciar

0

For reading xlsx file
Workbook workbook = new XSSFWorkbook(inputStream);

For reading xls file (< 2007)
Workbook workbook = new HSSFWorkbook(inputStream);

No other changes required.

almost 3 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda