com.tecnosur.util.Excel.java Source code

Java tutorial

Introduction

Here is the source code for com.tecnosur.util.Excel.java

Source

/*
 * 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 com.tecnosur.util;

import com.tecnosur.coleccion.CControlmatricula;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

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

    public Excel() {
    }

    public void ExportarMatriculados(CControlmatricula datos, String aula) {
        try { // Defino el Libro de Excel
            HSSFWorkbook wb = new HSSFWorkbook();

            // Creo la Hoja en Excel
            Sheet sheet = wb.createSheet("matriculados");

            // quito las lineas del libro para darle un mejor acabado
            sheet.setDisplayGridlines(false);
            sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 6));

            // creo una nueva fila
            Row trow = sheet.createRow((short) 1);

            createTituloCell(wb, trow, 0, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER,
                    "ALUMNOS MATRICULADOS");

            Row trow2 = sheet.createRow((short) 3);
            createTituloCell(wb, trow2, 0, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_CENTER, "AULA: " + aula);

            // Creo la cabecera de mi listado en Excel
            Row row = sheet.createRow((short) 5);

            // Creo las celdas de mi fila, se puede poner un diseo a la celda_codigo
            createCell(wb, row, 0, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER, "CDIGO", true, true);
            createCell(wb, row, 1, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER, "ALUMNO", true, true);
            createCell(wb, row, 2, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER, "TIPO PAGO", true, true);
            createCell(wb, row, 3, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER, "CRONO. PAGO", true, true);
            createCell(wb, row, 4, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER, "N CUOT.", true, true);
            createCell(wb, row, 5, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER, "MON. MATR.", true, true);
            createCell(wb, row, 6, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER, "MON. CUOT.", true, true);

            // Definimos el tamao de las celdas, podemos definir un tamaa especifico o hacer que 
            //la celda_codigo se acomode segn su tamao
            Sheet ssheet = wb.getSheetAt(0);
            ssheet.setColumnWidth(0, 60 * 40);
            ssheet.setColumnWidth(1, 255 * 40);
            ssheet.setColumnWidth(2, 90 * 40);
            ssheet.autoSizeColumn(3);
            ssheet.autoSizeColumn(4);
            ssheet.autoSizeColumn(5);
            ssheet.autoSizeColumn(6);

            HSSFFont cellFont = wb.createFont();

            cellFont.setFontHeightInPoints((short) 8);
            cellFont.setFontName(HSSFFont.FONT_ARIAL);

            CellStyle cellStyle = wb.createCellStyle();

            cellStyle.setFont(cellFont);
            cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            cellStyle.setLeftBorderColor((short) 8);
            cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
            cellStyle.setRightBorderColor((short) 8);
            cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            cellStyle.setBottomBorderColor((short) 8);

            for (int i = 0; i < datos.size(); i++) {
                row = sheet.createRow((short) i + 6);
                Cell celda_codigo = row.createCell(0);

                celda_codigo.setCellStyle(cellStyle);
                celda_codigo.setCellValue(datos.get(i).getIdalumno());

                Cell celda_alumno = row.createCell(1);
                celda_alumno.setCellStyle(cellStyle);
                celda_alumno.setCellValue(datos.get(i).getNom_completo());

                Cell celda_tipopago = row.createCell(2);
                celda_tipopago.setCellStyle(cellStyle);
                celda_tipopago.setCellValue(datos.get(i).getTipopagante());

                Cell celda_crono = row.createCell(3);
                celda_crono.setCellStyle(cellStyle);
                celda_crono.setCellValue(datos.get(i).getDesc_cronopagtable());

                Cell celda_ncuota = row.createCell(4);
                celda_ncuota.setCellStyle(cellStyle);
                celda_ncuota.setCellValue(datos.get(i).getNum_cuota());

                Cell celda_matricula = row.createCell(5);
                celda_matricula.setCellStyle(cellStyle);
                celda_matricula.setCellValue(datos.get(i).getMonmatricula());

                Cell celda_moncuota = row.createCell(6);
                celda_moncuota.setCellStyle(cellStyle);
                celda_moncuota.setCellValue(datos.get(i).getMoncuota());
            }

            String strRuta = "TYSAC_Matriculados.xls";
            FileOutputStream fileOut = new FileOutputStream(strRuta);
            wb.write(fileOut);

            fileOut.close();
            Runtime.getRuntime().exec("cmd /c start " + strRuta);

        } catch (IOException e) {
            System.out.println("Error de escritura");
            e.printStackTrace();
        }
    }

    private static void createTituloCell(HSSFWorkbook wb, Row row, int column, short halign, short valign,
            String strContenido) {

        CreationHelper ch = wb.getCreationHelper();
        Cell cell = row.createCell(column);
        cell.setCellValue(ch.createRichTextString(strContenido));

        HSSFFont cellFont = wb.createFont();
        cellFont.setFontHeightInPoints((short) 11);
        cellFont.setFontName(HSSFFont.FONT_ARIAL);
        cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(halign);
        cellStyle.setVerticalAlignment(valign);
        cellStyle.setFont(cellFont);
        cell.setCellStyle(cellStyle);

    }

    private static void createCell(HSSFWorkbook wb, Row row, int column, short halign, short valign,
            String strContenido, boolean booBorde, boolean booCabecera) {
        CreationHelper ch = wb.getCreationHelper();
        Cell cell = row.createCell(column);
        HSSFFont cellFont = wb.createFont();
        cellFont.setFontHeightInPoints((short) 8);
        cellFont.setFontName(HSSFFont.FONT_ARIAL);
        cellFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        cell.setCellValue(ch.createRichTextString(strContenido));
        CellStyle cellStyle = wb.createCellStyle();
        cellStyle.setAlignment(halign);
        cellStyle.setVerticalAlignment(valign);
        cellStyle.setFont(cellFont);
        if (booBorde) {
            cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            cellStyle.setBottomBorderColor((short) 8);
            cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            cellStyle.setLeftBorderColor((short) 8);
            cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
            cellStyle.setRightBorderColor((short) 8);
            cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
            cellStyle.setTopBorderColor((short) 8);
        }
        if (booCabecera) {
            cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            cellStyle.setBottomBorderColor((short) 8);
            cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            cellStyle.setLeftBorderColor((short) 8);
            cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
            cellStyle.setRightBorderColor((short) 8);
            cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
            cellStyle.setTopBorderColor((short) 8);

            cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        }
        cell.setCellStyle(cellStyle);
    }
}