Ver Mensaje Individual
  #3 (permalink)  
Antiguo 30/05/2013, 06:19
dubby
 
Fecha de Ingreso: agosto-2010
Mensajes: 51
Antigüedad: 13 años, 8 meses
Puntos: 1
Respuesta: Importar excel a java por web

Buenas,

Donde tengo el atasco es que no se como sacar los datos que me pinta en consola y ponerlos en la jsp, me imagino que tendre que pintarme como una tabla que simule una jsp dentro del metodo java

pongo el codigo que tengo hecho

Código Java:
Ver original
  1. package excel.prueba;
  2.  
  3. import java.io.FileInputStream;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.List;
  7.  
  8. import org.apache.poi.hssf.usermodel.HSSFCell;
  9. import org.apache.poi.hssf.usermodel.HSSFRow;
  10.  import org.apache.poi.hssf.usermodel.HSSFSheet;
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  12. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  13.  
  14. import excel.prueba.EjemploExcel;
  15.  
  16. public class EjemploExcel {
  17.  
  18.     public static void main(String[] args){
  19.         String fileName = "D:\\prueba.xls";
  20.         new EjemploExcel().readExcelFile(fileName);
  21.     }
  22.    
  23.         @SuppressWarnings({"rawtypes", "unchecked"})
  24.     private void readExcelFile(String fileName)    {
  25.                List cellDataList = new ArrayList();
  26.         try{
  27.               FileInputStream fileInputStream = new FileInputStream(fileName);
  28.                POIFSFileSystem fsFileSystem = new POIFSFileSystem(fileInputStream);
  29.        
  30.         HSSFWorkbook workBook = new HSSFWorkbook(fsFileSystem);
  31.          HSSFSheet hssfSheet = workBook.getSheetAt(0);
  32.               Iterator rowIterator = hssfSheet.rowIterator();
  33.              while (rowIterator.hasNext()){
  34.                 HSSFRow hssfRow = (HSSFRow) rowIterator.next();
  35.                 Iterator iterator = hssfRow.cellIterator();
  36.                 List cellTempList = new ArrayList();
  37.                      while (iterator.hasNext()){
  38.                         HSSFCell hssfCell = (HSSFCell) iterator.next();
  39.                         cellTempList.add(hssfCell);
  40.                     }
  41.                 cellDataList.add(cellTempList);
  42.              }
  43.         }catch (Exception e){
  44.             e.printStackTrace();
  45.         }
  46.             printToConsole(cellDataList);
  47.     }
  48.     /**
  49.     * Pinta las celdas en la consola.
  50.     * @param cellDataList - List of the data's in the spreadsheet.
  51.     */
  52.     @SuppressWarnings("rawtypes")
  53.      private void printToConsole(List cellDataList){
  54.         for (int i = 0; i < cellDataList.size(); i++){
  55.             List cellTempList = (List) cellDataList.get(i);
  56.             for (int j = 0; j < cellTempList.size(); j++){
  57.                  HSSFCell hssfCell = (HSSFCell) cellTempList.get(j);
  58.                 String stringCellValue = hssfCell.toString();
  59.                 System.out.print(stringCellValue + "\t");
  60.             }
  61.          System.out.println();
  62.         }
  63.     }
  64. }