Example usage for org.apache.poi.hssf.usermodel HSSFSheet setColumnWidth

List of usage examples for org.apache.poi.hssf.usermodel HSSFSheet setColumnWidth

Introduction

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

Prototype

@Override
public void setColumnWidth(int columnIndex, int width) 

Source Link

Document

Set the width (in units of 1/256th of a character width)

The maximum column width for an individual cell is 255 characters.

Usage

From source file:HSSFReadWrite.java

License:Apache License

/**
 * given a filename this outputs a sample sheet with just a set of
 * rows/cells.//  w  w w  .  j a v  a  2s  .c  o m
 */
private static void testCreateSampleSheet(String outputFilename) throws IOException {
    int rownum;
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet s = wb.createSheet();
    HSSFCellStyle cs = wb.createCellStyle();
    HSSFCellStyle cs2 = wb.createCellStyle();
    HSSFCellStyle cs3 = wb.createCellStyle();
    HSSFFont f = wb.createFont();
    HSSFFont f2 = wb.createFont();

    f.setFontHeightInPoints((short) 12);
    f.setColor((short) 0xA);
    f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    f2.setFontHeightInPoints((short) 10);
    f2.setColor((short) 0xf);
    f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
    cs.setFont(f);
    cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
    cs2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    cs2.setFillPattern((short) 1); // fill w fg
    cs2.setFillForegroundColor((short) 0xA);
    cs2.setFont(f2);
    wb.setSheetName(0, "HSSF Test");
    for (rownum = 0; rownum < 300; rownum++) {
        HSSFRow r = s.createRow(rownum);
        if ((rownum % 2) == 0) {
            r.setHeight((short) 0x249);
        }

        for (int cellnum = 0; cellnum < 50; cellnum += 2) {
            HSSFCell c = r.createCell(cellnum);
            c.setCellValue(rownum * 10000 + cellnum + (((double) rownum / 1000) + ((double) cellnum / 10000)));
            if ((rownum % 2) == 0) {
                c.setCellStyle(cs);
            }
            c = r.createCell(cellnum + 1);
            c.setCellValue(new HSSFRichTextString("TEST"));
            // 50 characters divided by 1/20th of a point
            s.setColumnWidth(cellnum + 1, (int) (50 * 8 / 0.05));
            if ((rownum % 2) == 0) {
                c.setCellStyle(cs2);
            }
        }
    }

    // draw a thick black border on the row at the bottom using BLANKS
    rownum++;
    rownum++;
    HSSFRow r = s.createRow(rownum);
    cs3.setBorderBottom(HSSFCellStyle.BORDER_THICK);
    for (int cellnum = 0; cellnum < 50; cellnum++) {
        HSSFCell c = r.createCell(cellnum);
        c.setCellStyle(cs3);
    }
    s.addMergedRegion(new CellRangeAddress(0, 3, 0, 3));
    s.addMergedRegion(new CellRangeAddress(100, 110, 100, 110));

    // end draw thick black border
    // create a sheet, set its title then delete it
    s = wb.createSheet();
    wb.setSheetName(1, "DeletedSheet");
    wb.removeSheetAt(1);

    // end deleted sheet
    FileOutputStream out = new FileOutputStream(outputFilename);
    wb.write(out);
    out.close();
}

From source file:attandance.standalone.manager.AttandanceManager.java

