private void endCellValue(String name) throws SAXException { // ensure size if (curCol >= curRowContent.length) { curRowContent = Arrays.copyOf(curRowContent, (int)(curCol * 1.5)); } if (CELL_VALUE_TAG.equals(name)) { switch (currentCellType) { case STRING: int idx = Integer.parseInt(currentCellValue); currentCellValue = new XSSFRichTextString(sst.getEntryAt(idx)).toString(); currentCellType = FieldType.EMPTY; break; } curRowContent[curCol] = currentCellValue; } else if (CELL_VALUE_TAG_1.equals(name)) { curRowContent[curCol] = currentCellValue; } }
break; case INLINESTR: result = new XSSFRichTextString(value.toString()).toString(); break; case SSTINDEX: try { final int index = Integer.parseInt(value); result = new XSSFRichTextString(sharedStringsTable.getEntryAt(index)).getString(); } catch (NumberFormatException e) { result = value;
/** * Applies a font to the specified characters of a string. * * @param startIndex The start index to apply the font to (inclusive) * @param endIndex The end index to apply to font to (exclusive) * @param font The index of the font to use. */ public void applyFont(int startIndex, int endIndex, Font font) { if (startIndex > endIndex) throw new IllegalArgumentException("Start index must be less than end index, but had " + startIndex + " and " + endIndex); if (startIndex < 0 || endIndex > length()) throw new IllegalArgumentException("Start and end index not in range, but had " + startIndex + " and " + endIndex); if (startIndex == endIndex) return; if(st.sizeOfRArray() == 0 && st.isSetT()) { //convert <t>string</t> into a text run: <r><t>string</t></r> st.addNewR().setT(st.getT()); st.unsetT(); } String text = getString(); XSSFFont xssfFont = (XSSFFont)font; TreeMap<Integer, CTRPrElt> formats = getFormatMap(st); CTRPrElt fmt = CTRPrElt.Factory.newInstance(); setRunAttributes(xssfFont.getCTFont(), fmt); applyFont(formats, startIndex, endIndex, fmt); CTRst newSt = buildCTRst(text, formats); st.set(newSt); }
/** * Creates a new XSSFRichTextString for you. */ @Override public XSSFRichTextString createRichTextString(String text) { XSSFRichTextString rt = new XSSFRichTextString(text); rt.setStylesTableReference(workbook.getStylesSource()); return rt; }
@Override protected RichTextString createRichTextString(String str) { return new XSSFRichTextString(str); }
case STRING: int sstIndex = Integer.parseInt(_cell.getV()); XSSFRichTextString rt = new XSSFRichTextString(_sharedStringSource.getEntryAt(sstIndex)); String text = rt.getString(); return Boolean.parseBoolean(text); case NUMERIC:
/** * 07版本复杂数据 * @param rich * @return */ private String getXSSFRichString(XSSFRichTextString rich) { int nums = rich.numFormattingRuns(); StringBuilder sb = new StringBuilder(); String text = rich.toString(); int currentIndex = 0, lastIndex = 0; for (int i = 1; i <= nums; i++) { sb.append("<span "); try { sb.append("class='font_" + getFontIndex(rich.getFontOfFormattingRun(i - 1))); sb.append("_"); sb.append(cssRandom); sb.append("'"); } catch (Exception e) { } sb.append(">"); currentIndex = rich.getIndexOfFormattingRun(i) == -1 ? text.length() : rich.getIndexOfFormattingRun(i); sb.append( XmlEscapers.xmlContentEscaper().escape(text.substring(lastIndex, currentIndex))); sb.append("</span>"); lastIndex = currentIndex; } return sb.toString(); }
XSSFRichTextString rt1 = new XSSFRichTextString("Apache POI is"); rt1.applyFont(plainArial); XSSFRichTextString rt2 = new XSSFRichTextString(" great!"); rt2.applyFont(boldArial); String text = rt2.getString(); cell1.setCellValue(rt1.append(text, boldArial));
HSSFCell hssfCell = row.createCell(idx); //rich text consists of two runs HSSFRichTextString richString = new HSSFRichTextString( "Hello, World!" ); richString.applyFont( 0, 6, font1 ); richString.applyFont( 6, 13, font2 ); hssfCell.setCellValue( richString ); XSSFRichTextString s1 = new XSSFRichTextString("Apache POI"); s1.applyFont(boldArial); cell1.setCellValue(s1);
/** * Returns the plain string representation. */ public String toString() { return getString(); }
XSSFRichTextString rt = new XSSFRichTextString("The quick brown fox"); rt.applyFont(0, 10, font1); font2.setUnderline(XSSFFont.U_DOUBLE); font2.setColor(new XSSFColor(new java.awt.Color(0, 255, 0), wb.getStylesSource().getIndexedColors())); rt.applyFont(10, 19, font2); rt.append(" Jumped over the lazy dog", font3);
str.setStylesTableReference(wb.getStylesSource()); if (str.numFormattingRuns() == 0) { CTRegularTextRun r = p.addNewR(); CTTextCharacterProperties rPr = r.addNewRPr(); rPr.setLang("en-US"); rPr.setSz(1100); r.setT(str.getString()); for (int i = 0; i < str.getCTRst().sizeOfRArray(); i++) { CTRElt lt = str.getCTRst().getRArray(i); CTRPrElt ltPr = lt.getRPr(); if (ltPr == null) {
for (int i = 0; i < cellText.numFormattingRuns(); i++) { try { htmlCode += getFormatFromFont(cellText.getFontAtIndex(i)); } catch (NullPointerException ex) { .getFontOfFormattingRun(i)); } catch (NullPointerException ex) { htmlCode += cellText.getString().substring( cellText.getIndexOfFormattingRun(i), cellText.getIndexOfFormattingRun(i) + cellText.getLengthOfFormattingRun(i));
return getRichStringCellValue().toString(); case FORMULA: return getCellFormula();
CTTextParagraph p = txBody.addNewP(); if (str.numFormattingRuns() == 0) { CTRegularTextRun r = p.addNewR(); CTTextCharacterProperties rPr = r.addNewRPr(); rPr.setLang("en-US"); rPr.setSz(1100); r.setT(str.getString()); for (int i = 0; i < str.getCTRst().sizeOfRArray(); i++) { CTRElt lt = str.getCTRst().getRArray(i); CTRPrElt ltPr = lt.getRPr(); if (ltPr == null) {
/** * Set a rich string value for the cell. * * @param value value to set the cell to. For formulas: we'll set the formula * string, for String cells: we'll set its value. For other types we will * change the cell to a string cell and set its value. * If value is null then we will change the cell to a Blank cell. */ @Override public void setCellValue(RichTextString value) { XSSFRichTextString xvalue = (XSSFRichTextString)value; if (xvalue != null && xvalue.getString() != null) { ensureRichTextStringType(); if (xvalue.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()) { throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters"); } ((RichTextValue)_value).setValue(xvalue); } else { setCellType(CellType.BLANK); } }
/** * Applies a font to the specified characters of a string. * * @param startIndex The start index to apply the font to (inclusive) * @param endIndex The end index to apply the font to (exclusive) * @param fontIndex The font to use. */ public void applyFont(int startIndex, int endIndex, short fontIndex) { XSSFFont font; if(styles == null) { //style table is not set, remember fontIndex and set the run properties later, //when setStylesTableReference is called font = new XSSFFont(); font.setFontName("#" + fontIndex); } else { font = styles.getFontAt(fontIndex); } applyFont(startIndex, endIndex, font); }
@Override protected RichTextString createRichTextString(String value) { return new XSSFRichTextString(value); }
case STRING: int sstIndex = Integer.parseInt(_cell.getV()); XSSFRichTextString rt = new XSSFRichTextString(_sharedStringSource.getEntryAt(sstIndex)); return rt.getString(); case NUMERIC: case ERROR: