Ver Mensaje Individual
  #1 (permalink)  
Antiguo 13/10/2015, 10:21
Avatar de HumFaJ
HumFaJ
 
Fecha de Ingreso: mayo-2015
Ubicación: Mexico
Mensajes: 12
Antigüedad: 8 años, 11 meses
Puntos: 0
Información El uso de Apache POI para importar datos de un .xlsx a un JSP

Hola! Que tal!.

Quisiera aprender a usar la api de java (POI) para manejar documentos de microsoft office,
no se donde puedo encontrar orientación pues no soy experto, ya logre usarla para leer un xlsx en consola, pero quiero exportar dichos datos a un JSP. Como se hace?. agradesco a quien me oriente, pues por mas que busco no encuentro nada por ningun lado.
aqui el codigo que me permite leer un .xlsx

Código Java:
Ver original
  1. package poi;
  2.  
  3.  
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.text.ParseException;
  7. import java.util.ArrayList;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. import org.apache.poi.xssf.usermodel.XSSFCell;
  11. import org.apache.poi.xssf.usermodel.XSSFRow;
  12. import org.apache.poi.xssf.usermodel.XSSFSheet;
  13. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  14.  
  15. public class Prueba {
  16.  
  17.     /**
  18.      * @param args the command line arguments
  19.      */
  20.     public Prueba(File fileName) throws ParseException{
  21. List cellDataList = new ArrayList();
  22. int t=0;
  23. try{
  24. FileInputStream fileInputStream = new FileInputStream( fileName);
  25. XSSFWorkbook workBook = new XSSFWorkbook(fileInputStream);
  26. XSSFSheet hssfSheet = workBook.getSheetAt(t);// aqui va el cero
  27. Iterator rowIterator = hssfSheet.rowIterator();
  28. while (rowIterator.hasNext()){
  29. XSSFRow hssfRow = (XSSFRow) rowIterator.next();
  30. Iterator iterator = hssfRow.cellIterator();
  31. List cellTempList = new ArrayList();
  32. while (iterator.hasNext()){
  33. XSSFCell hssfCell = (XSSFCell) iterator.next();
  34. cellTempList.add(hssfCell);
  35. }
  36. cellDataList.add(cellTempList);
  37. }
  38. }catch (Exception e)
  39. {e.printStackTrace();}
  40. Leer(cellDataList);
  41. }
  42. private void Leer(List cellDataList){
  43. for (int i = 0; i < cellDataList.size(); i++){
  44. List cellTempList = (List) cellDataList.get(i);
  45. for (int j = 0; j < cellTempList.size(); j++){
  46. XSSFCell hssfCell = (XSSFCell) cellTempList.get(j);
  47. String stringCellValue = hssfCell.toString();
  48. System.out.print(stringCellValue+" ");
  49. }
  50. System.out.println();
  51. }
  52. }
  53.     public static void main(String[] args) throws ParseException{
  54.         // TODO code application logic here
  55.     File f=new File("C:/prueba.xlsx");
  56. if(f.exists()){
  57. Prueba pb=new Prueba(f);
  58.  
  59. }
  60. }
  61.  
  62. }