private void outputAttandance(List<LateRecord> lates, List<AbsenceRecord> absences) {
    // webbookExcel  
    HSSFWorkbook wb = new HSSFWorkbook();
    // webbooksheet,Excelsheet  
    HSSFSheet sheet = wb.createSheet("");
    int width = ((int) (20 * 1.14388)) * 256;
    sheet.setColumnWidth(0, width);
    sheet.setColumnWidth(1, width);//from w ww . j  av  a 2  s  . c  om
    sheet.setColumnWidth(2, width);
    sheet.setColumnWidth(3, width);
    // sheet0,??poiExcel?short  
    HSSFRow row = sheet.createRow((int) 0);
    // ?   
    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ?  

    HSSFCell cell = row.createCell((short) 0);
    cell.setCellValue("??");
    cell.setCellStyle(style);
    cell = row.createCell((short) 1);
    cell.setCellValue("");
    cell.setCellStyle(style);
    cell = row.createCell((short) 2);
    cell.setCellValue("?");
    cell.setCellStyle(style);
    cell = row.createCell((short) 3);
    cell.setCellValue("?");
    cell.setCellStyle(style);

    // ? ??   
    int i = 0;
    for (; i < lates.size(); i++) {
        row = sheet.createRow((int) i + 1);
        LateRecord lateStaff = lates.get(i);
        // ?  
        row.createCell((short) 0).setCellValue(lateStaff.getStaffName());
        row.createCell((short) 1).setCellValue(lateStaff.getCaculateDateString());
        cell = row.createCell((short) 2);
        cell.setCellValue(new SimpleDateFormat(DateHelper.DATE_FORMAT).format(lateStaff.getLateDate()));
        row.createCell((short) 3).setCellValue(lateStaff.getLateTimeDesc());
    }
    for (int j = 0; j < absences.size(); j++) {
        row = sheet.createRow((int) i + j + 1);
        AbsenceRecord absenceStaff = absences.get(j);
        // ?  
        row.createCell((short) 0).setCellValue(absenceStaff.getStaffName());
        row.createCell((short) 1).setCellValue(absenceStaff.getCaculateDateString());
        cell = row.createCell((short) 2);
        cell.setCellValue(
                new SimpleDateFormat(DateHelper.ONLY_DATE_FORMAT).format(absenceStaff.getAbsenceDate()));
        row.createCell((short) 3).setCellValue("?");
    }
    // ?  
    try {
        String fileName = "C:/xhuxing-private/" + new Date(System.currentTimeMillis()).getMonth()
                + ".xls";
        FileOutputStream fout = new FileOutputStream(fileName);
        wb.write(fout);
        fout.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:br.edu.tglima.model.result.PlanilhaXLS.java

License:Open Source License

/**
 * Mtodo responsvel por gerar a planilha XLS.
 * //from w ww . j  ava 2s . com
 * 
 * @param arquivo Referente ao local e nome do arquivo.
 * @return Retorno do tipo Boolean, indicando se o arquivo foi gerado
 * com sucesso, ou no.
 */
public boolean gerarPlanilha(File arquivo) {

    try {

        /*Verificamos se j existe um arquivo com esse "nome".
        Caso ele exista, ele ser removido e o novo arquivo ser gerado.*/
        if (workbook.getNumberOfSheets() > 0) {
            workbook.removeSheetAt(0);
        }

        HSSFSheet sheet = workbook.createSheet("Valores Exportados");

        /*         Criando as linhas. --------------------------------------------- */

        HSSFRow header1 = sheet.createRow((short) 0);
        HSSFRow linha02 = sheet.createRow((short) 1);
        HSSFRow linha03 = sheet.createRow((short) 2);
        HSSFRow linha04 = sheet.createRow((short) 3);
        HSSFRow linha05 = sheet.createRow((short) 4);

        HSSFRow header2 = sheet.createRow((short) 7);
        HSSFRow linha09 = sheet.createRow((short) 8);
        HSSFRow linha10 = sheet.createRow((short) 9);
        HSSFRow linha11 = sheet.createRow((short) 10);
        HSSFRow linha12 = sheet.createRow((short) 11);
        HSSFRow linha13 = sheet.createRow((short) 12);
        HSSFRow linha14 = sheet.createRow((short) 13);
        HSSFRow linha15 = sheet.createRow((short) 14);
        HSSFRow linha17 = sheet.createRow((short) 16);

        HSSFRow header3 = sheet.createRow((short) 19);
        HSSFRow linha21 = sheet.createRow((short) 20);
        HSSFRow linha22 = sheet.createRow((short) 21);
        HSSFRow linha23 = sheet.createRow((short) 22);
        HSSFRow linha25 = sheet.createRow((short) 24);

        //  ------------------------------------------------------------------------ //

        /*         Mesclando as clulas. ------------------------------------------ */

        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
        sheet.addMergedRegion(new CellRangeAddress(7, 7, 0, 2));
        sheet.addMergedRegion(new CellRangeAddress(19, 19, 0, 2));

        //  ------------------------------------------------------------------------ //

        /*         Definindo a largura das colunas. ------------------------------- */

        sheet.setColumnWidth(0, 12000);
        sheet.setColumnWidth(1, 5200);
        sheet.setColumnWidth(2, 6500);

        //  ------------------------------------------------------------------------ //

        /*         Definindo os estilos das clulas. ------------------------------ */

        CellStyle headerStyle = workbook.createCellStyle();
        Font headerFont = workbook.createFont();
        headerFont.setFontHeightInPoints((short) 20);
        headerStyle.setFont(headerFont);
        headerStyle.setAlignment(HorizontalAlignment.CENTER);

        CellStyle cellCentered = workbook.createCellStyle();
        cellCentered.setAlignment(HorizontalAlignment.CENTER);

        CellStyle cellFontBold = workbook.createCellStyle();
        Font fontBold = workbook.createFont();
        fontBold.setBold(true);
        cellFontBold.setFont(fontBold);

        CellStyle cellResulted = workbook.createCellStyle();
        Font resultFont = workbook.createFont();
        resultFont.setBold(true);
        cellResulted.setFont(resultFont);
        cellResulted.setAlignment(HorizontalAlignment.CENTER);

        Cell cell;

        //  ------------------------------------------------------------------------ //

        /*         Criando as Colunas da Tabela ----------------------------------- */

        //Colunas da linha 1
        header1.createCell(0).setCellValue("Dados Fornecidos");

        //Colunas da linha 2
        linha02.createCell(0).setCellValue("Data de Entrada");
        linha02.createCell(2).setCellValue(this.rst.getDataEntrada());

        //Colunas da Linha 3
        linha03.createCell(0).setCellValue("Data de Sada");
        linha03.createCell(2).setCellValue(this.rst.getDataSaida());

        //Colunas da Linha 4
        linha04.createCell(0).setCellValue("Salrio Informado");
        linha04.createCell(2).setCellValue(this.rst.getSalario());

        //Colunas da Linha 5
        linha05.createCell(0).setCellValue("Motivo da Sada");
        linha05.createCell(2).setCellValue(this.rst.getMotivoRes());

        //Colunas da linha 8
        header2.createCell(0).setCellValue("Resciso");

        //Colunas da linha 9
        linha09.createCell(0).setCellValue("Item");
        linha09.createCell(1).setCellValue("Referncia");
        linha09.createCell(2).setCellValue("Valor");

        //Colunas da Linha 10
        linha10.createCell(0).setCellValue("Saldo Salrio");
        linha10.createCell(1).setCellValue(this.rst.getTotDiasTrabUltMes());
        linha10.createCell(2).setCellValue(this.rst.getUltSalario());

        //Colunas da Linha 11
        linha11.createCell(0).setCellValue("13 Proporcional");
        linha11.createCell(1).setCellValue(this.rst.getTotMesesTrabUltAno());
        linha11.createCell(2).setCellValue(this.rst.getVlrDecimo());

        //Colunas da Linha 12
        linha12.createCell(0).setCellValue("Frias Proporcional");
        linha12.createCell(1).setCellValue(this.rst.getTotMesesAqFerias());
        linha12.createCell(2).setCellValue(this.rst.getVlrFerias());

        //Colunas da Linha 13
        linha13.createCell(0).setCellValue("1/3 Frias Proporcional");
        linha13.createCell(1).setCellValue("-");
        linha13.createCell(2).setCellValue(this.rst.getVlrTercoFerias());

        //Colunas da Linha 14
        linha14.createCell(0).setCellValue("Frias Vencidas");
        linha14.createCell(1).setCellValue(this.rst.getTotFeriasVenc());
        linha14.createCell(2).setCellValue(this.rst.getVlrFeriasVenc());

        //Colunas da linha 15
        linha15.createCell(0).setCellValue("Aviso Prvio");
        linha15.createCell(1).setCellValue(this.rst.getTotDiasAviso());
        linha15.createCell(2).setCellValue(this.rst.getVlrAvisoP());

        //Colunas da linha 17
        linha17.createCell(0).setCellValue("Valor Total");
        linha17.createCell(1).setCellValue("-");
        linha17.createCell(2).setCellValue(this.rst.getVlrTotVenc());

        //Colunas da Linha 20
        header3.createCell(0).setCellValue("FGTS");

        //Colunas da Linha 21
        linha21.createCell(0).setCellValue("Valores do FGTS estaro disponveis para saque?");
        linha21.createCell(2).setCellValue(this.rst.getReceberFgts());

        //Colunas da Linha 22
        linha22.createCell(0).setCellValue("Saldo FGTS");
        linha22.createCell(2).setCellValue(this.rst.getSaldoFgts());

        //Colunas da Linha 23
        linha23.createCell(0).setCellValue("Multa de 40%");
        linha23.createCell(2).setCellValue(this.rst.getVlrMulta());

        //Colunas da Linha 25
        linha25.createCell(0).setCellValue("Valor total");
        linha25.createCell(2).setCellValue(this.rst.getVlrTotFgts());

        //  ------------------------------------------------------------------------ //  

        /*         Aplicando os estilos nas clulas ------------------------------- */

        cell = header1.getCell(0);
        cell.setCellStyle(headerStyle);
        header1.setRowStyle(headerStyle);
        header1.setHeightInPoints(30);

        cell = header2.getCell(0);
        cell.setCellStyle(headerStyle);
        header2.setRowStyle(headerStyle);
        header2.setHeightInPoints(30);

        cell = header3.getCell(0);
        cell.setCellStyle(headerStyle);
        header3.setRowStyle(headerStyle);
        header3.setHeightInPoints(30);

        linha02.getCell(2).setCellStyle(cellCentered);

        linha03.getCell(2).setCellStyle(cellCentered);

        linha04.getCell(2).setCellStyle(cellCentered);

        linha05.getCell(2).setCellStyle(cellCentered);

        linha09.getCell(1).setCellStyle(cellCentered);
        linha09.getCell(2).setCellStyle(cellCentered);

        linha10.getCell(1).setCellStyle(cellCentered);
        linha10.getCell(2).setCellStyle(cellCentered);

        linha11.getCell(1).setCellStyle(cellCentered);
        linha11.getCell(2).setCellStyle(cellCentered);

        linha12.getCell(1).setCellStyle(cellCentered);
        linha12.getCell(2).setCellStyle(cellCentered);

        linha13.getCell(1).setCellStyle(cellCentered);
        linha13.getCell(2).setCellStyle(cellCentered);

        linha14.getCell(1).setCellStyle(cellCentered);
        linha14.getCell(2).setCellStyle(cellCentered);

        linha15.getCell(1).setCellStyle(cellCentered);
        linha15.getCell(2).setCellStyle(cellCentered);

        linha17.getCell(0).setCellStyle(cellFontBold);
        linha17.getCell(1).setCellStyle(cellCentered);
        linha17.getCell(2).setCellStyle(cellResulted);

        linha21.getCell(2).setCellStyle(cellCentered);

        linha22.getCell(2).setCellStyle(cellCentered);

        linha23.getCell(2).setCellStyle(cellCentered);

        linha25.getCell(0).setCellStyle(cellFontBold);
        linha25.getCell(2).setCellStyle(cellResulted);

        //  ------------------------------------------------------------------------ //           

        /*         Escrever, salvar e fechar o arquivo ---------------------------- */

        workbook.write(arquivo); // Escrevendo no arquivo.

        workbook.close(); // Salvando e fechando o arquivo.

        return true;

    } catch (Exception e) {

        System.err.println("No foi possvel gerar seu arquivo!" + " \n" + e.getLocalizedMessage());

        return false;

    }

}

From source file:br.ufpa.psi.comportamente.labgame.mbeans.RelatoriosMB.java

License:Open Source License

public StreamedContent geraRelatorioJogadasExperimento()
        throws ParsePropertyException, IOException, InvalidFormatException {
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet("Relatrio das Jogadas");

    sheet.setColumnWidth(sheet.getFirstRowNum(), (14 * 256) + 200);
    sheet.setColumnWidth(1, (14 * 256) + 200);
    sheet.setColumnWidth(4, (17 * 256) + 200);
    sheet.setColumnWidth(5, (16 * 256) + 200);

    HSSFCellStyle cs1 = workbook.createCellStyle();
    cs1.setDataFormat(HSSFDataFormat.getBuiltinFormat("d-mmm-yy"));

    HSSFCellStyle cs2 = workbook.createCellStyle();
    cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("h:mm:ss"));

    JogadaDAO jogadaDAO = new JogadaDAO();
    jogadaDAO.beginTransaction();/*from  w  w  w.  j ava 2  s  .  com*/
    List<Jogada> jogadas = jogadaDAO.encontrarPorExperimento(experimentoSelecionado.getId());

    int countRow = 0;
    Row row1 = sheet.createRow(countRow++);
    Cell cell = row1.createCell(0);
    cell.setCellValue("Experimento: " + experimentoSelecionado.getNome());

    Row row = sheet.createRow(countRow++);

    row.createCell(0).setCellValue("Data da Jogada");
    row.createCell(1).setCellValue("Hora da Jogada");
    row.createCell(2).setCellValue("Coluna");
    row.createCell(3).setCellValue("Linha");
    row.createCell(4).setCellValue("Pontuacao Individual");
    row.createCell(5).setCellValue("Pontuacao Coletiva");
    row.createCell(6).setCellValue("Participante");
    row.createCell(7).setCellValue("Condio");

    for (Jogada jogada : jogadas) {

        Row nrow = sheet.createRow(countRow++);

        //Data
        Cell ncell0 = nrow.createCell(0);
        ncell0.setCellValue(jogada.getMomento());
        ncell0.setCellStyle(cs1);

        //Hora
        Cell ncell1 = nrow.createCell(1);
        ncell1.setCellValue(jogada.getMomento());
        ncell1.setCellStyle(cs2);

        //Coluna
        Cell ncell2 = nrow.createCell(2);
        ncell2.setCellValue(jogada.getColunaSelecionada());

        //Linha
        Cell ncell3 = nrow.createCell(3);
        ncell3.setCellValue(jogada.getLinhaSelecionada());

        //Pontuao Individual
        Cell ncell4 = nrow.createCell(4);
        ncell4.setCellValue(jogada.getPontuacaoIndividual());

        //Pontuao Coletiva
        Cell ncell5 = nrow.createCell(5);
        ncell5.setCellValue(jogada.getPontuacaoCultural());

        //Jogador
        Cell ncell6 = nrow.createCell(6);
        ncell6.setCellValue(jogada.getJogador().getNome());

        //Id da Condio
        Cell ncell7 = nrow.createCell(7);
        ncell7.setCellValue(jogada.getIdCondicao());

    }

    jogadaDAO.stopOperation(false);

    byte[] bytes;
    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        workbook.write(out);
        bytes = out.toByteArray();
    }

    InputStream ioStream = new ByteArrayInputStream(bytes);
    file = new DefaultStreamedContent(ioStream, "application/vnd.ms-excel", "Relatrio_Jogadas.xls");
    return file;
}

