Class XSSFRichTextString
- All Implemented Interfaces:
RichTextString
Most strings in a workbook have formatting applied at the cell level, that is, the entire string in the cell has the same formatting applied. In these cases, the formatting for the cell is stored in the styles part, and the string for the cell can be shared across the workbook. The following code illustrates the example.
cell1.setCellValue(new XSSFRichTextString("Apache POI"));
cell2.setCellValue(new XSSFRichTextString("Apache POI"));
cell3.setCellValue(new XSSFRichTextString("Apache POI"));
In the above example all three cells will use the same string cached on workbook level.
Some strings in the workbook may have formatting applied at a level that is more granular than the cell level. For instance, specific characters within the string may be bolded, have coloring, italicizing, etc. In these cases, the formatting is stored along with the text in the string table, and is treated as a unique entry in the workbook. The following xml and code snippet illustrate this.
XSSFRichTextString s1 = new XSSFRichTextString("Apache POI");
s1.applyFont(boldArial);
cell1.setCellValue(s1);
XSSFRichTextString s2 = new XSSFRichTextString("Apache POI");
s2.applyFont(italicCourier);
cell2.setCellValue(s2);
-
Constructor Summary
ConstructorsConstructorDescriptionCreate empty rich text string and initialize it with empty stringXSSFRichTextString(String str) Create a rich text stringXSSFRichTextString(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst st) Create a rich text string from the supplied XML bean -
Method Summary
Modifier and TypeMethodDescriptionvoidAppend new text to this text runvoidAppend new text to this text run and apply the specify font to itvoidapplyFont(int startIndex, int endIndex, short fontIndex) Applies a font to the specified characters of a string.voidApplies a font to the specified characters of a string.voidapplyFont(short fontIndex) Applies the specified font to the entire string.voidSets the font of the entire string.voidRemoves any formatting that may have been applied to the string.org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRstgetCTRst()Return the underlying xml beangetFontAtIndex(int index) Return a copy of the font in use at a particular index.getFontOfFormattingRun(int index) Gets a copy of the font used in a particular formatting run.intgetIndexOfFormattingRun(int index) The index within the string to which the specified formatting run applies.intgetLengthOfFormattingRun(int index) Returns the number of characters this format run covers.Returns the plain string representation.booleanDoes this string have any explicit formatting applied, or is it just text in the default style?intlength()Returns the number of characters in this string.intprotected static voidpreserveSpaces(org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STXstring xs) Add the xml:spaces="preserve" attribute if the string has leading or trailing spacesvoidRemoves any formatting and sets new string valueprotected voidprotected static org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFonttoCTFont(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt pr) CTRPrElt --> CTFont adaptertoString()Returns the plain string representation.static StringFor all characters which cannot be represented in XML as defined by the XML 1.0 specification, the characters are escaped using the Unicode numerical character representation escape character format _xHHHH_, where H represents a hexadecimal character in the character's value.static StringEncodes a string so that it survives a round trip throughutfDecode(String), using the_xHHHH_escapes defined forST_Xstringin the OOXML spec.
-
Constructor Details
-
XSSFRichTextString
Create a rich text string -
XSSFRichTextString
public XSSFRichTextString()Create empty rich text string and initialize it with empty string -
XSSFRichTextString
Create a rich text string from the supplied XML bean
-
-
Method Details
-
applyFont
public void applyFont(int startIndex, int endIndex, short fontIndex) Applies a font to the specified characters of a string.- Specified by:
applyFontin interfaceRichTextString- Parameters:
startIndex- The start index to apply the font to (inclusive)endIndex- The end index to apply the font to (exclusive)fontIndex- The font to use.
-
applyFont
Applies a font to the specified characters of a string.- Specified by:
applyFontin interfaceRichTextString- Parameters:
startIndex- The start index to apply the font to (inclusive)endIndex- The end index to apply to font to (exclusive)font- The index of the font to use.
-
applyFont
Sets the font of the entire string.- Specified by:
applyFontin interfaceRichTextString- Parameters:
font- The font to use.
-
applyFont
public void applyFont(short fontIndex) Applies the specified font to the entire string.- Specified by:
applyFontin interfaceRichTextString- Parameters:
fontIndex- the font to apply.
-
append
Append new text to this text run and apply the specify font to it- Parameters:
text- the text to appendfont- the font to apply to the appended text ornullif no formatting is required
-
append
Append new text to this text run- Parameters:
text- the text to append
-
hasFormatting
public boolean hasFormatting()Does this string have any explicit formatting applied, or is it just text in the default style? -
clearFormatting
public void clearFormatting()Removes any formatting that may have been applied to the string.- Specified by:
clearFormattingin interfaceRichTextString
-
getIndexOfFormattingRun
public int getIndexOfFormattingRun(int index) The index within the string to which the specified formatting run applies.- Specified by:
getIndexOfFormattingRunin interfaceRichTextString- Parameters:
index- the index of the formatting run- Returns:
- the index within the string.
-
getLengthOfFormattingRun
public int getLengthOfFormattingRun(int index) Returns the number of characters this format run covers.- Parameters:
index- the index of the formatting run- Returns:
- the number of characters this format run covers
-
getString
Returns the plain string representation.- Specified by:
getStringin interfaceRichTextString- Returns:
- The string representation of this RichText string, null if there is no data at all
-
setString
Removes any formatting and sets new string value- Parameters:
s- new string value
-
toString
Returns the plain string representation. -
length
public int length()Returns the number of characters in this string.- Specified by:
lengthin interfaceRichTextString- Returns:
- the number of characters in the font.
-
numFormattingRuns
public int numFormattingRuns()- Specified by:
numFormattingRunsin interfaceRichTextString- Returns:
- The number of formatting runs used.
-
getFontOfFormattingRun
Gets a copy of the font used in a particular formatting run.- Parameters:
index- the index of the formatting run- Returns:
- A copy of the font used or null if no formatting is applied to the specified text run.
-
getFontAtIndex
Return a copy of the font in use at a particular index.- Parameters:
index- The index.- Returns:
- A copy of the font that's currently being applied at that index or null if no font is being applied or the index is out of range.
-
getCTRst
Return the underlying xml bean -
setStylesTableReference
-
toCTFont
protected static org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont toCTFont(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt pr) CTRPrElt --> CTFont adapter -
utfDecode
For all characters which cannot be represented in XML as defined by the XML 1.0 specification, the characters are escaped using the Unicode numerical character representation escape character format _xHHHH_, where H represents a hexadecimal character in the character's value.Example: The Unicode character 0D is invalid in an XML 1.0 document, so it shall be escaped as
See section 3.18.9 in the OOXML spec._x000D_.- Parameters:
value- the string to decode- Returns:
- the decoded string or null if the input string is null
- Since:
- 6.0.0
- See Also:
-
utfEncode
Encodes a string so that it survives a round trip throughutfDecode(String), using the_xHHHH_escapes defined forST_Xstringin the OOXML spec.- The leading underscore of literal text that would otherwise be read as a
_xHHHH_escape is written as_x005F_, as Excel does, e.g._x0041_becomes_x005F_x0041_. - Characters that cannot be represented in XML 1.0 (control characters other than
tab, line feed and carriage return, and U+FFFE/U+FFFF) are escaped,
e.g. U+0001 becomes
_x0001_.
- Parameters:
value- the string to encode- Returns:
- the encoded string or null if the input string is null
- Since:
- 6.0.0
- See Also:
- The leading underscore of literal text that would otherwise be read as a
-