List of usage examples for org.apache.poi.hssf.usermodel HSSFCell setCellValue
@SuppressWarnings("fallthrough") public void setCellValue(boolean value)
From source file:binky.reportrunner.engine.renderers.exporters.TabbedXLSExporter.java
License:Open Source License
@SuppressWarnings("deprecation") @Override/*from ww w .j av a 2 s . co m*/ public void export(ResultSet resultSet, String label, OutputStream outputStream) throws ExportException { if (this.outputStream == null) this.outputStream = outputStream; try { if (wb == null) { logger.trace("creating new workbook"); wb = new HSSFWorkbook(); } logger.trace("creaing worksheet " + label); HSSFSheet sheet = wb.createSheet(label); 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); } } } catch (SQLException e) { throw new ExportException(e.getMessage(), e); } }
From source file:binky.reportrunner.engine.renderers.exporters.XLSExporter.java
License:Open Source License
@SuppressWarnings("deprecation") @Override// www .ja va 2 s .c o 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 w ww.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.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 w w w . ja v a2s .com 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.solutio.licita.controlador.ControladorPregao.java
public void editandoXlsParaExportar(Object document) { HSSFWorkbook wb = (HSSFWorkbook) document; HSSFSheet planilha = wb.getSheetAt(0); //Move as celulas selecionadas para baixo de acordo com o valor informado planilha.shiftRows(planilha.getFirstRowNum(), planilha.getLastRowNum(), 5); HSSFRow linha0 = planilha.createRow(0); linha0.createCell(0).setCellValue("Instituio Licitadora:"); planilha.addMergedRegion(new CellRangeAddress(0, 0, 0, 1)); linha0.createCell(2)//from w w w. j a v a2 s . c o m .setCellValue(" " + getEntidade().getInstituicaoLicitadora().getPessoaJuridica().getNomeFantasia()); planilha.addMergedRegion(new CellRangeAddress(0, 0, 2, 6)); HSSFRow linha1 = planilha.createRow(1); linha1.createCell(0).setCellValue("Numero do Pregao:"); planilha.addMergedRegion(new CellRangeAddress(1, 1, 0, 1)); linha1.createCell(2).setCellValue(" " + getEntidade().getNumeroPregao()); planilha.addMergedRegion(new CellRangeAddress(1, 1, 2, 6)); HSSFRow linha2 = planilha.createRow(2); linha2.createCell(0).setCellValue("Numero do Processo:"); planilha.addMergedRegion(new CellRangeAddress(2, 2, 0, 1)); linha2.createCell(2).setCellValue(" " + getEntidade().getNumeroProcesso()); planilha.addMergedRegion(new CellRangeAddress(2, 2, 2, 6)); HSSFRow linha3 = planilha.createRow(3); linha3.createCell(0).setCellValue("Empresa Licitante:"); planilha.addMergedRegion(new CellRangeAddress(3, 3, 0, 1)); linha3.createCell(2).setCellValue("Preencha com o nome de sua Empresa"); planilha.addMergedRegion(new CellRangeAddress(3, 3, 2, 6)); HSSFRow linha4 = planilha.createRow(4); //Nova coluna para a empresas adicionarem seus valores HSSFRow linha5 = planilha.getRow(5); HSSFCell celula5 = linha5.createCell(5); celula5.setCellValue("Valor do Licitante"); //for para ajustar automaticamente o tamnho das colunas for (int i = 0; i < 6; i++) { planilha.autoSizeColumn(i); } //Cor da linha de titulos da tabela HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index); cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); for (int i = 0; i < linha5.getPhysicalNumberOfCells(); i++) { HSSFCell cell = linha5.getCell(i); cell.setCellStyle(cellStyle); } CellStyle unlockedCellStyle = wb.createCellStyle(); unlockedCellStyle.setLocked(false); HSSFCell celula2 = linha3.getCell(2); celula2.setCellStyle(unlockedCellStyle); }
From source file:bs.global.util.ExcelFactory.java
private void writeCell(HSSFRow row, int col, Object value, FormatType formatType, Short bgColor, HSSFFont font) throws NestableException { HSSFCell cell = HSSFCellUtil.createCell(row, col, null); if (value == null) { return;/* w w w. j a v a2s . co m*/ } if (font != null) { HSSFCellStyle style = workbook.createCellStyle(); style.setFont(font); cell.setCellStyle(style); } switch (formatType) { case TEXT: cell.setCellValue(value.toString()); break; case INTEGER: cell.setCellValue(((Number) value).intValue()); HSSFCellUtil.setCellStyleProperty(cell, workbook, CellUtil.DATA_FORMAT, HSSFDataFormat.getBuiltinFormat(("#,##0"))); break; case FLOAT: cell.setCellValue(((Number) value).doubleValue()); HSSFCellUtil.setCellStyleProperty(cell, workbook, CellUtil.DATA_FORMAT, HSSFDataFormat.getBuiltinFormat(("#,##0.00"))); break; case DATE: cell.setCellValue((Date) value); HSSFCellUtil.setCellStyleProperty(cell, workbook, CellUtil.DATA_FORMAT, HSSFDataFormat.getBuiltinFormat(("m/d/yy"))); break; case MONEY: cell.setCellValue(((Number) value).intValue()); HSSFCellUtil.setCellStyleProperty(cell, workbook, CellUtil.DATA_FORMAT, format.getFormat("($#,##0.00);($#,##0.00)")); break; case PERCENTAGE: cell.setCellValue(((Number) value).doubleValue()); HSSFCellUtil.setCellStyleProperty(cell, workbook, CellUtil.DATA_FORMAT, HSSFDataFormat.getBuiltinFormat("0.00%")); } if (bgColor != null) { HSSFCellUtil.setCellStyleProperty(cell, workbook, CellUtil.FILL_FOREGROUND_COLOR, bgColor); HSSFCellUtil.setCellStyleProperty(cell, workbook, CellUtil.FILL_PATTERN, HSSFCellStyle.SOLID_FOREGROUND); } }
From source file:Calendar.Calendario.java
private void crearExcel(double[][] excel, String name) { HSSFWorkbook libro = new HSSFWorkbook(); HSSFSheet hoja = libro.createSheet(); HSSFRow fila;/* www .j a va 2 s . 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.javasoft.metabolic.generate.ExcelGenerator.java
License:BSD License
protected static void writeToCell(HSSFSheet sheet, int row, int col, String value, HSSFCellStyle style) { final HSSFCell xlsCell = getCell(sheet, row, col, true); xlsCell.setCellType(HSSFCell.CELL_TYPE_STRING); // xlsCell.setEncoding(HSSFCell.ENCODING_UTF_16); // xlsCell.setCellValue(new String(value.getBytes(CHARSET_UTF_16), CHARSET_UTF_16)); xlsCell.setCellValue(new HSSFRichTextString(value)); if (style != null) { xlsCell.setCellStyle(style);// w w w. j a va 2 s . com } }
From source file:citibob.reports.PoiXlsWriter.java
License:Open Source License
void copyCell(HSSFCell c0, HSSFCell c1) { copyCellFormatting(c0, c1);/*from w w w .ja v a 2 s . c o m*/ c1.setCellType(c0.getCellType()); switch (c0.getCellType()) { case HSSFCell.CELL_TYPE_STRING: c1.setCellValue(c0.getRichStringCellValue()); break; case HSSFCell.CELL_TYPE_NUMERIC: c1.setCellValue(c0.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_FORMULA: c1.setCellFormula(c0.getCellFormula()); break; case HSSFCell.CELL_TYPE_BOOLEAN: c1.setCellValue(c0.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: c1.setCellErrorValue(c0.getErrorCellValue()); break; } }
From source file:citibob.reports.PoiXlsWriter.java
License:Open Source License
void setValue(HSSFCell c2, Object val) { // Poor man's convert to Excel data types if (val instanceof java.util.Date) { c2.setCellType(HSSFCell.CELL_TYPE_NUMERIC); c2.setCellValue(xlserial.getSerial((java.util.Date) val)); } else if (val instanceof Number) { c2.setCellType(HSSFCell.CELL_TYPE_NUMERIC); c2.setCellValue(((Number) val).doubleValue()); } else if (val instanceof Boolean) { c2.setCellType(HSSFCell.CELL_TYPE_BOOLEAN); c2.setCellValue(((Boolean) val).booleanValue()); } else {/*from w w w . j av a 2 s . com*/ // Assume a String if (val == null) { c2.setCellType(HSSFCell.CELL_TYPE_BLANK); } else { String sval = val.toString(); c2.setCellType(HSSFCell.CELL_TYPE_STRING); c2.setCellValue(new HSSFRichTextString(sval)); } } }