List of usage examples for org.apache.poi.ss.usermodel Font setCharSet
void setCharSet(int charset);
From source file:jdbreport.model.io.xls.poi.Excel2003Writer.java
License:Apache License
private Font getFont(short fontIndex, AttributeSet attributeSet, Workbook wb) { Font font = null; String family = null;/*from ww w.j a v a2 s . co m*/ String sizeStr = null; short color = 0; boolean bold = false; boolean italic = false; boolean underline = false; boolean line_through = false; boolean sub = false; boolean sup = false; Enumeration<?> en = attributeSet.getAttributeNames(); while (en.hasMoreElements()) { Object key = en.nextElement(); String name = key.toString(); String attribute = attributeSet.getAttribute(key).toString(); switch (name) { case "font-weight": bold = attribute.equals("bold"); break; case "font-style": italic = attribute.equals("italic"); break; case "text-decoration": if (attribute.equals("underline")) { underline = true; } else if (attribute.equals("line-through")) { line_through = true; } break; case "font-family": family = attribute; break; case "font-size": sizeStr = attribute; break; case "color": Color fontColor = Utils.colorByName(attribute); if (fontColor == null) { try { fontColor = Utils.stringToColor(attribute); } catch (Exception ignored) { } } if (fontColor != null) { color = colorToIndex(wb, fontColor); } break; case "vertical-align": if (attribute.equals("sub")) { sub = true; } else if (attribute.equals("sup")) { sup = true; } break; } } if (family != null || bold || italic || underline || line_through || color > 0 || sizeStr != null || sub || sup) { font = wb.createFont(); if (fontIndex > 0) { Font parentFont = wb.getFontAt(fontIndex); if (parentFont != null) { font.setBold(parentFont.getBold()); font.setColor(parentFont.getColor()); try { font.setCharSet(parentFont.getCharSet()); } catch (Throwable ignored) { } font.setFontHeight(parentFont.getFontHeight()); font.setFontName(parentFont.getFontName()); font.setItalic(parentFont.getItalic()); font.setStrikeout(parentFont.getStrikeout()); font.setUnderline(parentFont.getUnderline()); font.setTypeOffset(parentFont.getTypeOffset()); } } if (family != null) { font.setFontName(family); } if (bold) { font.setBold(true); } if (italic) { font.setItalic(italic); } if (underline) { font.setUnderline(Font.U_SINGLE); } if (line_through) { font.setStrikeout(line_through); } if (color > 0) { font.setColor(color); } if (sizeStr != null) { short size = (short) Float.parseFloat(sizeStr); if (sizeStr.charAt(0) == '+' || sizeStr.charAt(0) == '-') { size = (short) (Content.pointToSize(font.getFontHeightInPoints()) + size); } font.setFontHeightInPoints(Content.sizeToPoints(size)); } if (sup) { font.setTypeOffset(Font.SS_SUPER); } else if (sub) { font.setTypeOffset(Font.SS_SUB); } } return font; }
From source file:org.pentaho.di.trans.steps.excelwriter.ExcelWriterStep.java
License:Apache License
void writeField(Object v, ValueMetaInterface vMeta, ExcelWriterStepField excelField, Row xlsRow, int posX, Object[] row, int fieldNr, boolean isTitle) throws KettleException { try {//from ww w.j a va 2 s. co m boolean cellExisted = true; // get the cell Cell cell = xlsRow.getCell(posX); if (cell == null) { cellExisted = false; cell = xlsRow.createCell(posX); } // if cell existed and existing cell's styles should not be changed, don't if (!(cellExisted && meta.isLeaveExistingStylesUnchanged())) { // if the style of this field is cached, reuse it if (!isTitle && data.getCachedStyle(fieldNr) != null) { cell.setCellStyle(data.getCachedStyle(fieldNr)); } else { // apply style if requested if (excelField != null) { // determine correct cell for title or data rows String styleRef = null; if (!isTitle && !Utils.isEmpty(excelField.getStyleCell())) { styleRef = excelField.getStyleCell(); } else if (isTitle && !Utils.isEmpty(excelField.getTitleStyleCell())) { styleRef = excelField.getTitleStyleCell(); } if (styleRef != null) { Cell styleCell = getCellFromReference(styleRef); if (styleCell != null && cell != styleCell) { cell.setCellStyle(styleCell.getCellStyle()); } } } // set cell format as specified, specific format overrides cell specification if (!isTitle && excelField != null && !Utils.isEmpty(excelField.getFormat()) && !excelField.getFormat().startsWith("Image")) { setDataFormat(excelField.getFormat(), cell); } // cache it for later runs if (!isTitle) { data.cacheStyle(fieldNr, cell.getCellStyle()); } } } // create link on cell if requested if (!isTitle && excelField != null && data.linkfieldnrs[fieldNr] >= 0) { String link = data.inputRowMeta.getValueMeta(data.linkfieldnrs[fieldNr]) .getString(row[data.linkfieldnrs[fieldNr]]); if (!Utils.isEmpty(link)) { CreationHelper ch = data.wb.getCreationHelper(); // set the link on the cell depending on link type Hyperlink hyperLink = null; if (link.startsWith("http:") || link.startsWith("https:") || link.startsWith("ftp:")) { hyperLink = ch.createHyperlink(HyperlinkType.URL); hyperLink.setLabel("URL Link"); } else if (link.startsWith("mailto:")) { hyperLink = ch.createHyperlink(HyperlinkType.EMAIL); hyperLink.setLabel("Email Link"); } else if (link.startsWith("'")) { hyperLink = ch.createHyperlink(HyperlinkType.DOCUMENT); hyperLink.setLabel("Link within this document"); } else { hyperLink = ch.createHyperlink(HyperlinkType.FILE); hyperLink.setLabel("Link to a file"); } hyperLink.setAddress(link); cell.setHyperlink(hyperLink); // if cell existed and existing cell's styles should not be changed, don't if (!(cellExisted && meta.isLeaveExistingStylesUnchanged())) { if (data.getCachedLinkStyle(fieldNr) != null) { cell.setCellStyle(data.getCachedLinkStyle(fieldNr)); } else { // CellStyle style = cell.getCellStyle(); Font origFont = data.wb.getFontAt(cell.getCellStyle().getFontIndex()); Font hlink_font = data.wb.createFont(); // reporduce original font characteristics hlink_font.setBold(origFont.getBold()); hlink_font.setCharSet(origFont.getCharSet()); hlink_font.setFontHeight(origFont.getFontHeight()); hlink_font.setFontName(origFont.getFontName()); hlink_font.setItalic(origFont.getItalic()); hlink_font.setStrikeout(origFont.getStrikeout()); hlink_font.setTypeOffset(origFont.getTypeOffset()); // make it blue and underlined hlink_font.setUnderline(Font.U_SINGLE); hlink_font.setColor(IndexedColors.BLUE.getIndex()); CellStyle style = cell.getCellStyle(); style.setFont(hlink_font); cell.setCellStyle(style); data.cacheLinkStyle(fieldNr, cell.getCellStyle()); } } } } // create comment on cell if requrested if (!isTitle && excelField != null && data.commentfieldnrs[fieldNr] >= 0 && data.wb instanceof XSSFWorkbook) { String comment = data.inputRowMeta.getValueMeta(data.commentfieldnrs[fieldNr]) .getString(row[data.commentfieldnrs[fieldNr]]); if (!Utils.isEmpty(comment)) { String author = data.commentauthorfieldnrs[fieldNr] >= 0 ? data.inputRowMeta.getValueMeta(data.commentauthorfieldnrs[fieldNr]).getString( row[data.commentauthorfieldnrs[fieldNr]]) : "Kettle PDI"; cell.setCellComment(createCellComment(author, comment)); } } // cell is getting a formula value or static content if (!isTitle && excelField != null && excelField.isFormula()) { // formula case cell.setCellFormula(vMeta.getString(v)); } else { // static content case switch (vMeta.getType()) { case ValueMetaInterface.TYPE_DATE: if (v != null && vMeta.getDate(v) != null) { cell.setCellValue(vMeta.getDate(v)); } break; case ValueMetaInterface.TYPE_BOOLEAN: if (v != null) { cell.setCellValue(vMeta.getBoolean(v)); } break; case ValueMetaInterface.TYPE_STRING: case ValueMetaInterface.TYPE_BINARY: if (v != null) { cell.setCellValue(vMeta.getString(v)); } break; case ValueMetaInterface.TYPE_BIGNUMBER: case ValueMetaInterface.TYPE_NUMBER: case ValueMetaInterface.TYPE_INTEGER: if (v != null) { cell.setCellValue(vMeta.getNumber(v)); } break; default: break; } } } catch (Exception e) { logError("Error writing field (" + data.posX + "," + data.posY + ") : " + e.toString()); logError(Const.getStackTracker(e)); throw new KettleException(e); } }