viernes, 31 de octubre de 2014

Clase en java para escribir en excel 2010

Depues de importar las librerias necesarias...
Esto le servira como una guia de lo que debe hacer

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package namemaestro;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author CLIENTE
 */
public class Excel {

    public static void readXLSX(String []valores) throws IOException {
//        Workbook wb = new XSSFWorkbook();
        Workbook archivo = new XSSFWorkbook(new FileInputStream("bastard.xlsx"));
//        String[] nombre = {"Armando", "Hernandez", "Mateu", "xlsx 2010"};
       
        Sheet sheet = archivo.getSheetAt(1);
//        Sheet sheet = archivo.createSheet("Mi hoja");
//        for (int i = 0; i < 10; i++) {
//            Row row = sheet.createRow((short) 6);
////            for (int j = 0; j < nombre.length; j++) {
//                Cell cell = row.createCell((short) 6);
//                cell.setCellValue("1100688005");
//                cell.getStringCellValue();
        //NUMERO DE SOLICITUD
        Row row = sheet.getRow(4);
        row.getCell(14).setCellValue(valores[0]);
       
        // fecha
        row.getCell(16).setCellValue(valores[1]);
       
        // hora
        row.getCell(18).setCellValue(valores[2]);
       
        //nombre del prestador
        Row row1 = sheet.getRow(5);
        row1.getCell(7).setCellValue(valores[3]);
       
        //Codigo
        Row row3 = sheet.getRow(6);
        row3.getCell(7).setCellValue(valores[4]);
       
        //Dirección del Prestador
        row3.getCell(15).setCellValue(valores[5]);
       
        //Telefono
        Row row4 = sheet.getRow(7);
        row4.getCell(1).setCellValue(valores[6]);
       
        //Departamento
        row4.getCell(4).setCellValue(valores[7]);
       
        //Municipio
        row4.getCell(7).setCellValue(valores[8]);
       
        //ENTIDAD A LA QUE SE SOLICITA AUTORIZACIÓN
        Row row5 = sheet.getRow(9);
        row5.getCell(14).setCellValue(valores[9]);
       
        //CODIGO:
        row5.getCell(18).setCellValue(valores[10]);
       
        //1er.  APELLIDO
        Row row6 = sheet.getRow(10);
        row6.getCell(0).setCellValue(valores[11]);
       
        //2er.  APELLIDO
        row6.getCell(1).setCellValue(valores[12]);
       
        //1er. NOMBRE
        row6.getCell(2).setCellValue(valores[13]);
       
        //2er. NOMBRE
        row6.getCell(3).setCellValue(valores[14]);
       
        //Tipo de Documento de Identificación
        if(valores[15].equals("Registro Civil")){
            Row row7 = sheet.getRow(15);
            row7.getCell(1).setCellValue("X");
        }
       
        if(valores[15].equals("Tarjeta de Identidad")){
            Row row7 = sheet.getRow(16);
            row7.getCell(1).setCellValue("X");
        }
       
        if(valores[15].equals("Cédula de Ciudadania")){
            Row row7 = sheet.getRow(17);
            row7.getCell(1).setCellValue("X");
        }
       
        if(valores[15].equals("Cédula de Extranjeria")){
            Row row7 = sheet.getRow(18);
            row7.getCell(1).setCellValue("X");
        }
       
        if(valores[15].equals("Pasaporte")){
            Row row7 = sheet.getRow(15);
            row7.getCell(11).setCellValue("X");
        }
               
        if(valores[15].equals("Adulto sin Identificar")){
            Row row7 = sheet.getRow(16);
            row7.getCell(11).setCellValue("X");
        }
       
        if(valores[15].equals("Menor sin Identificar")){
            Row row7 = sheet.getRow(17);
            row7.getCell(11).setCellValue("X");
        }
       
        //Número de Documento de Identificación
        Row row8 = sheet.getRow(17);
        row8.getCell(23).setCellValue(valores[16]);
       
        //Fecha de Nacimiento:
        Row row9 = sheet.getRow(19);
        row9.getCell(25).setCellValue(valores[17]);
       
        //Dirección de residencia Habitual:
        Row row10 = sheet.getRow(21);
        row10.getCell(10).setCellValue(valores[18]);
       
        //Teléfono:
        row10.getCell(15).setCellValue(valores[19]);
       
        //Departamento
        Row row11 = sheet.getRow(22);
        row11.getCell(5).setCellValue(valores[20]);
       
        //municipio
        row11.getCell(11).setCellValue(valores[21]);
       
        //Teléfono celular
        row11.getCell(19).setCellValue(valores[22]);
       
        //Correo electrónico
        Row row12 = sheet.getRow(23);
        row12.getCell(6).setCellValue(valores[23]);
       
        //Cobertura en Salud
        if(valores[24].equals("Regimen Contributivo")){
              Row row13 = sheet.getRow(25);
              row13.getCell(1).setCellValue("X");
        }
       
        if(valores[24].equals("Regimen Subsidiado-Parcial")){
              Row row13 = sheet.getRow(25);
              row13.getCell(10).setCellValue("X");
        }
       
        if(valores[24].equals("Población Pobre No Sisbenizada")){
              Row row13 = sheet.getRow(25);
              row13.getCell(20).setCellValue("X");
        }
       
        if(valores[24].equals("Planes Adicionales de salud")){
              Row row13 = sheet.getRow(25);
              row13.getCell(31).setCellValue("X");
        }
       
        if(valores[24].equals("Regimen Subsidiado-Total")){
              Row row13 = sheet.getRow(26);
              row13.getCell(1).setCellValue("X");
        }
     
        if(valores[24].equals("Población Pobre No Cubierta")){
              Row row13 = sheet.getRow(26);
              row13.getCell(10).setCellValue("X");
        }
       
        if(valores[24].equals("Desplazado")){
              Row row13 = sheet.getRow(26);
              row13.getCell(20).setCellValue("X");
        }
       
        if(valores[24].equals("OTRO.")){
              Row row13 = sheet.getRow(26);
              row13.getCell(31).setCellValue("X");
              row13.getCell(36).setCellValue(valores[25]);
        }
       
        //origen
        if(valores[27].equals("Enfermedad General")){
              Row row13 = sheet.getRow(30);
              row13.getCell(1).setCellValue("X");
        }
       
         if(valores[27].equals("Accidente de Trabajo")){
              Row row13 = sheet.getRow(30);
              row13.getCell(17).setCellValue("X");
         }
       
         if(valores[27].equals("Evento Catastrófico")){
              Row row13 = sheet.getRow(30);
              row13.getCell(29).setCellValue("X");
         }
       
         if(valores[27].equals("Enfermedad Profesional")){
              Row row13 = sheet.getRow(31);
              row13.getCell(1).setCellValue("X");
         }
       
         if(valores[27].equals("Accidente de Tránsito")){
              Row row13 = sheet.getRow(31);
              row13.getCell(17).setCellValue("X");
         }
       
         //Tipo de servicio
       
         if(valores[28].equals("Posterior a la atencion inicial de urgencias")){
              Row row13 = sheet.getRow(33);
              row13.getCell(1).setCellValue("X");
         }
       
         if(valores[28].equals("Servicios Electivos")){
              Row row13 = sheet.getRow(34);
              row13.getCell(1).setCellValue("X");
         }
       
         if(valores[29].equals("Prioridad de la Atencion")){
              Row row13 = sheet.getRow(33);
              row13.getCell(1).setCellValue("X");
         }
       
         if(valores[29].equals("Prioridad de la Atencion")){
              Row row13 = sheet.getRow(33);
              row13.getCell(1).setCellValue("X");
         }
       
       
       
//        for (Row fila : sheet) {
//            
//             if(i==6){
//                 for (int colum = 0; colum < 7; colum++) {
////                      Cell dato = fila.getCell(colum);
//                     Cell cell = fila.getCell(3);
//                    
////                     HSSFRichTextString miContenido= new HSSFRichTextString("¡¡¡Hola Mundo!!!");
//                     cell.setCellValue("1102578782");
////                     fila.set
////                      if (dato.getCellType() == Cell.CELL_TYPE_STRING) {
////                          System.out.println(dato.getStringCellValue() + " ");
////                      }
//            }
//          }
//             i++;
//         }
////        }
////        }
   
        FileOutputStream fos = new FileOutputStream("bastard.xlsx");
        archivo.write(fos);
        fos.flush();
        fos.close();
    }