From source file:Clavis.Windows.WShedule.java

public void createXLSDocument(String nome, String[][] valores) {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet(nome);
    HSSFRow row;//from www .j  ava2 s .c  o  m
    HSSFCell cell;
    for (int i = 0; i < valores.length; i++) {
        row = sheet.createRow(i);
        for (int j = 0; j < valores[i].length; j++) {
            cell = row.createCell(j);
            cell.setCellValue(valores[i][j]);
            if (valores[i][j] != null) {
                sheet.setColumnWidth(j, 255 * valores[i][j].length() + 4);
                CellUtil.setAlignment(cell, wb, CellStyle.ALIGN_CENTER);
            }
        }
    }
    wb.setPrintArea(0, 0, valores[0].length, 0, valores.length);
    sheet.getPrintSetup().setPaperSize(XSSFPrintSetup.A4_PAPERSIZE);
    FileOutputStream out;
    String sfile = new File("").getAbsolutePath() + System.getProperty("file.separator") + nome;
    File file = new File(sfile);
    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException ex) {
            Logger.getLogger(WShedule.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    if (file.canWrite()) {
        try {
            out = new FileOutputStream(file);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(WShedule.class.getName()).log(Level.SEVERE, null, ex);
            out = null;
        }
        if (out != null) {
            try {
                wb.write(out);
                out.close();
                if (Desktop.isDesktopSupported()) {
                    Desktop.getDesktop().open(file);
                } else {
                    Components.MessagePane mensagem = new Components.MessagePane(this,
                            Components.MessagePane.INFORMACAO, painelcor, lingua.translate("Nota"), 400, 200,
                            lingua.translate("O documento \"doc.xls\" foi criado na pasta raiz do programa")
                                    + ".",
                            new String[] { lingua.translate("Voltar") });
                    mensagem.showMessage();
                }
            } catch (IOException ex) {
                Logger.getLogger(WShedule.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

From source file:cn.mario256.blog.ExcelView.java

License:Open Source License

/**
 * ?Excel/*from   w  w  w . j  a va 2 s  .c  o  m*/
 * 
 * @param model
 *            ?
 * @param workbook
 *            HSSFWorkbook
 * @param request
 *            HttpServletRequest
 * @param response
 *            HttpServletResponse
 */
public void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    Assert.notEmpty(properties);

    HSSFSheet sheet;
    if (StringUtils.isNotEmpty(sheetName)) {
        sheet = workbook.createSheet(sheetName);
    } else {
        sheet = workbook.createSheet();
    }
    int rowNumber = 0;
    if (titles != null && titles.length > 0) {
        HSSFRow header = sheet.createRow(rowNumber);
        header.setHeight((short) 400);
        for (int i = 0; i < properties.length; i++) {
            HSSFCell cell = header.createCell(i);
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            HSSFFont font = workbook.createFont();
            font.setFontHeightInPoints((short) 11);
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);
            if (i == 0) {
                HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
                HSSFComment comment = patriarch
                        .createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 4, 4));
                comment.setString(new HSSFRichTextString("P" + "o" + "w" + "e" + "r" + "e" + "d" + " " + "B"
                        + "y" + " " + "S" + "H" + "O" + "P" + "+" + "+"));
                cell.setCellComment(comment);
            }
            if (titles.length > i && titles[i] != null) {
                cell.setCellValue(titles[i]);
            } else {
                cell.setCellValue(properties[i]);
            }
            if (widths != null && widths.length > i && widths[i] != null) {
                sheet.setColumnWidth(i, widths[i]);
            } else {
                sheet.autoSizeColumn(i);
            }
        }
        rowNumber++;
    }
    if (data != null) {
        for (Object item : data) {
            HSSFRow row = sheet.createRow(rowNumber);
            for (int i = 0; i < properties.length; i++) {
                HSSFCell cell = row.createCell(i);
                if (converters != null && converters.length > i && converters[i] != null) {
                    Class<?> clazz = PropertyUtils.getPropertyType(item, properties[i]);
                    ConvertUtils.register(converters[i], clazz);
                    cell.setCellValue(BeanUtils.getProperty(item, properties[i]));
                    ConvertUtils.deregister(clazz);
                    if (clazz.equals(Date.class)) {
                        DateConverter dateConverter = new DateConverter();
                        dateConverter.setPattern(DEFAULT_DATE_PATTERN);
                        ConvertUtils.register(dateConverter, Date.class);
                    }
                } else {
                    cell.setCellValue(BeanUtils.getProperty(item, properties[i]));
                }
                if (rowNumber == 0 || rowNumber == 1) {
                    if (widths != null && widths.length > i && widths[i] != null) {
                        sheet.setColumnWidth(i, widths[i]);
                    } else {
                        sheet.autoSizeColumn(i);
                    }
                }
            }
            rowNumber++;
        }
    }
    if (contents != null && contents.length > 0) {
        rowNumber++;
        for (String content : contents) {
            HSSFRow row = sheet.createRow(rowNumber);
            HSSFCell cell = row.createCell(0);
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            HSSFFont font = workbook.createFont();
            font.setColor(HSSFColor.GREY_50_PERCENT.index);
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);
            cell.setCellValue(content);
            rowNumber++;
        }
    }
    response.setContentType("application/force-download");
    if (StringUtils.isNotEmpty(filename)) {
        response.setHeader("Content-disposition",
                "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
    } else {
        response.setHeader("Content-disposition", "attachment");
    }
}

From source file:com.ah.ui.actions.admin.LicenseMgrAction.java

License:Open Source License

private void exportEntitleKeyInfo() {
    try {//from ww  w.java  2  s .co  m
        // create a new file
        FileOutputStream out = new FileOutputStream(ORDERKEYINFO_EXPORT_FILE_PATH);
        // create a new workbook
        HSSFWorkbook wb = new HSSFWorkbook();
        // create a new sheet
        HSSFSheet s = wb.createSheet("Sheet1");
        // declare a row object reference
        HSSFRow r = null;
        // declare a cell object reference
        HSSFCell c = null;
        // row index
        int rowNum = 0;
        // cell count
        int cellcount = 0;
        if (NmsUtil.isHMForOEM()) {
            cellcount = 7;
        } else if (getIsInHomeDomain()) {
            cellcount = 8;
        } else if (NmsUtil.isHostedHMApplication()) {
            cellcount = 9;
        }
        if (cellcount == 0) {
            return;
        } else {
            for (int i = 0; i < cellcount; i++) {
                s.setColumnWidth(i, getColumnWidthByIndex(i) * 256);
            }
        }
        // create cell style
        HSSFCellStyle cs = wb.createCellStyle();
        // create font object
        HSSFFont f = wb.createFont();

        //set font to 12 point type
        f.setFontHeightInPoints((short) 12);
        f.setFontName("Calibri");

        //set cell stlye
        cs.setFont(f);
        cs.setAlignment(CellStyle.ALIGN_CENTER);
        cs.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);

        // create cell style
        HSSFCellStyle cs1 = wb.createCellStyle();
        // create font object
        HSSFFont f1 = wb.createFont();

        //set font to 12 point type
        f1.setFontHeightInPoints((short) 12);
        f1.setFontName("Calibri");
        f1.setBoldweight(Font.BOLDWEIGHT_BOLD);

        //set cell stlye
        cs1.setFont(f1);
        cs1.setAlignment(CellStyle.ALIGN_RIGHT);
        cs1.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);

        // create cell style
        HSSFCellStyle cs2 = wb.createCellStyle();
        cs2.setFont(f1);
        cs2.setAlignment(CellStyle.ALIGN_CENTER);
        cs2.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);

        // create cell style
        HSSFCellStyle cs3 = wb.createCellStyle();
        cs3.setFont(f);
        cs3.setAlignment(CellStyle.ALIGN_RIGHT);
        cs3.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);

        // create cell style
        HSSFCellStyle cs4 = wb.createCellStyle();
        cs4.setFont(f1);
        cs4.setAlignment(CellStyle.ALIGN_LEFT);
        cs4.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);

        // create cell style
        HSSFCellStyle cs5 = wb.createCellStyle();
        // create font object
        HSSFFont f2 = wb.createFont();

        //set font to 12 point type
        f2.setFontHeightInPoints((short) 12);
        f2.setFontName("Calibri");
        f2.setBoldweight(Font.BOLDWEIGHT_BOLD);
        f2.setColor(Font.COLOR_RED);
        cs5.setFont(f2);
        cs5.setAlignment(CellStyle.ALIGN_LEFT);
        cs5.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);

        // create cell style
        HSSFCellStyle cs6 = wb.createCellStyle();
        cs6.setFont(f);
        cs6.setAlignment(CellStyle.ALIGN_LEFT);
        cs6.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);

        // create a row
        // row 1
        s.addMergedRegion(new CellRangeAddress(0, 0, 0, cellcount - 1));
        r = s.createRow(rowNum++);
        c = r.createCell(0);
        c.setCellValue(MgrUtil.getUserMessage("admin.license.orderkey.export.title"));
        c.setCellStyle(cs);

        // row 2
        String sysInfo = "";
        if (getIsInHomeDomain()) {
            sysInfo = "System ID: " + getSystemId();
            // HiveManager Online user
        } else if (NmsUtil.isHostedHMApplication()) {
            sysInfo = "VHM ID: " + getDomain().getVhmID();
        }
        s.addMergedRegion(new CellRangeAddress(1, 1, 0, cellcount - 1));
        r = s.createRow(rowNum++);
        c = r.createCell(0);
        c.setCellValue(sysInfo);
        c.setCellStyle(cs);

        // row 3
        s.addMergedRegion(new CellRangeAddress(2, 2, 0, cellcount - 1));
        r = s.createRow(rowNum++);
        c = r.createCell(0);
        List<?> userInfo = QueryUtil.executeQuery(
                "SELECT email, company FROM " + UserRegInfoForLs.class.getSimpleName(), null,
                new FilterParams("owner.domainName", getDomain().getDomainName()));
        if (!userInfo.isEmpty()) {
            Object[] userInfos = (Object[]) userInfo.get(0);
            c.setCellValue(MgrUtil.getUserMessage("admin.license.orderkey.export.email.company",
                    new String[] { (String) userInfos[0], (String) userInfos[1] }));
        }
        c.setCellStyle(cs);

        // row 4
        r = s.createRow(rowNum++);

        // row 5
        r = s.createRow(rowNum++);

        // row 6 cell 1
        c = r.createCell(0);
        c.setCellValue(MgrUtil.getUserMessage("admin.license.orderkey.export.device.licensed"));
        c.setCellStyle(cs1);

        // row 6 cell 2
        c = r.createCell(1);
        c.setCellValue(licenseInfo.getHiveAps());
        c.setCellStyle(cs2);

        // device management info
        Map<String, Integer> apcount = HiveAPInfoFromeDatabase
                .getManagedDeviceTypeAndNumber(getDomain().getDomainName());
        int vpnCount = 0;
        int totalCount = 0;
        if (null != apcount) {
            Object[] typeNames = apcount.keySet().toArray();
            Arrays.sort(typeNames);
            for (Object typeName : typeNames) {

                // VPN Gateway VA does not belong to device
                if (MgrUtil.getEnumString("enum.hiveAp.model.10").equals(typeName)) {
                    vpnCount = apcount.get(typeName);
                } else {
                    r = s.createRow(rowNum++);
                    c = r.createCell(0);
                    c.setCellValue((String) typeName);
                    c.setCellStyle(cs3);

                    c = r.createCell(1);
                    c.setCellValue(apcount.get(typeName));
                    c.setCellStyle(cs);
                    totalCount += apcount.get(typeName);
                }
            }

        }

        // managed device total number cell1
        r = s.createRow(rowNum++);
        c = r.createCell(0);
        c.setCellValue(MgrUtil.getUserMessage("admin.license.orderkey.export.device.total"));
        c.setCellStyle(cs1);

        // managed device total number cell2
        c = r.createCell(1);
        c.setCellValue(totalCount);
        c.setCellStyle(cs2);

        // blank row
        r = s.createRow(rowNum++);

        // licensed VPN Gateway VA cell1
        r = s.createRow(rowNum++);
        c = r.createCell(0);
        c.setCellValue(MgrUtil.getUserMessage("admin.license.orderkey.export.cvg.licensed"));
        c.setCellStyle(cs1);

        // licensed VPN Gateway VA cell2
        c = r.createCell(1);
        c.setCellValue(licenseInfo.getCvgNumber());
        c.setCellStyle(cs2);

        // managed VPN Gateway VA cell1
        r = s.createRow(rowNum++);
        c = r.createCell(0);
        c.setCellValue(MgrUtil.getUserMessage("admin.license.orderkey.export.cvg.total"));
        c.setCellStyle(cs1);

        // managed VPN Gateway VA cell 2
        c = r.createCell(1);
        c.setCellValue(vpnCount);
        c.setCellStyle(cs2);

        // entitlement key information
        preparePage();
        if (null != page && !page.isEmpty()) {
            r = s.createRow(rowNum++);
            List<OrderHistoryInfo> normalKey = new ArrayList<>();
            List<OrderHistoryInfo> invalidKey = new ArrayList<>();
            List<OrderHistoryInfo> expiredKey = new ArrayList<>();

            for (Object obj : page) {
                OrderHistoryInfo orderInfo = (OrderHistoryInfo) obj;
                if (orderInfo.getStatusFlag() == OrderHistoryInfo.ENTITLE_KEY_STATUS_NORMAL
                        && orderInfo.getCvgStatusFlag() == OrderHistoryInfo.ENTITLE_KEY_STATUS_NORMAL) {
                    normalKey.add(orderInfo);
                } else if (orderInfo.getStatusFlag() == OrderHistoryInfo.ENTITLE_KEY_STATUS_DISABLE
                        || orderInfo.getCvgStatusFlag() == OrderHistoryInfo.ENTITLE_KEY_STATUS_DISABLE) {
                    invalidKey.add(orderInfo);
                } else {
                    expiredKey.add(orderInfo);
                }
            }

            if (!normalKey.isEmpty()) {

                // normal entitle key title
                r = s.createRow(rowNum++);
                setEntitlementKeyCellValue(cellcount, cs4, null, r, null, MgrUtil.getUserMessage("order.key"));

                // normal entitle key info
                for (OrderHistoryInfo keyInfo : normalKey) {
                    r = s.createRow(rowNum++);
                    setEntitlementKeyCellValue(cellcount, cs6, cs3, r, keyInfo, null);
                }
            }

            if (!invalidKey.isEmpty()) {

                if (!normalKey.isEmpty()) {
                    r = s.createRow(rowNum++);
                    r = s.createRow(rowNum++);
                }

                // invalid entitle key title
                r = s.createRow(rowNum++);
                setEntitlementKeyCellValue(cellcount, cs5, null, r, null,
                        MgrUtil.getUserMessage("admin.license.orderkey.export.invalidkey.title"));

                // invalid entitle key info
                for (OrderHistoryInfo keyInfo : invalidKey) {
                    r = s.createRow(rowNum++);
                    setEntitlementKeyCellValue(cellcount, cs6, cs3, r, keyInfo, null);
                }
            }

            if (!expiredKey.isEmpty()) {

                if (!normalKey.isEmpty() || !invalidKey.isEmpty()) {
                    r = s.createRow(rowNum++);
                    r = s.createRow(rowNum++);
                }

                // expired entitle key title
                r = s.createRow(rowNum++);
                setEntitlementKeyCellValue(cellcount, cs5, null, r, null,
                        MgrUtil.getUserMessage("admin.license.orderkey.export.expiredkey.title"));

                // expired entitle key info
                for (OrderHistoryInfo keyInfo : expiredKey) {
                    r = s.createRow(rowNum++);
                    setEntitlementKeyCellValue(cellcount, cs6, cs3, r, keyInfo, null);
                }
            }
        }

        // write the workbook to the output stream
        // close our file (don't blow out our file handles)
        wb.write(out);
        out.close();
        generateAuditLog(HmAuditLog.STATUS_SUCCESS,
                MgrUtil.getUserMessage("hm.audit.log.export.entitlement.key"));
    } catch (Exception ex) {
        generateAuditLog(HmAuditLog.STATUS_FAILURE,
                MgrUtil.getUserMessage("hm.audit.log.export.entitlement.key"));
        log.error("exportEntitleKeyInfo()", ex.getMessage());
    }
}

From source file:com.alibaba.differ.biz.TableExporter.java

License:Open Source License

public void export() throws IOException {
    fc.setFileFilter(new ExcelFileFilter());
    fc.setFileHidingEnabled(true);// w ww .j  a  v a 2 s  . c  o  m
    fc.setAcceptAllFileFilterUsed(false);
    int returnValue = fc.showSaveDialog(null);
    if (returnValue != JFileChooser.APPROVE_OPTION) {
        return;
    }
    File file = fc.getSelectedFile();
    if (file.exists()) {
        JOptionPane.showMessageDialog(null, "");
        return;
    }
    FileOutputStream fos = new FileOutputStream(file + ".xls");
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet hs = wb.createSheet();
    TableModel tm = table.getModel();
    int row = tm.getRowCount();
    int cloumn = tm.getColumnCount();
    HSSFCellStyle style = wb.createCellStyle();
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    style.setBorderRight(HSSFCellStyle.BORDER_THIN);
    style.setBorderTop(HSSFCellStyle.BORDER_THIN);
    style.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);
    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    HSSFFont font = wb.createFont();
    font.setFontHeightInPoints((short) 11);
    style.setFont(font);
    HSSFCellStyle style1 = wb.createCellStyle();
    style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    style1.setBorderRight(HSSFCellStyle.BORDER_THIN);
    style1.setBorderTop(HSSFCellStyle.BORDER_THIN);
    style1.setFillForegroundColor(HSSFColor.ORANGE.index);
    style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    HSSFFont font1 = wb.createFont();
    font1.setFontHeightInPoints((short) 15);
    font1.setBoldweight((short) 700);
    style1.setFont(font);

    for (int i = 0; i < row + 1; i++) {
        HSSFRow hr = hs.createRow(i);
        for (int j = 0; j < cloumn; j++) {
            if (i == 0) {
                String value = tm.getColumnName(j);
                hs.setColumnWidth(j, UIConfig.EXCEL_COLUMN_WIDTH);
                HSSFRichTextString srts = new HSSFRichTextString(value);
                HSSFCell hc = hr.createCell((short) j);
                hc.setCellStyle(style1);
                hc.setCellValue(srts);
            } else {
                if (tm.getValueAt(i - 1, j) != null) {
                    String value = tm.getValueAt(i - 1, j).toString();
                    HSSFRichTextString srts = new HSSFRichTextString(value);
                    HSSFCell hc = hr.createCell((short) j);
                    hc.setCellStyle(style);

                    if (value.equals("") || value == null) {
                        hc.setCellValue(new HSSFRichTextString(""));
                    } else {
                        hc.setCellValue(srts);
                    }
                }
            }
        }
    }
    wb.write(fos);
    fos.close();
    JOptionPane.showMessageDialog(null, "Excel");
}

