Example usage for org.apache.poi.hssf.usermodel HSSFWorkbook create

List of usage examples for org.apache.poi.hssf.usermodel HSSFWorkbook create

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFWorkbook create.

Prototype

public static HSSFWorkbook create(InternalWorkbook book) 

Source Link

Usage

From source file:br.com.pontocontrol.controleponto.controller.impl.ExportadorXLSController.java

@Override
public boolean extrair(FolhaMensalPonto folhaMensal, String outputFileDir) {
    Calendar data = Calendar.getInstance();
    data.set(Calendar.MONTH, folhaMensal.getMes());
    data.set(Calendar.YEAR, folhaMensal.getAno());
    final String nomeArquivo = format("%s-%s_%s.xls",
            SessaoManager.getInstance().getUsuarioAutenticado().getLogin(),
            formatDate("yyyy-MMMM", data.getTime()), formatDate("yyyyMMddHHmmssSSS", new Date()));
    final String filePath = format("%s/%s", outputFileDir, nomeArquivo);
    File arquivoXLS = new File(filePath);
    FileOutputStream fos = null;//from ww  w . j a va 2s.  com
    try {
        arquivoXLS.createNewFile();

        fos = new FileOutputStream(arquivoXLS);

        HSSFWorkbook workbook = HSSFWorkbook.create(InternalWorkbook.createWorkbook());

        final String nomePlanilha = formatDate("MMM-yy", data.getTime());
        HSSFSheet planilha = workbook.createSheet(nomePlanilha);

        //TOTAL ROW
        HSSFRow totalRow = planilha.createRow(0);
        totalRow.createCell(CELL_INDEX.TOTAL_ROW.TOTAL_TXT).setCellValue("Total:");
        totalRow.createCell(CELL_INDEX.TOTAL_ROW.TOTAL_VALUE).setCellValue(folhaMensal.calcularTotalMensal());
        totalRow.createCell(CELL_INDEX.TOTAL_ROW.VAR_TXT).setCellValue("Variao:");
        totalRow.createCell(CELL_INDEX.TOTAL_ROW.VAR_VALUE).setCellValue(folhaMensal.calcularVariacaoMensal());

        //HEADER
        HSSFRow headerRow = planilha.createRow(1);
        headerRow.createCell(CELL_INDEX.DIA).setCellValue("Dia");
        headerRow.createCell(CELL_INDEX.ENTRADA).setCellValue("Entrada");
        headerRow.createCell(CELL_INDEX.ALMOCO).setCellValue("Almoo");
        headerRow.createCell(CELL_INDEX.RETORNO).setCellValue("Retorno");
        headerRow.createCell(CELL_INDEX.SAIDA).setCellValue("Sada");
        headerRow.createCell(CELL_INDEX.TOTAL_EXP).setCellValue("Expediente");
        headerRow.createCell(CELL_INDEX.VARIACAO).setCellValue("Variao");

        formatHeaderRow(workbook, headerRow);
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, folhaMensal.getAno());
        cal.set(Calendar.MONTH, folhaMensal.getMes());

        for (int dia = 1; dia <= cal.getActualMaximum(Calendar.DAY_OF_MONTH); dia++) {
            int i = planilha.getPhysicalNumberOfRows();
            HSSFRow row = planilha.createRow(i);
            cal.set(Calendar.DAY_OF_MONTH, dia);
            row.createCell(CELL_INDEX.DIA).setCellValue(formatDate("MMM dd, EEE", cal.getTime()));

            RegistroDiarioPonto reg = folhaMensal.getRegistros().get(dia);
            if (reg != null) {
                row.createCell(CELL_INDEX.ENTRADA)
                        .setCellValue(reg.getEntrada() != null ? reg.getEntrada().format(TIME_FORMATTER) : "");
                row.createCell(CELL_INDEX.ALMOCO)
                        .setCellValue(reg.getAlmoco() != null ? reg.getAlmoco().format(TIME_FORMATTER) : "");
                row.createCell(CELL_INDEX.RETORNO)
                        .setCellValue(reg.getRetorno() != null ? reg.getRetorno().format(TIME_FORMATTER) : "");
                row.createCell(CELL_INDEX.SAIDA)
                        .setCellValue(reg.getSaida() != null ? reg.getSaida().format(TIME_FORMATTER) : "");
                row.createCell(CELL_INDEX.TOTAL_EXP).setCellValue(
                        reg.isRegistroDiarioCompleto() ? reg.calcularTotalExpedienteAsNumber() : 0);
                row.createCell(CELL_INDEX.VARIACAO)
                        .setCellValue(reg.isRegistroDiarioCompleto() ? reg.calcularVariacaoExpediente() : 0);
            } else {
                row.createCell(CELL_INDEX.ENTRADA).setCellValue("");
                row.createCell(CELL_INDEX.ALMOCO).setCellValue("");
                row.createCell(CELL_INDEX.RETORNO).setCellValue("");
                row.createCell(CELL_INDEX.SAIDA).setCellValue("");
                row.createCell(CELL_INDEX.TOTAL_EXP).setCellValue("");
                row.createCell(CELL_INDEX.VARIACAO).setCellValue("");
            }
            if (SessaoManager.getInstance().getUsuarioAutenticado().checarSeDiaExpediente(cal)) {
                formatRow(workbook, row);
            } else {
                formatHeaderRow(workbook, row);
            }
        }
        for (int i = 0; i < headerRow.getPhysicalNumberOfCells(); i++) {
            planilha.autoSizeColumn(i);
        }
        workbook.write(fos);
        fos.flush();
        return true;
    } catch (IOException ex) {
        LOG.log(Level.SEVERE, "Erro ao criar arquivo XLS de sada", ex);
        return false;
    } finally {
        IOUtils.closeQuietly(fos);
    }
}