List of usage examples for org.apache.poi.ss.usermodel Font setColor
void setColor(short color);
From source file:net.triptech.buildulator.view.ExcelTemplateView.java
License:Open Source License
@Override protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { DataGrid dataGrid = (DataGrid) model.get("dataGrid"); String sheetName = "Sheet 1"; if (StringUtils.isNotBlank(dataGrid.getTitle())) { sheetName = dataGrid.getTitle(); }/*from www . j a v a 2s .co m*/ HSSFSheet sheet = workbook.createSheet(sheetName); Font font = workbook.createFont(); font.setColor(HSSFColor.WHITE.index); HSSFCellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(HSSFColor.GREY_50_PERCENT.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setFont(font); int rowNum = 0; int maxColumnCount = 0; if (dataGrid.getHeaderFields().size() > 0) { HSSFRow header = sheet.createRow(rowNum); rowNum++; maxColumnCount = dataGrid.getHeaderFields().size(); int i = 0; for (String field : dataGrid.getHeaderFields()) { HSSFCell cell = header.createCell(i); cell.setCellValue(field); cell.setCellStyle(style); i++; } } for (int y = 0; y < dataGrid.getRowCount(); y++) { HSSFRow row = sheet.createRow(rowNum++); List<String> rowData = dataGrid.getRowFields(y); if (rowData.size() > maxColumnCount) { maxColumnCount = rowData.size(); } int x = 0; for (String data : rowData) { HSSFCell cell = row.createCell(x); try { double dbValue = Double.parseDouble(data); cell.setCellValue(dbValue); } catch (NumberFormatException nfe) { cell.setCellValue(data); } x++; } } for (int i = 0; i < maxColumnCount; i++) { sheet.autoSizeColumn(i); } }
From source file:nz.ac.auckland.abi.formatting.poi.ModelJSONToExcel.java
License:LGPL
/** * Create a library of cell styles// w w w .j a va 2 s .com */ private static Map<String, CellStyle> createStyles(Workbook wb) { Map<String, CellStyle> styles = new HashMap<String, CellStyle>(); CellStyle style; Font titleFont = wb.createFont(); titleFont.setFontHeightInPoints((short) 18); titleFont.setBoldweight(Font.BOLDWEIGHT_NORMAL); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFont(titleFont); styles.put("title", style); Font monthFont = wb.createFont(); monthFont.setFontHeightInPoints((short) 11); monthFont.setColor(IndexedColors.WHITE.getIndex()); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setFont(monthFont); style.setWrapText(true); styles.put("header", style); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setWrapText(true); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderTop(CellStyle.BORDER_THIN); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); styles.put("cell", style); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setDataFormat(wb.createDataFormat().getFormat("0.00")); styles.put("formula", style); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setDataFormat(wb.createDataFormat().getFormat("0.00")); styles.put("formula_2", style); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.RED.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setDataFormat(wb.createDataFormat().getFormat("0.00")); styles.put("MAX", style); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.BLUE.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setDataFormat(wb.createDataFormat().getFormat("0.00")); styles.put("MIN", style); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.GREEN.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setDataFormat(wb.createDataFormat().getFormat("0.00")); styles.put("AVERAGE", style); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setDataFormat(wb.createDataFormat().getFormat("0.00")); styles.put("STDEV", style); style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setDataFormat(wb.createDataFormat().getFormat("0.00")); styles.put("AVGSERIES", style); return styles; }
From source file:opn.greenwebs.HyperlinkExample.java
public static void main(String[] args) throws Exception { Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); CreationHelper createHelper = wb.getCreationHelper(); //cell style for hyperlinks //by default hyperlinks are blue and underlined CellStyle hlink_style = wb.createCellStyle(); Font hlink_font = wb.createFont(); hlink_font.setUnderline(Font.U_SINGLE); hlink_font.setColor(IndexedColors.BLUE.getIndex()); hlink_style.setFont(hlink_font);//ww w. j av a 2s .com Cell cell; Sheet sheet = wb.createSheet("Hyperlinks"); //URL cell = sheet.createRow(0).createCell((short) 0); cell.setCellValue("URL Link"); Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL); link.setAddress("http://poi.apache.org/"); cell.setHyperlink(link); cell.setCellStyle(hlink_style); //link to a file in the current directory cell = sheet.createRow(1).createCell((short) 0); cell.setCellValue("File Link"); link = createHelper.createHyperlink(Hyperlink.LINK_FILE); link.setAddress("link1.xls"); cell.setHyperlink(link); cell.setCellStyle(hlink_style); //e-mail link cell = sheet.createRow(2).createCell((short) 0); cell.setCellValue("Email Link"); link = createHelper.createHyperlink(Hyperlink.LINK_EMAIL); //note, if subject contains white spaces, make sure they are url-encoded link.setAddress("mailto:poi@apache.org?subject=Hyperlinks"); cell.setHyperlink(link); cell.setCellStyle(hlink_style); //link to a place in this workbook //create a target sheet and cell Sheet sheet2 = wb.createSheet("Target Sheet"); sheet2.createRow(0).createCell((short) 0).setCellValue("Target Cell"); cell = sheet.createRow(3).createCell((short) 0); cell.setCellValue("Worksheet Link"); Hyperlink link2 = createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT); link2.setAddress("'Target Sheet'!A1"); cell.setHyperlink(link2); cell.setCellStyle(hlink_style); FileOutputStream out = new FileOutputStream("hyperinks.xlsx"); wb.write(out); out.close(); }
From source file:org.alanwilliamson.openbd.plugin.spreadsheet.SpreadSheetFormatOptions.java
License:Open Source License
public static Font createCommentFont(Workbook workbook, cfStructData _struct) throws Exception { Font font = workbook.createFont(); if (_struct.containsKey("bold")) { if (_struct.getData("bold").getBoolean()) font.setBoldweight(Font.BOLDWEIGHT_BOLD); else// ww w.j av a 2s . c o m font.setBoldweight(Font.BOLDWEIGHT_NORMAL); } if (_struct.containsKey("color")) { String v = _struct.getData("color").getString(); Short s = lookup_colors.get(v); if (s == null) { throw new Exception("invalid parameter for 'color' (" + v + ")"); } else font.setColor(s); } if (_struct.containsKey("font")) { font.setFontName(_struct.getData("font").getString()); } if (_struct.containsKey("italic")) { font.setItalic(_struct.getData("italic").getBoolean()); } if (_struct.containsKey("strikeout")) { font.setStrikeout(_struct.getData("strikeout").getBoolean()); } if (_struct.containsKey("underline")) { font.setUnderline((byte) _struct.getData("underline").getInt()); } if (_struct.containsKey("size")) { font.setFontHeightInPoints((short) _struct.getData("size").getInt()); } return font; }
From source file:org.alanwilliamson.openbd.plugin.spreadsheet.SpreadSheetFormatOptions.java
License:Open Source License
public static CellStyle createCellStyle(Workbook workbook, cfStructData _struct) throws Exception { CellStyle style = workbook.createCellStyle(); if (_struct.containsKey("alignment")) { String v = _struct.getData("alignment").getString(); Short s = lookup_alignment.get(v); if (s == null) { throw new Exception("invalid parameter for 'alignment' (" + v + ")"); } else//from w ww. j a va 2 s .co m style.setAlignment(s); } if (_struct.containsKey("bottomborder")) { String v = _struct.getData("bottomborder").getString(); Short s = lookup_border.get(v); if (s == null) { throw new Exception("invalid parameter for 'bottomborder' (" + v + ")"); } else style.setBorderBottom(s); } if (_struct.containsKey("topborder")) { String v = _struct.getData("topborder").getString(); Short s = lookup_border.get(v); if (s == null) { throw new Exception("invalid parameter for 'topborder' (" + v + ")"); } else style.setBorderTop(s); } if (_struct.containsKey("leftborder")) { String v = _struct.getData("leftborder").getString(); Short s = lookup_border.get(v); if (s == null) { throw new Exception("invalid parameter for 'leftborder' (" + v + ")"); } else style.setBorderLeft(s); } if (_struct.containsKey("rightborder")) { String v = _struct.getData("rightborder").getString(); Short s = lookup_border.get(v); if (s == null) { throw new Exception("invalid parameter for 'rightborder' (" + v + ")"); } else style.setBorderRight(s); } if (_struct.containsKey("bottombordercolor")) { String v = _struct.getData("bottombordercolor").getString(); Short s = lookup_colors.get(v); if (s == null) { throw new Exception("invalid parameter for 'bottombordercolor' (" + v + ")"); } else style.setBottomBorderColor(s); } if (_struct.containsKey("topbordercolor")) { String v = _struct.getData("topbordercolor").getString(); Short s = lookup_colors.get(v); if (s == null) { throw new Exception("invalid parameter for 'topbordercolor' (" + v + ")"); } else style.setTopBorderColor(s); } if (_struct.containsKey("leftbordercolor")) { String v = _struct.getData("leftbordercolor").getString(); Short s = lookup_colors.get(v); if (s == null) { throw new Exception("invalid parameter for 'leftbordercolor' (" + v + ")"); } else style.setLeftBorderColor(s); } if (_struct.containsKey("rightbordercolor")) { String v = _struct.getData("rightbordercolor").getString(); Short s = lookup_colors.get(v); if (s == null) { throw new Exception("invalid parameter for 'rightbordercolor' (" + v + ")"); } else style.setRightBorderColor(s); } if (_struct.containsKey("fillpattern")) { String v = _struct.getData("fillpattern").getString(); Short s = lookup_fillpatten.get(v); if (s == null) { throw new Exception("invalid parameter for 'fillpattern' (" + v + ")"); } else style.setFillPattern(s); } if (_struct.containsKey("fgcolor")) { String v = _struct.getData("fgcolor").getString(); Short s = lookup_colors.get(v); if (s == null) { throw new Exception("invalid parameter for 'fgcolor' (" + v + ")"); } else style.setFillForegroundColor(s); } if (_struct.containsKey("bgcolor")) { String v = _struct.getData("bgcolor").getString(); Short s = lookup_colors.get(v); if (s == null) { throw new Exception("invalid parameter for 'bgcolor' (" + v + ")"); } else style.setFillBackgroundColor(s); } if (_struct.containsKey("textwrap")) { Boolean b = _struct.getData("textwrap").getBoolean(); style.setWrapText(b); } if (_struct.containsKey("hidden")) { Boolean b = _struct.getData("hidden").getBoolean(); style.setHidden(b); } if (_struct.containsKey("locked")) { Boolean b = _struct.getData("locked").getBoolean(); style.setLocked(b); } if (_struct.containsKey("indent")) { style.setIndention((short) _struct.getData("indent").getInt()); } if (_struct.containsKey("rotation")) { style.setRotation((short) _struct.getData("rotation").getInt()); } if (_struct.containsKey("dateformat")) { style.setDataFormat(workbook.createDataFormat().getFormat(_struct.getData("dateformat").getString())); } // Manage the fonts Font f = workbook.createFont(); if (_struct.containsKey("strikeout")) { f.setStrikeout(true); } if (_struct.containsKey("bold")) { Boolean b = _struct.getData("bold").getBoolean(); f.setBoldweight(b ? Font.BOLDWEIGHT_BOLD : Font.BOLDWEIGHT_NORMAL); } if (_struct.containsKey("underline")) { String v = _struct.getData("underline").getString(); Byte b = lookup_underline.get(v); if (b == null) { throw new Exception("invalid parameter for 'underline' (" + v + ")"); } else f.setUnderline(b); } if (_struct.containsKey("color")) { String v = _struct.getData("color").getString(); Short s = lookup_colors.get(v); if (s == null) { throw new Exception("invalid parameter for 'color' (" + v + ")"); } else f.setColor(s); } if (_struct.containsKey("fontsize")) { int s = _struct.getData("fontsize").getInt(); f.setFontHeightInPoints((short) s); } if (_struct.containsKey("font")) { f.setFontName(_struct.getData("font").getString()); } style.setFont(f); return style; }
From source file:org.apache.tika.eval.reports.XLSXHREFFormatter.java
License:Apache License
@Override public void reset(XSSFWorkbook workbook) { this.workbook = workbook; style = workbook.createCellStyle();//from w w w . j a v a2 s. co m Font hlinkFont = workbook.createFont(); hlinkFont.setUnderline(Font.U_SINGLE); hlinkFont.setColor(IndexedColors.BLUE.getIndex()); style.setFont(hlinkFont); links = 0; }
From source file:org.betaconceptframework.astroboa.commons.excelbuilder.WorkbookBuilder.java
License:Open Source License
private void initializeDefaultCellStyles() { // create cell styles for property values and property names propertyNameCellStyle = workbook.createCellStyle(); stringPropertyValueCellStyle = workbook.createCellStyle(); integerPropertyValueCellStyle = workbook.createCellStyle(); doublePropertyValueCellStyle = workbook.createCellStyle(); datePropertyValueCellStyle = workbook.createCellStyle(); dateTimePropertyValueCellStyle = workbook.createCellStyle(); // create 2 fonts objects Font propertyNameFont = workbook.createFont(); Font propertyValueFont = workbook.createFont(); // Set propertyNameFont to 14 point type, blue and bold propertyNameFont.setFontHeightInPoints((short) 14); propertyNameFont.setColor(IndexedColors.BLUE.getIndex()); propertyNameFont.setBoldweight(Font.BOLDWEIGHT_BOLD); // Set propertyValueFont to 10 point type, black propertyValueFont.setFontHeightInPoints((short) 10); propertyValueFont.setColor(IndexedColors.BLACK.getIndex()); // Set cell style and formatting propertyNameCellStyle.setFont(propertyNameFont); propertyNameCellStyle.setDataFormat(dataFormat.getFormat("text")); stringPropertyValueCellStyle.setFont(propertyValueFont); stringPropertyValueCellStyle.setDataFormat(dataFormat.getFormat("text")); stringPropertyValueCellStyle.setWrapText(true); integerPropertyValueCellStyle.setFont(propertyValueFont); integerPropertyValueCellStyle.setWrapText(true); //integerPropertyValueCellStyle.setDataFormat(dataFormat.getFormat("")); doublePropertyValueCellStyle.setFont(propertyValueFont); doublePropertyValueCellStyle.setDataFormat(dataFormat.getFormat("#,##0.000")); doublePropertyValueCellStyle.setWrapText(true); datePropertyValueCellStyle.setFont(propertyValueFont); datePropertyValueCellStyle.setDataFormat(dataFormat.getFormat("dd/mm/yyyy")); datePropertyValueCellStyle.setWrapText(true); dateTimePropertyValueCellStyle.setFont(propertyValueFont); dateTimePropertyValueCellStyle.setDataFormat(dataFormat.getFormat("dd/mm/yyyy HH:mm")); dateTimePropertyValueCellStyle.setWrapText(true); // Set the other cell style and formatting //cs2.setBorderBottom(cs2.BORDER_THIN); }
From source file:org.cerberus.service.export.ExportServiceFactory.java
License:Open Source License
private int createRow(String test, HashMap<String, List<TestCaseExecution>> executionsPerTestCase, Sheet sheet, int currentIndex, List<String> mapCountries) { int lastRow = currentIndex + executionsPerTestCase.size(); int current = currentIndex; TreeMap<String, List<TestCaseExecution>> sortedKeys = new TreeMap<String, List<TestCaseExecution>>( executionsPerTestCase);//from w ww . j a v a 2 s .c o m CellStyle wrapStyle = sheet.getColumnStyle(0); //Create new style wrapStyle.setWrapText(true); //Set wordwrap for (String testCaseKey : sortedKeys.keySet()) { List<String> browserEnvironment = new LinkedList<String>(); String application; String description; Row r = sheet.createRow(current); List<TestCaseExecution> executionList = executionsPerTestCase.get(testCaseKey); Cell testCell = r.createCell(0); testCell.setCellValue(test); testCell.setCellStyle(wrapStyle); r.createCell(1).setCellValue(testCaseKey); //gets the first object to retrieve the application - at least exists one test case execution if (executionList.isEmpty()) { application = "N/D"; description = "N/D"; } else { application = executionList.get(0).getApplication(); description = executionList.get(0).getTestCaseObj().getBehaviorOrValueExpected(); } //Sets the application and description r.createCell(2).setCellValue(application); r.createCell(3).setCellValue(description); int rowStartedTestCaseInfo = current; for (TestCaseExecution exec : executionList) { if (browserEnvironment.isEmpty()) { browserEnvironment.add(exec.getEnvironment() + "_" + exec.getBrowser()); r.createCell(4).setCellValue(exec.getEnvironment()); r.createCell(5).setCellValue(exec.getBrowser()); } else { int index = browserEnvironment.indexOf(exec.getEnvironment() + "_" + exec.getBrowser()); //Does not exist any information about browser and environment if (browserEnvironment.indexOf(exec.getEnvironment() + "_" + exec.getBrowser()) == -1) { //need to add another row with the same characteristics r = sheet.createRow(++current); r.createCell(0).setCellValue(test); r.createCell(1).setCellValue(testCaseKey); r.createCell(2).setCellValue(application); r.createCell(3).setCellValue(description); r.createCell(4).setCellValue(exec.getEnvironment()); r.createCell(5).setCellValue(exec.getBrowser()); browserEnvironment.add(exec.getEnvironment() + "_" + exec.getBrowser()); } else { //there is information about the browser and environment Row rowExisting = sheet.getRow(rowStartedTestCaseInfo + index); r = rowExisting; } } //TODO:FN tirar daqui estes valores int indexOfCountry = mapCountries.indexOf(exec.getCountry()) + 6; Cell executionResult = r.createCell(indexOfCountry); executionResult.setCellValue(exec.getControlStatus()); //Create hyperling CreationHelper createHelper = sheet.getWorkbook().getCreationHelper(); CellStyle hlinkstyle = sheet.getWorkbook().createCellStyle(); Font hlinkfont = sheet.getWorkbook().createFont(); hlinkfont.setUnderline(XSSFFont.U_SINGLE); hlinkfont.setColor(HSSFColor.BLUE.index); hlinkstyle.setFont(hlinkfont); Hyperlink link = (Hyperlink) createHelper.createHyperlink(Hyperlink.LINK_URL); link.setAddress("http://www.tutorialspoint.com/"); executionResult.setHyperlink((Hyperlink) link); executionResult.setCellStyle(hlinkstyle); } current++; } /*r.createCell(1).setCellValue(""); r.createCell(2).setCellValue(""); r.createCell(3).setCellValue(""); r.createCell(4).setCellValue(""); r.createCell(5).setCellValue(""); */ // for(TestCaseWithExecution exec : execution){ // // //r.createCell(2).setCellValue(exec.getDescription()); // //r.createCell(3).setCellValue(exec.getApplication()); // //r.createCell(4).setCellValue(exec.getEnvironment()); // //r.createCell(5).setCellValue(exec.getBrowser()); // int indexOfCountry = mapCountries.indexOf(exec.getCountry()) + 6; // r.createCell(indexOfCountry).setCellValue(exec.getControlStatus()); // //current++; // } //puts the test name in the first column /*r = sheet.getRow(currentIndex); r.getCell(0).setCellValue(test); */ /*CellRangeAddress range = new CellRangeAddress(currentIndex, lastRow, 0, 0); sheet.addMergedRegion(range);*/ return lastRow; }
From source file:org.cgiar.ccafs.ap.summaries.projects.xlsx.BaseXLS.java
License:Open Source License
/** * This method writes the title box into the given sheet. * //from ww w.ja va 2 s . c o m * @param sheet is the sheet where you want to write the title box. * @param text is the title of the report. */ public void writeTitleBox(Sheet sheet, String text) { XSSFDrawing draw = (XSSFDrawing) sheet.createDrawingPatriarch(); XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 1, 1, 1, 1, 3, 6); anchor.setAnchorType(2); XSSFTextBox textbox = draw.createTextbox(anchor); textbox.setFillColor(TEXTBOX_BACKGROUND_COLOR_RGB.getRed(), TEXTBOX_BACKGROUND_COLOR_RGB.getGreen(), TEXTBOX_BACKGROUND_COLOR_RGB.getBlue()); textbox.setVerticalAlignment(VerticalAlignment.CENTER); XSSFRichTextString stringX = new XSSFRichTextString(); Font font = workbook.createFont(); font.setFontHeightInPoints((short) 20); font.setFontName("Tahoma"); font.setColor(TEXTBOX_FONT_COLOR_INDEX); stringX.append(text); stringX.applyFont(font); textbox.setText(stringX); }
From source file:org.eclipse.nebula.widgets.nattable.extension.poi.HSSFExcelExporter.java
License:Open Source License
protected void setFontColor(Font xlFont, Color swtColor) { xlFont.setColor(getColorIndex(swtColor)); }