From source file:com.anphat.list.controller.ListStaffController.java

public File exportFile(List<StaffDTO> lstStaffInfo) {
    try {//w w  w . j  a  v  a2  s .c o m
        FileOutputStream fileOut = new FileOutputStream(PATH_EXPORT + Constants.FILE_NAME.LIST_STAFF);
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet worksheet = workbook.createSheet("danhsachnhanvien");
        HSSFCellStyle cellStyle;
        HSSFCellStyle rowStyle;

        HSSFCellStyle cellStyleLeft = null;
        HSSFCellStyle cellStyleRight = null;
        //style left
        cellStyleLeft = workbook.createCellStyle();
        cellStyleLeft.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        //phai
        cellStyleRight = workbook.createCellStyle();
        cellStyleRight.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
        cellStyleRight.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cellStyleRight.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cellStyleRight.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cellStyleRight.setBorderTop(HSSFCellStyle.BORDER_THIN);

        //                //header bang
        HSSFRow row5 = worksheet.createRow(0);

        HSSFCell cellA1 = row5.createCell(0);
        cellA1.setCellValue(BundleUtils.getString("STT"));
        cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        cellStyle.setWrapText(true);
        //rowstyle
        rowStyle = workbook.createCellStyle();
        rowStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        rowStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        rowStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        rowStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        rowStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        rowStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        rowStyle.setFillForegroundColor(HSSFColor.WHITE.index);
        rowStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        rowStyle.setWrapText(true);

        cellA1.setCellStyle(cellStyle);

        HSSFCell cellB1 = row5.createCell(1);
        cellB1.setCellValue(BundleUtils.getString("cms.StaffInfo.code"));
        cellB1.setCellStyle(cellStyle);

        HSSFCell cellC1 = row5.createCell(2);
        cellC1.setCellValue(BundleUtils.getString("cms.StaffInfo.name"));
        cellC1.setCellStyle(cellStyle);
        HSSFCell cellD1 = row5.createCell(3);
        cellD1.setCellValue(BundleUtils.getString("cms.StaffInfo.email"));
        cellD1.setCellStyle(cellStyle);
        HSSFCell cellE1 = row5.createCell(4);
        cellE1.setCellValue(BundleUtils.getString("cms.StaffInfo.birthdate"));
        cellE1.setCellStyle(cellStyle);
        HSSFCell cellF1 = row5.createCell(5);
        cellF1.setCellValue(BundleUtils.getString("cms.StaffInfo.phoneNumber"));
        cellF1.setCellStyle(cellStyle);
        HSSFCell cellG1 = row5.createCell(6);
        cellG1.setCellValue(BundleUtils.getString("cms.StaffInfo.deptName"));
        cellG1.setCellStyle(cellStyle);
        HSSFCell cellH1 = row5.createCell(7);
        cellH1.setCellValue(BundleUtils.getString("cms.StaffInfo.staffType"));
        cellH1.setCellStyle(cellStyle);
        HSSFCell cellI1 = row5.createCell(8);
        cellI1.setCellValue(BundleUtils.getString("cms.StaffInfo.status"));
        cellI1.setCellStyle(cellStyle);
        //content bang
        if (!lstStaffInfo.isEmpty()) {
            int j = 0;
            for (StaffDTO staff : lstStaffInfo) {
                HSSFRow row = worksheet.createRow(j + 1);

                HSSFCell cellA = row.createCell(0);
                cellA.setCellValue(j + 1);
                cellA.setCellStyle(rowStyle);
                HSSFCell cellB = row.createCell(1);
                cellB.setCellValue((staff.getDeptId() == null) ? Constants.NULL : staff.getCode());
                cellB.setCellStyle(cellStyleLeft);
                HSSFCell cellC = row.createCell(2);
                cellC.setCellValue((staff.getCode() == null) ? Constants.NULL : staff.getName());
                cellC.setCellStyle(cellStyleLeft);
                HSSFCell cellD = row.createCell(3);
                cellD.setCellValue((staff.getName() == null) ? Constants.NULL : staff.getEmail());
                cellD.setCellStyle(cellStyleLeft);
                HSSFCell cellE = row.createCell(4);
                //                    cellE.setCellValue((staff.getEmail() == null) ? Constants.NULL : staff.getBirthDate());
                cellE.setCellStyle(cellStyleLeft);
                HSSFCell cellF = row.createCell(5);
                cellF.setCellValue((staff.getTelNumber() == null) ? Constants.NULL : staff.getTelNumber());
                cellF.setCellStyle(cellStyleLeft);
                HSSFCell cellG = row.createCell(6);
                //                    cellG.setCellValue((staff.getDeptName() == null) ? Constants.NULL : staff.getDeptName());
                cellG.setCellStyle(cellStyleLeft);
                HSSFCell cellH = row.createCell(7);
                cellH.setCellValue((staff.getStaffType() == null) ? Constants.NULL
                        : BundleUtils.getString(
                                "staff.type." + DataUtil.getStringEscapeHTML4(staff.getStaffType())));
                cellH.setCellStyle(cellStyleLeft);
                HSSFCell cellI = row.createCell(8);
                cellI.setCellValue((staff.getStatus() == null) ? Constants.NULL
                        : BundleUtils.getString(
                                "common.status." + DataUtil.getStringEscapeHTML4(staff.getStatus())));
                cellI.setCellStyle(cellStyleLeft);
                j++;
            }
            //Set Width
            for (int i = 0; i <= 0; i++) {
                worksheet.setColumnWidth(i, 2000);
            }
            for (int i = 1; i <= 7; i++) {
                worksheet.setColumnWidth(i, 5000);
            }
            for (int i = 8; i <= 10; i++) {
                worksheet.setColumnWidth(i, 3000);
            }
        }
        try {
            workbook.write(fileOut);
            fileOut.flush();
            fileOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    File file = new File(PATH_EXPORT + Constants.FILE_NAME.LIST_STAFF);
    return file;

}

From source file:com.app.ExcelView.java

License:Open Source License

/**
 * ?Excel//from   ww w . j ava 2 s  . com
 * 
 * @param model
 *            ?
 * @param workbook
 *            workbook
 * @param request
 *            request
 * @param response
 *            response
 */
public void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    Assert.notEmpty(properties);
    HSSFSheet sheet;
    if (StringUtils.isNotEmpty(sheetName)) {
        sheet = workbook.createSheet(sheetName);
    } else {
        sheet = workbook.createSheet();
    }
    int rowNumber = 0;
    if (titles != null && titles.length > 0) {
        HSSFRow header = sheet.createRow(rowNumber);
        header.setHeight((short) 400);
        for (int i = 0; i < properties.length; i++) {
            HSSFCell cell = header.createCell(i);
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
            cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            HSSFFont font = workbook.createFont();
            font.setFontHeightInPoints((short) 11);
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);
            if (i == 0) {
                HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
                HSSFComment comment = patriarch
                        .createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 1, 1, (short) 4, 4));
                comment.setString(new HSSFRichTextString("P" + "o" + "w" + "e" + "r" + "e" + "d" + " " + "B"
                        + "y" + " " + "A" + "P" + "P" + "T" + "E" + "A" + "M"));
                cell.setCellComment(comment);
            }
            if (titles.length > i && titles[i] != null) {
                cell.setCellValue(titles[i]);
            } else {
                cell.setCellValue(properties[i]);
            }
            if (widths != null && widths.length > i && widths[i] != null) {
                sheet.setColumnWidth(i, widths[i]);
            } else {
                sheet.autoSizeColumn(i);
            }
        }
        rowNumber++;
    }
    if (data != null) {
        for (Object item : data) {
            HSSFRow row = sheet.createRow(rowNumber);
            for (int i = 0; i < properties.length; i++) {
                HSSFCell cell = row.createCell(i);
                if (converters != null && converters.length > i && converters[i] != null) {
                    Class<?> clazz = PropertyUtils.getPropertyType(item, properties[i]);
                    ConvertUtils.register(converters[i], clazz);
                    cell.setCellValue(BeanUtils.getProperty(item, properties[i]));
                    ConvertUtils.deregister(clazz);
                    if (clazz.equals(Date.class)) {
                        DateConverter dateConverter = new DateConverter();
                        dateConverter.setPattern(DEFAULT_DATE_PATTERN);
                        ConvertUtils.register(dateConverter, Date.class);
                    }
                } else {
                    cell.setCellValue(BeanUtils.getProperty(item, properties[i]));
                }
                if (rowNumber == 0 || rowNumber == 1) {
                    if (widths != null && widths.length > i && widths[i] != null) {
                        sheet.setColumnWidth(i, widths[i]);
                    } else {
                        sheet.autoSizeColumn(i);
                    }
                }
            }
            rowNumber++;
        }
    }
    if (contents != null && contents.length > 0) {
        rowNumber++;
        for (String content : contents) {
            HSSFRow row = sheet.createRow(rowNumber);
            HSSFCell cell = row.createCell(0);
            HSSFCellStyle cellStyle = workbook.createCellStyle();
            HSSFFont font = workbook.createFont();
            font.setColor(HSSFColor.GREY_50_PERCENT.index);
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);
            cell.setCellValue(content);
            rowNumber++;
        }
    }
    response.setContentType("application/force-download");
    if (StringUtils.isNotEmpty(filename)) {
        response.setHeader("Content-disposition",
                "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
    } else {
        response.setHeader("Content-disposition", "attachment");
    }
}