Mostrando entradas con la etiqueta poi. Mostrar todas las entradas
Mostrando entradas con la etiqueta poi. Mostrar todas las entradas

lunes, 30 de marzo de 2009

Creacion de Reportes en Excel con POI

bueno para la creación de un reporte en excel utilizaremos la api de apache POI http://poi.apache.org/ en este pequeño ejemplo documentado se podrá observar la creación de un reporte para 2 versiones de Excel.

Veamos la creación del reporte para el primer caso usando versiones de excel menores a la del 2007 utilizando POI HSSF:


package com.kaox.poi;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

import org.apache.poi.hssf.record.formula.functions.Cell;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

public class Ejemplo {
public static void main(String[] args)
{
try
{
//Creamos el libro Excel
HSSFWorkbook wb = new HSSFWorkbook();

//Creamos una nueva hoja
HSSFSheet sheet = wb.createSheet("Clientes");

//Se crea una fila dentro de la hoja
HSSFRow row = sheet.createRow(1);

//Se crea una celda
HSSFCell cell=null;

HSSFCellStyle cellStyle1 = wb.createCellStyle();
cellStyle1.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
cellStyle1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

HSSFCellStyle cellStyle2 = wb.createCellStyle();
cellStyle2.setFillForegroundColor(HSSFColor.WHITE.index);
cellStyle2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

//Creamos celdas de varios tipos
row.createCell(1).setCellValue("usersId");
row.createCell(2).setCellValue("rut");
row.createCell(3).setCellValue("passwordCreation");
row.createCell(4).setCellValue("status");
row.createCell(5).setCellValue("genero");
row.createCell(6).setCellValue("estadoCivil");
row.createCell(7).setCellValue("empresa");

for(int i=2;i<20;i++){

row = sheet.createRow(i);

//Creamos celdas con dos colores d fondos distintos

if(i%2==0){
cell =row.createCell(1);
cell.setCellValue(3502);
cell.setCellStyle(cellStyle1);

cell =row.createCell(2);
cell.setCellValue("01/09/2009");
cell.setCellStyle(cellStyle1);

cell =row.createCell(3);
cell.setCellValue("19/11/2007 05:31:58 p.m.");
cell.setCellStyle(cellStyle1);

cell =row.createCell(4);
cell.setCellValue(1);
cell.setCellStyle(cellStyle1);

cell =row.createCell(5);
cell.setCellValue("M");
cell.setCellStyle(cellStyle1);

cell =row.createCell(6);
cell.setCellValue("P");
cell.setCellStyle(cellStyle1);

cell =row.createCell(7);
cell.setCellValue("");
cell.setCellStyle(cellStyle1);
}else{
cell =row.createCell(1);
cell.setCellValue(3502);
cell.setCellStyle(cellStyle2);

cell =row.createCell(2);
cell.setCellValue("01/09/2009");
cell.setCellStyle(cellStyle2);

cell =row.createCell(3);
cell.setCellValue("19/11/2007 05:31:58 p.m.");
cell.setCellStyle(cellStyle2);

cell =row.createCell(4);
cell.setCellValue(1);
cell.setCellStyle(cellStyle2);

cell =row.createCell(5);
cell.setCellValue("M");
cell.setCellStyle(cellStyle2);

cell =row.createCell(6);
cell.setCellValue("P");
cell.setCellStyle(cellStyle2);

cell =row.createCell(7);
cell.setCellValue("");
cell.setCellStyle(cellStyle2);
}
}

//Escribimos los resultados a un fichero Excel
FileOutputStream fileOut = new FileOutputStream("C:/libro1.xls");

wb.write(fileOut);
fileOut.close();
}
catch(IOException e)
{
System.err.println("Error al escribir el archivo");
}

}

}


El resultado de ejecutar correr el ejemplo nos saldra algo asi: