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

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

Introduction

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

Prototype

public HSSFWorkbook() 

Source Link

Document

Creates new HSSFWorkbook from scratch (start here!)

Usage

From source file:br.com.deltex.sqlexecutor.core.controller.SqlExecutorController.java

public void exportExcel() throws FileNotFoundException, IOException {
    // create a new workbook       
    HSSFWorkbook workBook = new HSSFWorkbook();
    // create a new sheet
    HSSFSheet sheet = workBook.createSheet();
    // declare a row object reference
    HSSFRow row = null;//from   w  w w  . j a v a2 s . c om
    // declare a cell object reference
    HSSFCell cell = null;

    int rowIndex = 0;
    int cellIndex = 0;

    try {
        for (List<String> rowDataQuery : this.dataQuery) {
            row = sheet.createRow(rowIndex);

            for (String column : rowDataQuery) {
                cell = row.createCell(cellIndex);
                cell.setCellValue(column);

                cellIndex++;
            }
            cellIndex = 0;
            rowIndex++;
        }

        // create a new file
        FileOutputStream out = new FileOutputStream(
                new File("C:/Users/harlan.bruno.santos/Desktop/workbook.xls"));

        workBook.write(out);
        out.close();
    } catch (FileNotFoundException e) {
        logger.error(e.getMessage(), e);
        throw e;
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        throw e;
    }
}

From source file:br.com.hslife.orcamento.controller.LancamentoContaController.java

License:Open Source License

@SuppressWarnings("resource")
public void exportarLancamentos() {
    if (listEntity == null || listEntity.isEmpty()) {
        warnMessage("Listagem vazio. Nada a exportar.");
    }//from   ww w . j a  v  a2s . c  o m

    try {

        HSSFWorkbook excel = new HSSFWorkbook();
        HSSFSheet planilha = excel.createSheet("lancamentoConta");

        HSSFRow linha = planilha.createRow(0);

        HSSFCell celula = linha.createCell(0);
        celula.setCellValue("Data");
        celula = linha.createCell(1);
        celula.setCellValue("Histrico");
        celula = linha.createCell(2);
        celula.setCellValue("Valor");

        int linhaIndex = 1;
        for (LancamentoConta l : listEntity) {
            linha = planilha.createRow(linhaIndex);

            celula = linha.createCell(0);
            celula.setCellValue(Util.formataDataHora(l.getDataPagamento(), Util.DATA));

            celula = linha.createCell(1);
            celula.setCellValue(l.getDescricao());

            celula = linha.createCell(2);
            celula.setCellValue(l.getValorPago());

            linhaIndex++;
        }

        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance()
                .getExternalContext().getResponse();
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=lancamentoConta.xls");
        response.setContentLength(excel.getBytes().length);
        ServletOutputStream output = response.getOutputStream();
        output.write(excel.getBytes(), 0, excel.getBytes().length);
        FacesContext.getCurrentInstance().responseComplete();

    } catch (IOException e) {
        errorMessage(e.getMessage());
    }
}

From source file:br.com.muranodesign.resources.AlunoVariavelResource.java

License:Creative Commons License

/**
 * Gerar relatrio de aluno variavel/*from  w  ww .j a  va  2  s .c o  m*/
 * @param Tutoria
 * @param Ano
 * @param Periodo
 * @param Nome
 * @param Sexo
 * @param Datanascimento
 * @param Endereco
 * @param TelefoneResidencial
 * @param TelefoneCelular
 * @param email
 * @param NomeResponsavel
 * @param ParentescoResponsavel
 * @param TelefoneResidencialResponsavel
 * @param TelefoneCelularResponsavel
 * @param TelefoneComercialResponsavel
 * @param emailResponsavel
 * @param NomeMae
 * @param EnderecoMae
 * @param TelefoneCelularMae
 * @param TelefoneResidencialMae
 * @param TelefoneComercialMae
 * @param emailMae
 * @return list
 */
