PostulantesExcelExporter.java 4.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
package com.roshka.utils;


import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.roshka.DTO.PostulanteListaDTO;
import com.roshka.modelo.PostulanteTecnologia;
import com.roshka.modelo.Tecnologia;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class PostulantesExcelExporter {
    private XSSFWorkbook workbook;
    private XSSFSheet sheet;
    private List<PostulanteListaDTO> listUsers;
    private HashMap<String, String> filtros;

    public PostulantesExcelExporter(List<PostulanteListaDTO> listUsers, HashMap<String, String> filtros) {
        this.listUsers = listUsers;
        workbook = new XSSFWorkbook();
        this.filtros = filtros;
    }


    private void writeHeaderLine() {
        sheet = workbook.createSheet("Postulantes");
        DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
        String currentDateTime = dateFormatter.format(new Date());

        Row row = sheet.createRow(0);
        CellStyle style = workbook.createCellStyle();
        XSSFFont font = workbook.createFont();
        font.setBold(true);
        font.setFontHeight(14);
        style.setFont(font);
        createCell(row, 0, "Postulantes "+currentDateTime, style);

        row = sheet.createRow(2);
        createCell(row, 0, "Filtros", style);

        row = sheet.createRow(3);
        createCell(row, 0, "Nombre", style);
        createCell(row, 1, "Nivel de Ingles", style);
        createCell(row, 2, "Experiencia (Meses)", style);
        createCell(row, 3, "Tecnologias", style);
        createCell(row, 4, "Nivel Tecnologia", style);
        createCell(row, 5, "Institucion", style);
        createCell(row, 6, "Estado", style);
        createCell(row, 7, "Convocatoria", style);


        row = sheet.createRow(6);
        style = workbook.createCellStyle();
        font = workbook.createFont();
        font.setBold(true);
        font.setFontHeight(14);
        style.setFont(font);

        createCell(row, 0, "Nombre", style);
        createCell(row, 1, "Nivel de Ingles", style);
        createCell(row, 2, "Experiencia", style);
        createCell(row, 3, "Tecnologias", style);
        createCell(row, 4, "Estado", style);

        row = sheet.createRow(4);
        font.setBold(false);
        font.setFontHeight(12);
        style.setFont(font);
        createCell(row, 0, filtros.get("nombre"), style);
        createCell(row, 1, filtros.get("nivelIngles"), style);
        createCell(row, 2, filtros.get("experienciaEnMeses"), style);
        createCell(row, 3, filtros.get("tecnologia"), style);
        createCell(row, 4, filtros.get("nivelTecnologia"), style);
        createCell(row, 5, filtros.get("institucion"), style);
        createCell(row, 6, filtros.get("estado"), style);
        createCell(row, 7, filtros.get("convocatoria"), style);

    }

    private void createCell(Row row, int columnCount, Object value, CellStyle style) {
        sheet.autoSizeColumn(columnCount);
        Cell cell = row.createCell(columnCount);
        if (value instanceof Integer) {
            cell.setCellValue((Integer) value);
        } else if (value instanceof Boolean) {
            cell.setCellValue((Boolean) value);
        }else {
            cell.setCellValue((String) value);
        }
        cell.setCellStyle(style);
    }

    private void writeDataLines() {
        int rowCount = 7;

        CellStyle style = workbook.createCellStyle();
        XSSFFont font = workbook.createFont();
        font.setFontHeight(12);
        style.setFont(font);

        for (PostulanteListaDTO user : listUsers) {
            Row row = sheet.createRow(rowCount++);
            int columnCount = 0;

            StringBuilder tecno = new StringBuilder();
            for(PostulanteTecnologia tecnologia: user.getTecnologias()){
                tecno.append(tecnologia.getTecnologia().getNombre()).append(" ");
            }

            createCell(row, columnCount++, user.getNombre() + " " + user.getApellido(), style);
            createCell(row, columnCount++, user.getNivelIngles().intValue(), style);
            createCell(row, columnCount++, user.getExperienciaMeses().intValue(), style);
            createCell(row, columnCount++, tecno.toString(), style);
            createCell(row, columnCount++, user.getEstado().getEstado(), style);
            tecno.delete(0, tecno.length()-1);
        }
    }

    public void export(HttpServletResponse response) throws IOException {
        writeHeaderLine();
        writeDataLines();

        ServletOutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        workbook.close();

        outputStream.close();

    }
}