    public static void leerXLSX() throws IOException {
        XSSFWorkbook archivo = new XSSFWorkbook(new FileInputStream("bastard.xlsx"));
        int numHoja = 0;
        XSSFSheet hoja = archivo.getSheetAt(numHoja);
        for (Row fila : hoja) {
            for (int colum = 0; colum < 4; colum++) {
                Cell dato = fila.getCell(colum);
                if (dato.getCellType() == Cell.CELL_TYPE_STRING) {
                    System.out.print(dato.getStringCellValue() + "");
                }
            }
            System.out.println();
        }
    }

    public static void main(String[] args) throws IOException {
        System.out.println("Leer archivo xlsx");
//        leerXLSX();
        //leerXLS();
        System.out.println("\nLeer archivo xls");
//        readXLSX();
        System.out.println("\nLeer archivo xls");
    }

    private static void leerXLS() throws IOException {
        Workbook archivo = new HSSFWorkbook(new FileInputStream("bastard.xls"));
        int numHoja = 0;
        Sheet hoja = archivo.getSheetAt(numHoja);
        for (Row fila : hoja) {
            for (int colum = 0; colum < 3; colum++) {
                Cell dato = fila.getCell(colum);
                if (dato.getCellType() == Cell.CELL_TYPE_STRING) {
                    System.out.println(dato.getStringCellValue() + " ");
                }
            }
            System.out.println();
        }
    }

}

No hay comentarios:

Publicar un comentario