@Path("Relatorio")
@POST
@Produces("application/json")
public List<String> relatorio(@FormParam("Tutoria") int Tutoria, @FormParam("Ano") int Ano,
        @FormParam("Periodo") int Periodo, @FormParam("Neces") String Neces, @FormParam("Prog") String Prog,
        @FormParam("Status") String Status,

        @FormParam("tutoria") String tut, @FormParam("Nome") String Nome, @FormParam("Sexo") String Sexo,
        @FormParam("Datanascimento") String Datanascimento, @FormParam("Endereco") String Endereco,
        @FormParam("TelefoneResidencial") String TelefoneResidencial,
        @FormParam("TelefoneCelular") String TelefoneCelular, @FormParam("email") String email,
        @FormParam("NomeResponsavel") String NomeResponsavel,
        @FormParam("ParentescoResponsavel") String ParentescoResponsavel,
        @FormParam("TelefoneResidencialResponsavel") String TelefoneResidencialResponsavel,
        @FormParam("TelefoneCelularResponsavel") String TelefoneCelularResponsavel,
        @FormParam("TelefoneComercialResponsavel") String TelefoneComercialResponsavel,
        @FormParam("emailResponsavel") String emailResponsavel, @FormParam("NomeMae") String NomeMae,
        @FormParam("EnderecoMae") String EnderecoMae,
        @FormParam("TelefoneCelularMae") String TelefoneCelularMae,
        @FormParam("TelefoneResidencialMae") String TelefoneResidencialMae,
        @FormParam("TelefoneComercialMae") String TelefoneComercialMae,
        @FormParam("emailMae") String emailMae) {

    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet firstSheet = workbook.createSheet("Relatorio Aluno");

    FileOutputStream fos = null;

    String nomeArquivo;

    try {

        nomeArquivo = new StringUtil().geraNomeAleatorio("xls", 15);
        fos = new FileOutputStream(new File("/home/tomcat/webapps/files/" + nomeArquivo));
        //fos = new FileOutputStream(new File("C:/Users/murano/Desktop/Teste/" + nomeArquivo));      

        HSSFRow row = firstSheet.createRow(0);

        List<String> retorno = new ArrayList<String>();
        List<List<String>> listRetorno = new ArrayList<List<String>>();

        int h = 0;
        row = firstSheet.createRow(h);
        row.createCell((short) 0).setCellValue("Tutoria");
        row.createCell((short) 1).setCellValue("Nome");
        row.createCell((short) 2).setCellValue("Sexo");
        row.createCell((short) 3).setCellValue("Datanascimento");
        row.createCell((short) 4).setCellValue("Endereco");
        row.createCell((short) 5).setCellValue("TelefoneResidencial");
        row.createCell((short) 6).setCellValue("TelefoneCelular");
        row.createCell((short) 7).setCellValue("email");
        row.createCell((short) 8).setCellValue("NomeResponsavel");
        row.createCell((short) 9).setCellValue("ParentescoResponsavel");
        row.createCell((short) 10).setCellValue("TelefoneResidencialResponsavel");
        row.createCell((short) 11).setCellValue("TelefoneCelularResponsavel");
        row.createCell((short) 12).setCellValue("TelefoneComercialResponsavel");
        row.createCell((short) 13).setCellValue("emailResponsavel");
        row.createCell((short) 14).setCellValue("NomeMae");
        row.createCell((short) 15).setCellValue("EnderecoMae");
        row.createCell((short) 16).setCellValue("TelefoneCelularMae");
        row.createCell((short) 17).setCellValue("TelefoneResidencialMae");
        row.createCell((short) 18).setCellValue("TelefoneComercialMae");
        row.createCell((short) 19).setCellValue("emailMae");

        List<AlunoVariavel> list = new AlunoVariavelService().listarRelatorioSecretaria(Tutoria, Ano, Periodo,
                Neces, Prog, Status);
        for (AlunoVariavel alunoVariavel : list) {
            h++;
            row = firstSheet.createRow(h);
            //row.createCell((short) 0).setCellValue("Relatrio");

            if (tut != "" && tut != null) {

                String valor = alunoVariavel.getGrupo().getTutoria().getTutoria();

                if (valor != null && valor != "-") {
                    retorno.add(valor);

                    row.createCell((short) 0).setCellValue(valor);
                } else {
                    retorno.add("no informado");
                }
            }

            if (Nome != "" && Nome != null) {

                String valor = alunoVariavel.getAluno().getNome();

                if (valor != null && valor != "-") {
                    retorno.add(valor);
                    row.createCell((short) 1).setCellValue(valor);

                } else {
                    retorno.add("no informado");
                }

            }
            if (Sexo != "" && Sexo != null) {

                String valor = alunoVariavel.getAluno().getSexo();

                if (valor != null && valor != "-") {
                    retorno.add(valor);
                    row.createCell((short) 2).setCellValue(valor);
                } else {
                    retorno.add("no informado");
                }

            }
            if (Datanascimento != "" && Datanascimento != null) {

                Date valor = alunoVariavel.getAluno().getDataNascimento();

                if (valor != null) {

                    DateFormat formatter = new SimpleDateFormat("dd-MM-yy");

                    String dataS = formatter.format(valor);

                    //Date data = (Date) formatter.parse(dataS);

                    retorno.add(dataS);

                    row.createCell((short) 3).setCellValue(valor);
                } else {
                    retorno.add(null);
                }

            }
            if (Endereco != "" && Endereco != null) {

                String valor = alunoVariavel.getAluno().getEndereco();

                if (valor != null && valor != "-") {
                    retorno.add(valor);
                    row.createCell((short) 4).setCellValue(valor);
                } else {
                    retorno.add("no informado");
                }

            }
            if (TelefoneResidencial != "" && TelefoneResidencial != null) {

                String valor = alunoVariavel.getAluno().getTelefoneResidencialResponsavel();

                if (valor != null && valor != "-") {
                    retorno.add(valor);
                    row.createCell((short) 5).setCellValue(valor);
                } else {
                    retorno.add("no informado");
                }

            }
            if (TelefoneCelular != "" && TelefoneCelular != null) {

                String valor = alunoVariavel.getAluno().getTelefoneCelularResponsavel();

                if (valor != null && valor != "-") {
                    retorno.add(valor);

                    row.createCell((short) 6).setCellValue(valor);
                } else {
                    retorno.add("no informado");
                }

            }
            if (email != "" && email != null) {

                String valor = alunoVariavel.getAluno().getEmail();

                if (valor != null && valor != "-") {
                    retorno.add(valor);

                    row.createCell((short) 7).setCellValue(valor);
                } else {
                    retorno.add("no informado");
                }

            }
            if (NomeResponsavel != "" && NomeResponsavel != null) {

                String valor = alunoVariavel.getAluno().getNomeResponsavel();

                if (valor != null && valor != "-") {
                    retorno.add(valor);

                    row.createCell((short) 8).setCellValue(valor);
                } else {
                    retorno.add("no informado");
                }

            }
            if (ParentescoResponsavel != "" && ParentescoResponsavel != null) {

                String valor = alunoVariavel.getAluno().getParentescoResponsavel();

                if (valor != null && valor != "-") {
                    retorno.add(valor);

                    row.createCell((short) 9).setCellValue(valor);
                } else {
                    retorno.add("no informado");
                }

            }
            if (TelefoneResidencialResponsavel != "" && TelefoneResidencialResponsavel != null) {

                String valor = alunoVariavel.getAluno().getTelefoneResidencialResponsavel();

                if (valor != null && valor != "-") {
                    retorno.add(valor);

                    row.createCell((short) 10).setCellValue(valor);
                } else {
                    retorno.add("no informado");
                }

            }
            if (TelefoneCelularResponsavel != "" && TelefoneCelularResponsavel != null) {

                String valor = alunoVariavel.getAluno().getTelefoneCelularResponsavel();

                if (valor != null && valor != "-") {
                    retorno.add(valor);

                    row.createCell((short) 11).setCellValue(valor);
                } else {
                    retorno.add("no informado");
                }

            }
            if (TelefoneComercialResponsavel != "" && TelefoneComercialResponsavel != null) {

                String valor = alunoVariavel.getAluno().getTelefoneComercialResponsavel();

                if (valor != null && valor != "-") {
                    retorno.add(valor);

                    row.createCell((short) 12).setCellValue(valor);
                } else {
                    retorno.add("no informado");
                }

            }
            if (emailResponsavel != "" && emailResponsavel != null) {

                String valor = alunoVariavel.getAluno().getEmail1Responsavel();

                if (valor != null && valor != "-") {
                    retorno.add(valor);

                    row.createCell((short) 13).setCellValue(valor);
                } else {
                    retorno.add("no informado");
                }

            }
            if (NomeMae != "" && NomeMae != null) {

                String valor = alunoVariavel.getAluno().getNomeMae();

                if (valor != null && valor != "-") {
                    retorno.add(valor);

                    row.createCell((short) 14).setCellValue(valor);
                } else {
                    retorno.add("no informado");
                }

            }
            if (EnderecoMae != "" && EnderecoMae != null) {

                String valor = alunoVariavel.getAluno().getEnderecoMae();

                if (valor != null && valor != "-") {
                    retorno.add(valor);

                    row.createCell((short) 15).setCellValue(valor);
                } else {
                    retorno.add("no informado");
                }

            }
            if (TelefoneCelularMae != "" && TelefoneCelularMae != null) {

                String valor = alunoVariavel.getAluno().getTelefoneCelularMae();

                if (valor != null && valor != "-") {
                    retorno.add(valor);

                    row.createCell((short) 16).setCellValue(valor);
                } else {
                    retorno.add("no informado");
                }

            }
            if (TelefoneResidencialMae != "" && TelefoneResidencialMae != null) {

                String valor = alunoVariavel.getAluno().getTelefoneResidencialMae();

                if (valor != null && valor != "-") {
                    retorno.add(valor);

                    row.createCell((short) 17).setCellValue(valor);
                } else {
                    retorno.add("no informado");
                }

            }
            if (TelefoneComercialMae != "" && TelefoneComercialMae != null) {

                String valor = alunoVariavel.getAluno().getTelefoneComercialMae();

                if (valor != null && valor != "-") {
                    retorno.add(valor);

                    row.createCell((short) 18).setCellValue(valor);
                } else {
                    retorno.add("no informado");
                }

            }
            if (emailMae != "" && emailMae != null) {

                String valor = alunoVariavel.getAluno().getEmail1Mae();

                if (valor != null && valor != "-") {
                    retorno.add(valor);

                    row.createCell((short) 19).setCellValue(valor);
                } else {
                    retorno.add("no informado");
                }
            }

            listRetorno.add(retorno);
        }

        if (listRetorno.isEmpty()) {
            retorno.add("no h valores");
            listRetorno.add(retorno);
        }

        workbook.write(fos);

        retorno.add("http://177.55.99.90/files/" + nomeArquivo);
        //retorno.add("http://172.16.31.178/"+nomeArquivo);
        listRetorno.add(retorno);
        return listRetorno.get(0);

    } catch (Exception e) {
        e.printStackTrace();
        logger.info("Erro ao exportar arquivo");

    } finally {
        try {
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;

}

From source file:br.com.objectos.way.io.POIWorkbook.java

License:Apache License

public POIWorkbook() {
    wb = new HSSFWorkbook();
}

From source file:br.com.tecsinapse.dataio.style.TableCellStyleTest.java

License:LGPL

@Test(dataProvider = "toStyleDs")
public void toCellStyleTest(HSSFColor bgColor, int bgIndex, HSSFColor fontColor, int fontIndex) {
    HSSFWorkbook wb = new HSSFWorkbook();
    TableCellStyle style = new TableCellStyle(bgColor);
    style.setFontColor(fontColor);//  ww w .ja  va  2 s. c  o m
    CellStyle cellStyle = style.toCellStyle(wb);
    Assert.assertEquals(cellStyle.getFillForegroundColor(), bgIndex);
}

From source file:br.com.tecsinapse.dataio.Table.java

License:LGPL

public Workbook toHSSFWorkBook() {
    return toWorkBook(new HSSFWorkbook());
}

From source file:br.com.tecsinapse.dataio.util.ExporterUtil.java

License:LGPL

private static void writeXls(List<Table> tables, OutputStream outputStream) throws IOException {
    writeSpreadsheet(new HSSFWorkbook(), tables, outputStream);
}

From source file:br.com.tecsinapse.files.test.ExporterFileTest.java

License:LGPL

@DataProvider(name = "beans")
public Object[][] beans() {
    final List<FileBean> beans = FileBean.getBeans();

    //melhorar e unificar api de exportao
    final Function<Table, File> csvExport = new Function<Table, File>() {
        @Override//from ww  w.j a  v a  2s .c o m
        public File apply(Table table) {
            try {
                final File csv = File.createTempFile("csv", ".csv");
                csv.deleteOnExit();

                ExporterUtil.writeCsvToOutput(table, Charsets.ISO_8859_1.displayName(),
                        new FileOutputStream(csv));

                return csv;
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
        }
    };
    final Function<File, List<List<String>>> csvLines = new Function<File, List<List<String>>>() {
        @Override
        public List<List<String>> apply(File input) {
            try {
                final List<String> strings = CsvUtil.processCSV(new FileInputStream(input),
                        Charsets.ISO_8859_1);

                return FluentIterable.from(strings).transform(new Function<String, List<String>>() {
                    @Override
                    public List<String> apply(String input) {
                        return Arrays.asList(input.split(";"));
                    }
                }).toList();
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
        }
    };

    final Function<File, List<List<String>>> excelLines = new Function<File, List<List<String>>>() {
        @Override
        public List<List<String>> apply(File file) {
            try (final SpreadsheetParser<?> parser = new SpreadsheetParser<>(null, file)) {
                parser.setExporterFormatter(exporterFormatter);
                parser.setHeadersRows(0);
                return parser.getLines();
            } catch (Exception e) {
                throw Throwables.propagate(e);
            }
        }
    };

    final Function<Table, File> xlsExport = toWorkbookFunction(new Supplier<Workbook>() {
        @Override
        public Workbook get() {
            return new HSSFWorkbook();
        }
    });
    final Function<Table, File> xlsxExport = toWorkbookFunction(new Supplier<Workbook>() {
        @Override
        public Workbook get() {
            return new XSSFWorkbook();
        }
    });
    final Function<Table, File> sxlsxExport = toWorkbookFunction(new Supplier<Workbook>() {
        @Override
        public Workbook get() {
            return new SXSSFWorkbook();
        }
    });

    return new Object[][] { { beans, csvExport, csvLines, "#.#", "" },
            { beans, xlsExport, excelLines, "#.#", "" }, { beans, xlsxExport, excelLines, ".#", "" },
            { beans, sxlsxExport, excelLines, ".#", "" } };
}

From source file:br.prof.salesfilho.oci.service.ExcelService.java

public void createWorkBook() {
    this.workbook = new HSSFWorkbook();
}

From source file:br.sp.telesul.service.ExportServiceImpl.java

@Override
public void buildExcelDocument(String type, String[] columns, String[] columnsFormacao, String[] columnsIdiomas,
        String[] columnsCertificacoes, HttpServletRequest request, HttpServletResponse response) {
    HSSFWorkbook workbook = new HSSFWorkbook();
    writeExcel("Funcionarios", columns, workbook);
    writeExcelFormacao("Formaes", columnsFormacao, workbook);
    writeExcelIdiomas("Idiomas", columnsIdiomas, workbook);
    writeExcelCertificacoes("Certificaes", columnsCertificacoes, workbook);

    downloadExcel(request, response, workbook);
}