InformáticaBásico1 respuestas

Como leer archivos Excel desde netbeans?

Como leer archivos Excel desde netbeans?

Pregunta de Informática · 1 respuesta · 9 votos · mejor respuesta de Alexrap972003

En resumen

Este es un método que te lee los datos del archivo exel y te retorna la información dentro de un vector.

Respuestas de la comunidad

Mejor respuesta

Alexrap972003
0

Este es un método que te lee los datos del archivo exel y te retorna la información dentro de un vector.

__________________________________________________ __________________________________________________ ______________________________ / * * * Método que se encarga de leer los datos de un archivo en formato excel * @param file Archivo que se va a proceder a leer * @return Vector que contiene la informacion del archivo * /

public Vector leeArchivo(File file){

Vector informacionArchivo = new Vector() ;

POIFSFileSystem poifsFileSystem = null ;

try {

poifsFileSystem = new POIFSFileSystem(new FileInputStream(file)) ;

} catch (FileNotFoundException ex) {

ex.

PrintStackTrace() ;

} catch (IOException ex) {

ex.

PrintStackTrace() ;

}

HSSFWorkbook hssfWorkbook = null ;

try {

hssfWorkbook = new HSSFWorkbook(poifsFileSystem) ;

} catch (IOException ex) {

ex.

PrintStackTrace() ;

}

HSSFSheet hssfSheet = hssfWorkbook.

GetSheetAt(0) ;

Iterator iterator = hssfSheet.

RowIterator() ; / / Recorro datos de fila en fila

while(iterator.

HasNext()){

HSSFRow hssfRow = (HSSFRow)iterator.

Next() ;

Iterator iteratorAuxiliar = hssfRow.

CellIterator() ;

Vector informacionFila = new Vector() ; / / Me barro todos los elementos de una fila

for(short i = hssfRow.

GetFirstCellNum() ; i < hssfRow.

GetLastCellNum() ; i + + ){

HSSFCell hssfCell = hssfRow.

GetCell(i) ;

if(hssfCell !

= null){

switch(hssfCell.

GetCellType()){

case HSSFCell.

CELL_TYPE_BLANK : informacionFila.

Add("") ; break ;

case HSSFCell.

CELL_TYPE_BOOLEAN : informacionFila.

Add(hssfCell.

GetBooleanCellValue() ) ; break ;

case HSSFCell.

CELL_TYPE_FORMULA : informacionFila.

Add(hssfCell.

GetStringCellValue()) ; break ;

case HSSFCell.

CELL_TYPE_NUMERIC : informacionFila.

Add(hssfCell.

GetNumericCellValue() ) ; break ;

case HSSFCell.

CELL_TYPE_STRING : informacionFila.

Add(hssfCell.

GetStringCellValue()) ; break ;

default :

}

}

}

informacionArchivo.

Add(informacionFila) ;

}

return informacionArchivo ;

}

__________________________________________________

Espero que te sirva con este método he logrado leer datos de un archivo de 12 megas.

Preguntas frecuentes

Como leer archivos Excel desde netbeans?

Este es un método que te lee los datos del archivo exel y te retorna la información dentro de un vector.