List of usage examples for org.apache.poi.hssf.usermodel HSSFWorkbook write
private void write(POIFSFileSystem fs) throws IOException
From source file:binky.reportrunner.engine.renderers.exporters.XLSExporter.java
License:Open Source License
@SuppressWarnings("deprecation") @Override//from w w w .ja v a 2 s .co m public void export(ResultSet resultSet, String label, OutputStream outputStream) throws ExportException { try { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("Report"); ResultSetMetaData metaData; metaData = resultSet.getMetaData(); short rowCount = 0; // logger.debug("writing header"); HSSFRow headerRow = sheet.createRow(rowCount); for (int i = 1; i <= metaData.getColumnCount(); i++) { //TODO:fix HSSFCell cell = headerRow.createCell((short) (i - 1)); HSSFRichTextString string = new HSSFRichTextString(metaData.getColumnName(i)); string.applyFont(HSSFFont.BOLDWEIGHT_BOLD); cell.setCellValue(string); } while (resultSet.next()) { rowCount++; HSSFRow row = sheet.createRow(rowCount); for (int i = 1; i <= metaData.getColumnCount(); i++) { //TODO:fix HSSFCell cell = row.createCell((short) (i - 1)); // TODO:make this better by using types HSSFRichTextString string = new HSSFRichTextString("" + resultSet.getObject(i)); cell.setCellValue(string); } } // Write the output to the stream file wb.write(outputStream); outputStream.flush(); } catch (SQLException e) { throw new ExportException(e.getMessage(), e); } catch (IOException e) { throw new ExportException(e.getMessage(), e); } }
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 ww w . ja va 2 s. c o m*/ // 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.muranodesign.resources.AlunoVariavelResource.java
License:Creative Commons License
/** * Gerar relatrio de aluno variavel// w w w . j av a 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.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;/* w ww . j a v a 2 s . 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); } }
From source file:br.sp.telesul.service.ExportServiceImpl.java
public void downloadExcel(HttpServletRequest request, HttpServletResponse response, HSSFWorkbook wb) { ServletOutputStream stream = null;//from w w w.j av a 2 s . c o m String fileName = "relatorio" + " " + new Date().getTime(); fileName = fileName.replace(" ", "_"); try { stream = response.getOutputStream(); response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls"); response.setContentType("application/vnd.ms-excel"); wb.write(stream); System.out.println("Excel saved!!!!!"); } catch (Exception e) { System.out.println("Error write excel" + e); } finally { if (stream != null) { try { stream.close(); wb.close(); } catch (IOException io) { System.out.println("Error close Steram" + io); } } } }
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();// www .ja v a2 s. co m 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:br.ufpa.psi.comportamente.labgame.relatorios.RelatorioGrafico.java
License:Open Source License
public InputStream estatisticaJogadas(List<Jogada> jogadas) throws ParsePropertyException, IOException, InvalidFormatException { TipoJogada jogadaPontCult = new TipoJogada("Pontuao Cultural", 0); TipoJogada jogadaPontIndiv = new TipoJogada("Pontuao Individual", 0); TipoJogada jogadaSemPont = new TipoJogada("Sem Pontuao", 0); for (Jogada jogada : jogadas) { int cont = 0; if (jogada.getPontuacaoCultural() > 0) { cont++;//www . j av a 2 s.c o m jogadaPontCult.total++; } if (jogada.getPontuacaoIndividual() > 0) { cont++; jogadaPontIndiv.total++; } if (cont == 0) { jogadaSemPont.total++; } } HSSFWorkbook workbook = new HSSFWorkbook(); List<TipoJogada> listaTipoJogadas = new ArrayList<>(); listaTipoJogadas.add(jogadaSemPont); listaTipoJogadas.add(jogadaPontCult); listaTipoJogadas.add(jogadaPontIndiv); Map beans = new HashMap(); beans.put("jogadas", listaTipoJogadas); XLSTransformer transformer = new XLSTransformer(); InputStream ioStream = RelatorioGrafico.class.getClassLoader().getResourceAsStream( "br/ufpa/psi/comportamente/labgame/relatorios/templates/Template_Chart_Pontuacao.xls"); transformer.markAsFixedSizeCollection("jogadas"); byte[] bytes; try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { workbook = (HSSFWorkbook) transformer.transformXLS(ioStream, beans); workbook.write(out); bytes = out.toByteArray(); } InputStream in = new ByteArrayInputStream(bytes); return in; }
From source file:Calendar.Calendario.java
private void crearExcel(double[][] excel, String name) { HSSFWorkbook libro = new HSSFWorkbook(); HSSFSheet hoja = libro.createSheet(); HSSFRow fila;/* www. ja v a 2s. c o m*/ HSSFCell celda; fila = hoja.createRow(2); celda = fila.createCell(3); HSSFRichTextString texto = new HSSFRichTextString("prueba"); celda.setCellValue(texto); celda = fila.createCell(4); celda.setCellValue("prueba2"); /* for (int i = 0; i < modelo.getColumnCount(); i++) { celda = fila.createCell(i); HSSFRichTextString texto = new HSSFRichTextString((String) (modelo.getColumnName(i))); celda.setCellValue(texto); } for (int i = 0; i < modelo.getRowCount(); i++) { fila = hoja.createRow(i + 1); for (int k = 0; k < modelo.getColumnCount(); k++) { celda = fila.createCell(k); HSSFRichTextString texto = new HSSFRichTextString((String) (modelo.getValueAt(i, k))); celda.setCellValue(texto); } }*/ FileOutputStream elFichero; try { elFichero = new FileOutputStream(name + ".xls"); libro.write(elFichero); elFichero.close(); JOptionPane.showMessageDialog(null, "Se genero la planilla correctamente"); } catch (IOException ex) { JOptionPane.showMessageDialog(null, "Error al crear la planilla de Excel. Recuerde que el nombre del archivo no puede contener ninguno de los siguientes caracteres: \\ / : * ? \"< > | Intentelo nuevamente y si el problema persiste contacte con el administrador."); System.out.println(ex); System.exit(0); } }
From source file:ch.astina.hesperid.util.jasper.JasperExcelStreamResponse.java
License:Apache License
@Override public void exportReportToStream(JasperPrint jasperPrint, OutputStream outputStream) throws Exception { JRXlsExporter exporter = new JRXlsExporter(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, baos); exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, this.removeEmptySpaceBetweenRows); exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS, this.removeEmptySpaceBetweenColumns); exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, this.printWhitePageBackground); exporter.setParameter(JRXlsExporterParameter.IS_FONT_SIZE_FIX_ENABLED, this.fixFontSize); exporter.setParameter(JRXlsExporterParameter.IS_IGNORE_GRAPHICS, this.ignoreGraphics); exporter.exportReport();//from ww w.ja v a 2 s . co m HSSFWorkbook workbook = new HSSFWorkbook(new ByteArrayInputStream(baos.toByteArray())); workbook.getSheetAt(0).setAutobreaks(true); workbook.getSheetAt(0).getPrintSetup().setFitHeight((short) jasperPrint.getPages().size()); workbook.getSheetAt(0).getPrintSetup().setFitWidth((short) 1); SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy")); for (Integer x = 0; x < workbook.getSheetAt(0).getPhysicalNumberOfRows(); x++) { HSSFRow row = workbook.getSheetAt(0).getRow(x); Iterator<Cell> ci = row.cellIterator(); Cell c = null; Date d = null; while (ci.hasNext()) { c = ci.next(); try { d = sdf.parse(c.getStringCellValue().trim()); c.setCellValue(d); c.setCellStyle(cellStyle); } catch (Exception e) { } } } workbook.write(outputStream); }
From source file:chocanproject.ServiceDetailsGUI.java
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: try {// ww w . j av a 2 s . c o m Connection con = DriverManager.getConnection(connectionUrl); Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); String sql = "ChocAn.dbo.GetProviderDirectory_Select"; ResultSet rs = st.executeQuery(sql); //Create a blank workbook and sheet HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("NewSheet"); HSSFRow rowhead = sheet.createRow((short) 0); rowhead.createCell((short) 0).setCellValue("CellHeadName1"); rowhead.createCell((short) 1).setCellValue("CellHeadName2"); rowhead.createCell((short) 2).setCellValue("CellHeadName3"); int i = 1; while (rs.next()) { HSSFRow row = sheet.createRow((short) i); row.createCell((short) 0).setCellValue(rs.getString("ServiceDesc")); row.createCell((short) 1).setCellValue(Integer.toString(rs.getInt("ServiceID"))); row.createCell((short) 2).setCellValue(rs.getString("Cost")); i++; } String ProviderDirectory = "D:/Excel/ProviderDirectory.xls"; FileOutputStream fileOut = new FileOutputStream(ProviderDirectory); workbook.write(fileOut); JOptionPane.showMessageDialog(null, "Directory file has been sent to your email address successfully and saved a .xlsx file at local path D:/Excel/"); fileOut.close(); } catch (SQLException e1) { e1.printStackTrace(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } }