List of usage examples for org.apache.poi.hssf.usermodel HSSFFont BOLDWEIGHT_BOLD
short BOLDWEIGHT_BOLD
To view the source code for org.apache.poi.hssf.usermodel HSSFFont BOLDWEIGHT_BOLD.
Click Source Link
From source file:HSSFReadWrite.java
License:Apache License
/** * given a filename this outputs a sample sheet with just a set of * rows/cells.//www .j a v a 2 s .co 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:PlacasClientes.java
private void EnviarExcel(ResultSet rs) throws IOException { String rutaArchivo = System.getProperty("user.home") + "/ejemploExcelJava.xls"; /*Se crea el objeto de tipo File con la ruta del archivo*/ File archivoXLS = new File(rutaArchivo); /*Si el archivo existe se elimina*/ if (archivoXLS.exists()) archivoXLS.delete();/*from w w w. j a va 2 s . c o m*/ /*Se crea el archivo*/ archivoXLS.createNewFile(); /*Se crea el libro de excel usando el objeto de tipo Workbook*/ Workbook libro = new HSSFWorkbook(); CreationHelper createhelper = libro.getCreationHelper(); CellStyle cellStyle = libro.createCellStyle(); cellStyle.setDataFormat(createhelper.createDataFormat().getFormat("dd/mm/yyyy")); CellStyle cellStyle2 = libro.createCellStyle(); /*Se inicializa el flujo de datos con el archivo xls*/ FileOutputStream archivo = new FileOutputStream(archivoXLS); /*Utilizamos la clase Sheet para crear una nueva hoja de trabajo dentro del libro que creamos anteriormente*/ Sheet hoja = libro.createSheet("ClientesPlacas"); Font fuente = libro.createFont(); fuente.setFontHeightInPoints((short) 13); fuente.setFontName("Arial"); fuente.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); Font fuente2 = libro.createFont(); fuente.setFontHeightInPoints((short) 13); fuente.setFontName("Arial"); //fuente.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); Row Enc = hoja.createRow(2); Cell celda = Enc.createCell(0); celda.setCellValue("PLACA DE VEHICULOS POR CLIENTES"); }
From source file:angiotool.SaveToExcel.java
License:Open Source License
private HSSFCellStyle headingCellStyle() { HSSFFont font = wb.createFont(); // defaults to Arial on windows font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // bold HSSFCellStyle style = wb.createCellStyle(); // make style style.setFont(font); // set font style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // set alignment to centered return style; }
From source file:at.fh.swenga.firefighters.report.ExcelFireEngineReportView.java
@Override protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { List<FireEngineModel> fireEngines = (List<FireEngineModel>) model.get("fireEngines"); // create a worksheet Sheet sheet = workbook.createSheet("FireEngine Report"); // create style for header cells CellStyle style = workbook.createCellStyle(); Font font = workbook.createFont(); font.setFontName("Arial"); style.setFillForegroundColor(HSSFColor.BLUE.index); style.setFillPattern(CellStyle.SOLID_FOREGROUND); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); font.setColor(HSSFColor.WHITE.index); style.setFont(font);/*from w w w . j a va 2 s . c o m*/ // create a new row in the worksheet Row headerRow = sheet.createRow(0); // create a new cell in the row Cell cell0 = headerRow.createCell(0); cell0.setCellValue("ID"); cell0.setCellStyle(style); // create a new cell in the row Cell cell1 = headerRow.createCell(1); cell1.setCellValue("Modell"); cell1.setCellStyle(style); // create a new cell in the row Cell cell2 = headerRow.createCell(2); cell2.setCellValue("Kennzeichen"); cell2.setCellStyle(style); // create a new cell in the row Cell cell3 = headerRow.createCell(3); cell3.setCellValue("Leistung"); cell3.setCellStyle(style); // create a new cell in the row Cell cell4 = headerRow.createCell(4); cell4.setCellValue("Baujahr"); cell4.setCellStyle(style); // create a new cell in the row Cell cell5 = headerRow.createCell(5); cell5.setCellValue("Aktiv"); cell5.setCellStyle(style); // create a new cell in the row Cell cell6 = headerRow.createCell(6); cell6.setCellValue("Funktion"); cell6.setCellStyle(style); // create a new cell in the row Cell cell7 = headerRow.createCell(7); cell7.setCellValue("Feuerwehr"); cell7.setCellStyle(style); // create multiple rows with fireEngines data int rowNum = 1; for (FireEngineModel fireEngine : fireEngines) { // create the row data Row row = sheet.createRow(rowNum++); row.createCell(0).setCellValue(fireEngine.getId()); row.createCell(1).setCellValue(fireEngine.getModel()); row.createCell(2).setCellValue(fireEngine.getLicensePlate()); row.createCell(3).setCellValue(fireEngine.getPerformance()); row.createCell(4).setCellValue(fireEngine.getBuildYear()); row.createCell(5).setCellValue(fireEngine.getActive()); row.createCell(6).setCellValue(fireEngine.getAbbreviation().getAbbreviation()); row.createCell(7).setCellValue(fireEngine.getFireBrigade().getName()); } // adjust column width to fit the content sheet.autoSizeColumn((short) 0); sheet.autoSizeColumn((short) 1); sheet.autoSizeColumn((short) 2); sheet.autoSizeColumn((short) 3); sheet.autoSizeColumn((short) 4); sheet.autoSizeColumn((short) 5); sheet.autoSizeColumn((short) 6); sheet.autoSizeColumn((short) 7); }
From source file:at.spardat.xma.mdl.grid.GridPOIAdapter.java
License:Open Source License
/** * Transfers the spreadsheet data from the POI <code>HSSFWorkbook</code> into the <code>IGridWMServer</code>. * Only the sheet on the given sheetIndex is copied. // w w w .ja v a 2s . c o m * @param igrid the XMA model where to copy the data * @param book the POI represntation of the data * @param sheetIndex the index of the sheet to copy * @return a list containing all SysExceptions describing problems with individual cell formulas or ranges */ public static List poi2xma(IGridWM igrid, HSSFWorkbook book, int sheetIndex) { GridWM grid = (GridWM) igrid; try { List errorList = new ArrayList(); grid.setSheetName(book.getSheetName(sheetIndex)); grid.colors.clear(); grid.initBuildInColors(); short ic = GridWM.HSSF_FIRST_COLOR_INDEX; HSSFPalette palette = book.getCustomPalette(); for (HSSFColor color = palette.getColor(ic); ic < 64 && color != null; color = palette.getColor(++ic)) { grid.colors.add(ic, new GridColor(color.getTriplet())); } grid.fonts.clear(); int numFonts = book.getNumberOfFonts(); if (numFonts > 4) { // adjust for "There is no 4" see code of org.apache.poi.hssf.model.Workbook.getFontRecordAt() numFonts += 1; } for (short i = 0; i < numFonts; i++) { HSSFFont font = book.getFontAt(i); byte fontstyle = GridFont.FONT_NORML; if (font.getBoldweight() >= HSSFFont.BOLDWEIGHT_BOLD) fontstyle |= GridFont.FONT_BOLD; if (font.getItalic()) fontstyle |= GridFont.FONT_ITALIC; grid.fonts.add(i, new GridFont(font.getFontName(), fontstyle, font.getColor())); } grid.styles.clear(); for (short i = 0, numStyles = book.getNumCellStyles(); i < numStyles; i++) { HSSFCellStyle style = book.getCellStyleAt(i); grid.styles.add(i, new GridCellStyle(style.getFontIndex(), style.getFillForegroundColor())); } grid.namedRanges.clear(); for (int i = 0, numRanges = book.getNumberOfNames(); i < numRanges; i++) { HSSFName name = book.getNameAt(i); String rangeName = name.getNameName(); String rangeRef = null; try { // ranges not defined but referenced by formulas have a name but no reference in HSSF rangeRef = name.getReference(); } catch (Exception exc) { errorList.add(new SysException(exc, ((GridWM) grid).getMessage("inconsistentRange", rangeName)) .setCode(GridWM.CODE_inconsistentRange)); } if (rangeRef != null) { try { GridRange range = grid.getJeksDelegate().toRange(rangeRef); range.setKey(rangeName); grid.namedRanges.put(rangeName, range); } catch (Exception exc) { errorList.add(new SysException(exc, ((GridWM) grid).getMessage("unsupportedReference", rangeName, rangeRef)) .setCode(GridWM.CODE_unsupportedReference)); } } } grid.rows.clear(); grid.cols.clear(); grid.cells.clear(); grid.delegate = new GridJeksDelegate(grid); HSSFSheet sheet = book.getSheetAt(sheetIndex); int firstColNum = Integer.MAX_VALUE; int lastColNum = Integer.MIN_VALUE; for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) { HSSFRow row = sheet.getRow(i); if (row == null) continue; if (row.getFirstCellNum() >= 0) firstColNum = Math.min(firstColNum, row.getFirstCellNum()); lastColNum = Math.max(lastColNum, row.getLastCellNum()); if (lastColNum > 255) lastColNum = 255; for (short j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) { HSSFCell hssfcell = row.getCell(j); if (hssfcell == null) continue; GridCell gridcell = grid.getOrCreateCell(i, j); switch (hssfcell.getCellType()) { case HSSFCell.CELL_TYPE_BLANK: break; case HSSFCell.CELL_TYPE_BOOLEAN: gridcell.setValue(hssfcell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: // TODO: recherche error text byte errorCode = hssfcell.getErrorCellValue(); // gridcell.setValue(errorCode); gridcell.setValue("#ERROR"); errorList.add(new SysException(((GridWM) grid).getMessage("errorRecord", grid.getJeksDelegate().toExcelRef(i, j), Byte.toString(errorCode))) .setCode(GridWM.CODE_errorRecord)); break; case HSSFCell.CELL_TYPE_FORMULA: String formula = null; try { formula = hssfcell.getCellFormula(); gridcell.setFormula(formula); } catch (SysException e) { if (formula != null) gridcell.setValue("=" + formula); //set it as text without interpretation errorList.add(e); } break; case HSSFCell.CELL_TYPE_NUMERIC: if (isDateCell(book, hssfcell)) { gridcell.setValue(hssfcell.getDateCellValue()); } else { gridcell.setValue(hssfcell.getNumericCellValue()); } break; case HSSFCell.CELL_TYPE_STRING: gridcell.setValue(hssfcell.getStringCellValue()); break; default: throw new SysException("unknown cell type " + hssfcell.getCellType()); } gridcell.setEditable(!hssfcell.getCellStyle().getLocked()); gridcell.setStyle(hssfcell.getCellStyle().getIndex()); } } final int scalefactor = 256 / 7; // empirically testet // int width = sheet.getDefaultColumnWidth(); // returns nonsense // width = width/scalefactor; // grid.setDefaultColumnWidth(width); for (short i = (short) firstColNum; i <= lastColNum; i++) { int width = sheet.getColumnWidth(i); width = width / scalefactor; grid.getOrCreateColumn(i).setWidth(width); } if (firstColNum == Integer.MAX_VALUE) firstColNum = 0; if (lastColNum == Integer.MIN_VALUE) lastColNum = 0; grid.setMaxRange( new GridRange(grid, sheet.getFirstRowNum(), firstColNum, sheet.getLastRowNum(), lastColNum)); grid.setVisibleRange(grid.getMaxRange()); return errorList; } finally { grid.handle(grid.new GridReloadEvent()); } }
From source file:be.vds.jtbdive.client.view.core.dive.profile.DiveProfileExcelParser.java
License:Open Source License
public void export(Date date, DiveProfile diveProfile, File file) throws IOException { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(UIAgent.getInstance().getExportFormatDateHoursFull().format(date)); HSSFCellStyle cellStyle = wb.createCellStyle(); HSSFFont font = wb.createFont();/* w w w . j a v a 2 s . co m*/ font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); cellStyle.setFont(font); int rowIndex = 0; int colIndex = 0; HSSFRow row = sheet.createRow(rowIndex++); HSSFCell cell = row.createCell(colIndex++); cell.setCellValue("Time (s)"); cell.setCellStyle(cellStyle); cell = row.createCell(colIndex++); cell.setCellValue("Depth (m)"); cell.setCellStyle(cellStyle); cell = row.createCell(colIndex++); cell.setCellValue("Ascent Warning"); cell.setCellStyle(cellStyle); cell = row.createCell(colIndex++); cell.setCellValue("Bottom Time Warning"); cell.setCellStyle(cellStyle); cell = row.createCell(colIndex++); cell.setCellValue("Deco Ceiling Warning"); cell.setCellStyle(cellStyle); cell = row.createCell(colIndex++); cell.setCellValue("Deco Entry"); cell.setCellStyle(cellStyle); Map<Double, Double> entries = diveProfile.getDepthEntries(); List<Double> seconds = new ArrayList<Double>(entries.keySet()); Collections.sort(seconds); for (Double time : seconds) { colIndex = 0; row = sheet.createRow(rowIndex++); row.createCell(colIndex++, HSSFCell.CELL_TYPE_NUMERIC).setCellValue(time); row.createCell(colIndex++, HSSFCell.CELL_TYPE_NUMERIC).setCellValue(entries.get(time)); row.createCell(colIndex++, HSSFCell.CELL_TYPE_BOOLEAN) .setCellValue(diveProfile.getAscentWarnings().contains(time)); row.createCell(colIndex++, HSSFCell.CELL_TYPE_BOOLEAN) .setCellValue(diveProfile.getRemainingBottomTimeWarnings().contains(time)); row.createCell(colIndex++, HSSFCell.CELL_TYPE_BOOLEAN) .setCellValue(diveProfile.getDecoCeilingWarnings().contains(time)); row.createCell(colIndex++, HSSFCell.CELL_TYPE_BOOLEAN) .setCellValue(diveProfile.getDecoEntries().contains(time)); } for (int i = 0; i <= colIndex; i++) { sheet.autoSizeColumn(i); } FileOutputStream fileOut = new FileOutputStream(file); wb.write(fileOut); fileOut.close(); }
From source file:binky.reportrunner.engine.renderers.exporters.TabbedXLSExporter.java
License:Open Source License
@SuppressWarnings("deprecation") @Override//from www . ja va 2s . c o 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// w w w . ja v a 2 s .com 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:bs.global.util.ExcelFactory.java
public ExcelFactory(ResultSet resultSet, FormatType[] formatTypes, String sheetName) { workbook = new HSSFWorkbook(); this.resultSet = resultSet; sheet = workbook.createSheet(sheetName); boldFont = workbook.createFont();// w w w .j a v a 2 s. c o m boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); format = workbook.createDataFormat(); this.formatTypes = formatTypes; }
From source file:campmanager.CampUI.java
public void exportToExcel() { JFrame parentFrame = new JFrame(); File fileToSave = null;/*from w ww .jav a 2 s. co m*/ ; JFileChooser fileChooser = new JFileChooser(); fileChooser.setDialogTitle("Export to Excel"); int userSelection = fileChooser.showSaveDialog(parentFrame); if (userSelection == JFileChooser.APPROVE_OPTION) { fileToSave = fileChooser.getSelectedFile(); // System.out.println("Save as file: " + fileToSave.getAbsolutePath()); } if (fileToSave != null) { String fileName = fileToSave.getAbsolutePath(); if (!fileName.endsWith(".xls")) fileName += ".xls"; try { DefaultTableModel dtm = (DefaultTableModel) jTable_records.getModel(); Workbook wb = new HSSFWorkbook(); CreationHelper createhelper = wb.getCreationHelper(); Sheet sheet = wb.createSheet("new sheet"); Row row = null; Cell cell = null; row = sheet.createRow(0); HSSFFont font = (HSSFFont) wb.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); HSSFCellStyle style = (HSSFCellStyle) wb.createCellStyle(); style.setFont(font); for (int t = 0; t < dtm.getColumnCount() - 1; t++) { cell = row.createCell(t); cell.setCellStyle(style); cell.setCellValue(dtm.getColumnName(t)); } HSSFCellStyle style_gray = (HSSFCellStyle) wb.createCellStyle(); style_gray.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex()); // style_gray.setFillPattern(CellStyle.ALT_BARS); for (int i = 1; i <= dtm.getRowCount(); i++) { row = sheet.createRow(i); for (int j = 0; j < dtm.getColumnCount() - 1; j++) { cell = row.createCell(j); if (i % 2 == 0) cell.setCellStyle(style_gray); cell.setCellValue(dtm.getValueAt(i - 1, j).toString()); } } FileOutputStream out = new FileOutputStream(fileName); wb.write(out); out.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } }