Inicio

jueves, 22 de septiembre de 2016

Leer Archivo CVS en Primefaces

En este tutorial vamos a mostrar como leer archivos CVS y posteriomente montraremos en una lista de Datatable utilizando el Primefaces

Procedimientos a seguir, crear una clase llamada Leer con los siguientes atributos

   private String codigo;
    private String dni;
    private  String nombres;
    private String especialidad;

crear los métos de accedo get y set

Posteriormente crear un controller  llamado LeerBean con los atributos
private List<Leer> lista;

crear los métos de accedo get y set

Crear un un método llamado handleFileCVS

con el siguiente codigo

 public void handleFileCVS(FileUploadEvent event) {
         UploadedFile uploadedFile = (UploadedFile) event.getFile();
        InputStream inputStr;
        lista.clear();
        try {
             inputStr = uploadedFile.getInputstream();
            String lineas;
            Scanner dato = new Scanner(inputStr);
            dato.nextLine();
             while (dato.hasNext()) {
                 Leer l = new Leer();
                  lineas = dato.nextLine();
                String[] valores = lineas.split(",");
                 l.setCodigo(valores[0]);
                 l.setDni(valores[1]);
                 l.setNombres(valores[2]);
                 l.setEspecialidad(valores[3]);
                 lista.add(l);
             }
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage().toString());
        }
         FacesMessage msg = new FacesMessage(event.getFile().getFileName() + " Carga correcta.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

Crear lee.xhtml tal como puestra en la img































Resultados






No hay comentarios:

Publicar un comentario