List of usage examples for org.apache.poi.ss.usermodel RichTextString applyFont
void applyFont(int startIndex, int endIndex, Font font);
From source file:com.dua3.meja.model.poi.PoiCell.java
License:Apache License
@Override public Cell set(RichText s) { Object old = get();//from w w w. j a va 2 s .c om RichTextString richText = workbook.createRichTextString(s.toString()); for (Run run : s) { PoiFont font = getWorkbook().getPoiFont(getCellStyle().getFont(), run.getStyle()); richText.applyFont(run.getStart(), run.getEnd(), font.getPoiFont()); } poiCell.setCellValue(richText); updateRow(); getSheet().cellValueChanged(this, old, s); return this; }
From source file:com.helger.poi.excel.ExcelReadUtilsTest.java
License:Apache License
@Test public void testGetCellValueObject() { for (final EExcelVersion eVersion : EExcelVersion.values()) { final Workbook aWB = eVersion.createWorkbook(); final Sheet aSheet = aWB.createSheet(); final Row aRow = aSheet.createRow(0); final Cell aCell = aRow.createCell(0); // boolean aCell.setCellValue(true);/*from w ww .j av a 2 s . c om*/ assertEquals(Boolean.TRUE, ExcelReadUtils.getCellValueObject(aCell)); // int aCell.setCellValue(4711); assertEquals(Integer.valueOf(4711), ExcelReadUtils.getCellValueObject(aCell)); // long aCell.setCellValue(Long.MAX_VALUE); assertEquals(Long.valueOf(Long.MAX_VALUE), ExcelReadUtils.getCellValueObject(aCell)); // double aCell.setCellValue(3.14159); assertEquals(Double.valueOf(3.14159), ExcelReadUtils.getCellValueObject(aCell)); // String aCell.setCellValue("Anyhow"); assertEquals("Anyhow", ExcelReadUtils.getCellValueObject(aCell)); // Rich text string final Font aFont = aWB.createFont(); aFont.setItalic(true); final RichTextString aRTS = eVersion.createRichText("Anyhow"); aRTS.applyFont(1, 3, aFont); aCell.setCellValue(aRTS); assertEquals("Anyhow", ExcelReadUtils.getCellValueObject(aCell)); } }
From source file:net.algem.planning.export.PlanningExportService.java
License:Open Source License
/** * * @param p current schedule// ww w . ja va 2 s .co m * @param wb workbook * @return a formatted-string */ private RichTextString getLabel(ScheduleObject p, HSSFWorkbook wb) { String header = p.getStart() + "-" + p.getEnd() + "\n"; StringBuilder sb = new StringBuilder(); sb.append(header); switch (p.getType()) { case Schedule.COURSE: case Schedule.WORKSHOP: case Schedule.TRAINING: Course c = ((CourseSchedule) p).getCourse(); sb.append(c.getLabel() != null && c.getLabel().length() > 0 ? c.getLabel() : c.getTitle()); sb.append('\n'); sb.append(p.getPerson().getAbbrevFirstNameName()); if (p.getLength() > 30 && printMembers) { try { List<Person> members = planningService.getPersons(p.getId()); if (members.size() == 1) { Person per = members.get(0); sb.append('\n') .append(per.getNickName() != null && per.getNickName().length() > 0 ? per.getNickName() : per.getAbbrevFirstNameName()); } } catch (SQLException ex) { GemLogger.log(ex.getMessage()); } } break; default: sb.append(p.getScheduleLabel()); } String content = sb.toString(); HSSFFont bf = wb.createFont(); bf.setBold(true); HSSFFont nf = wb.createFont(); java.awt.Color textColor = colorizer.getTextColor(p); bf.setColor(getColorIndex(wb, textColor)); nf.setColor(getColorIndex(wb, textColor)); // RichTextString is used here to apply bold font to title RichTextString rts = new HSSFRichTextString(content); //int idx = content.indexOf("\n", content.indexOf("\n")+1); int idx = content.indexOf("\n") + 1; rts.applyFont(0, header.length(), bf); if (idx >= header.length()) {// some additional content was found rts.applyFont(header.length(), content.length(), nf); } return rts; }
From source file:org.pentaho.reporting.engine.classic.core.modules.output.table.xls.helper.ExcelTextExtractor.java
License:Open Source License
public static RichTextString computeRichText(final ExcelFontFactory fontFactory, final CreationHelper creationHelper, final String text, final ArrayList<RichTextFormat> buffer) { if (text.length() > 0) { if (text.length() < 32768) { // There's rich text. final RichTextString rtStr = creationHelper.createRichTextString(text); for (int i = 0; i < buffer.size(); i++) { final RichTextFormat o = buffer.get(i); final int position = o.getPosition(); final HSSFFontWrapper font = o.getFont(); if (i == (buffer.size() - 1)) { // Last element .. rtStr.applyFont(position, text.length(), fontFactory.getExcelFont(font)); } else { final RichTextFormat next = buffer.get(i + 1); rtStr.applyFont(position, next.getPosition(), fontFactory.getExcelFont(font)); }// w w w . j a va2s . co m } return rtStr; } else { ExcelTextExtractor.logger.warn( "Excel-Cells cannot contain text larger than 32.737 characters. Text will be clipped."); final String realText = text.substring(0, 32767); final RichTextString rtStr = creationHelper.createRichTextString(realText); for (int i = 0; i < buffer.size(); i++) { final RichTextFormat o = buffer.get(i); final int position = o.getPosition(); if (position >= 32767) { break; } final HSSFFontWrapper font = o.getFont(); if (i == (buffer.size() - 1)) { // Last element .. final int endPosition = Math.min(32767, text.length()); rtStr.applyFont(position, endPosition, fontFactory.getExcelFont(font)); } else { final RichTextFormat next = buffer.get(i + 1); final int endPosition = Math.min(32767, next.getPosition()); rtStr.applyFont(position, endPosition, fontFactory.getExcelFont(font)); } } return rtStr; } } return null; }
From source file:uk.co.spudsoft.birt.emitters.excel.handlers.CellContentHandler.java
License:Open Source License
/** * Finish processing for the current (real) cell. * @param element/*from ww w . j a v a2 s . c o m*/ * The element that signifies the end of the cell (this may not be an ICellContent object if the * cell is created for a label or text outside of a table). */ protected void endCellContent(HandlerState state, ICellContent birtCell, IContent element, Cell cell, Area area) { StyleManager sm = state.getSm(); StyleManagerUtils smu = state.getSmu(); BirtStyle birtCellStyle = null; if (birtCell != null) { birtCellStyle = new BirtStyle(birtCell); if (element != null) { // log.debug( "Overlaying style from ", element ); birtCellStyle.overlay(element); } } else if (element != null) { birtCellStyle = new BirtStyle(element); } else { birtCellStyle = new BirtStyle(state.getSm().getCssEngine()); } if (preferredAlignment != null) { birtCellStyle.setProperty(StyleConstants.STYLE_TEXT_ALIGN, preferredAlignment); } if (CSSConstants.CSS_TRANSPARENT_VALUE .equals(birtCellStyle.getString(StyleConstants.STYLE_BACKGROUND_COLOR))) { if (parent != null) { birtCellStyle.setProperty(StyleConstants.STYLE_BACKGROUND_COLOR, parent.getBackgroundColour()); } } if (hyperlinkUrl != null) { Hyperlink hyperlink = cell.getSheet().getWorkbook().getCreationHelper() .createHyperlink(Hyperlink.LINK_URL); hyperlink.setAddress(hyperlinkUrl); cell.setHyperlink(hyperlink); } if (hyperlinkBookmark != null) { Hyperlink hyperlink = cell.getSheet().getWorkbook().getCreationHelper() .createHyperlink(Hyperlink.LINK_DOCUMENT); hyperlink.setAddress(prepareName(hyperlinkBookmark)); cell.setHyperlink(hyperlink); } if (lastValue != null) { if (lastValue instanceof String) { String lastString = (String) lastValue; smu.correctFontColorIfBackground(birtCellStyle); for (RichTextRun run : richTextRuns) { run.font = smu.correctFontColorIfBackground(sm.getFontManager(), state.getWb(), birtCellStyle, run.font); } if (!richTextRuns.isEmpty()) { RichTextString rich = smu.createRichTextString(lastString); int runStart = richTextRuns.get(0).startIndex; Font lastFont = richTextRuns.get(0).font; for (int i = 0; i < richTextRuns.size(); ++i) { RichTextRun run = richTextRuns.get(i); log.debug("Run: ", run.startIndex, " font :", run.font); if (!lastFont.equals(run.font)) { log.debug("Applying ", runStart, " - ", run.startIndex); rich.applyFont(runStart, run.startIndex, lastFont); runStart = run.startIndex; lastFont = richTextRuns.get(i).font; } } log.debug("Finalising with ", runStart, " - ", lastString.length()); rich.applyFont(runStart, lastString.length(), lastFont); setCellContents(cell, rich); } else { setCellContents(cell, lastString); } if (lastString.contains("\n")) { if (!CSSConstants.CSS_NOWRAP_VALUE.equals(lastElement.getStyle().getWhiteSpace())) { birtCellStyle.setProperty(StyleConstants.STYLE_WHITE_SPACE, new StringValue(StringValue.CSS_STRING, CSSConstants.CSS_PRE_VALUE)); } } if (!richTextRuns.isEmpty()) { birtCellStyle.setProperty(StyleConstants.STYLE_VERTICAL_ALIGN, new StringValue(StringValue.CSS_STRING, CSSConstants.CSS_TOP_VALUE)); } if (preferredAlignment != null) { birtCellStyle.setProperty(StyleConstants.STYLE_TEXT_ALIGN, preferredAlignment); } } else { setCellContents(cell, lastValue); } } int colIndex = cell.getColumnIndex(); state.getSmu().applyAreaBordersToCell(state.areaBorders, cell, birtCellStyle, state.rowNum, colIndex); if ((birtCell != null) && ((birtCell.getColSpan() > 1) || (birtCell.getRowSpan() > 1))) { AreaBorders mergedRegionBorders = AreaBorders.createForMergedCells( state.rowNum + birtCell.getRowSpan() - 1, colIndex, colIndex + birtCell.getColSpan() - 1, state.rowNum, birtCellStyle); if (mergedRegionBorders != null) { state.insertBorderOverload(mergedRegionBorders); } } String customNumberFormat = EmitterServices.stringOption(state.getRenderOptions(), element, ExcelEmitter.CUSTOM_NUMBER_FORMAT, null); if (customNumberFormat != null) { StyleManagerUtils.setNumberFormat(birtCellStyle, ExcelEmitter.CUSTOM_NUMBER_FORMAT + customNumberFormat, null); } setCellStyle(sm, cell, birtCellStyle, lastValue); // Excel auto calculates the row height (if it isn't specified) as long as the cell isn't merged - if it is merged I have to do it if (((colSpan > 1) || (state.rowHasSpans(state.rowNum))) && ((lastValue instanceof String) || (lastValue instanceof RichTextString))) { int spannedRowAlgorithm = EmitterServices.integerOption(state.getRenderOptions(), element, ExcelEmitter.SPANNED_ROW_HEIGHT, ExcelEmitter.SPANNED_ROW_HEIGHT_SPREAD); Font defaultFont = state.getWb().getFontAt(cell.getCellStyle().getFontIndex()); double cellWidth = spanWidthMillimetres(state.currentSheet, cell.getColumnIndex(), cell.getColumnIndex() + colSpan - 1); float cellDesiredHeight = smu.calculateTextHeightPoints(cell.getStringCellValue(), defaultFont, cellWidth, richTextRuns); if (cellDesiredHeight > state.requiredRowHeightInPoints) { int rowSpan = birtCell.getRowSpan(); if (rowSpan < 2) { state.requiredRowHeightInPoints = cellDesiredHeight; } else { switch (spannedRowAlgorithm) { case ExcelEmitter.SPANNED_ROW_HEIGHT_FIRST: state.requiredRowHeightInPoints = cellDesiredHeight; break; case ExcelEmitter.SPANNED_ROW_HEIGHT_IGNORED: break; default: if (area != null) { area.setHeight(cellDesiredHeight); } } } } } // Adjust the required row height for any relevant areas based on what's left float rowSpanHeightRequirement = state.calculateRowSpanHeightRequirement(state.rowNum); if (rowSpanHeightRequirement > state.requiredRowHeightInPoints) { state.requiredRowHeightInPoints = rowSpanHeightRequirement; } if (EmitterServices.booleanOption(state.getRenderOptions(), element, ExcelEmitter.FREEZE_PANES, false)) { if (state.currentSheet.getPaneInformation() == null) { state.currentSheet.createFreezePane(state.colNum, state.rowNum); } } lastValue = null; lastElement = null; richTextRuns.clear(); }