Package org.apache.poi.xssf.usermodel
Class XSSFRichTextString
java.lang.Object
org.apache.poi.xssf.usermodel.XSSFRichTextString
- All Implemented Interfaces:
RichTextString
Rich text unicode string. These strings can have fonts applied to arbitary parts of the string.
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.
In the above example all three cells will use the same string cached on workbook level.cell1.setCellValue(new XSSFRichTextString("Apache POI")); cell2.setCellValue(new XSSFRichTextString("Apache POI")); cell3.setCellValue(new XSSFRichTextString("Apache POI"));
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 TypeMethodDescriptionvoid
Append new text to this text runvoid
Append new text to this text run and apply the specify font to itvoid
applyFont
(int startIndex, int endIndex, short fontIndex) Applies a font to the specified characters of a string.void
Applies a font to the specified characters of a string.void
applyFont
(short fontIndex) Applies the specified font to the entire string.void
Sets the font of the entire string.void
Removes any formatting that may have been applied to the string.org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst
getCTRst()
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.int
getIndexOfFormattingRun
(int index) The index within the string to which the specified formatting run applies.int
getLengthOfFormattingRun
(int index) Returns the number of characters this format run covers.Returns the plain string representation.boolean
Does this string have any explicit formatting applied, or is it just text in the default style?int
length()
Returns the number of characters in this string.int
protected static void
preserveSpaces
(org.openxmlformats.schemas.spreadsheetml.x2006.main.STXstring xs) Add the xml:spaces="preserve" attribute if the string has leading or trailing spacesvoid
Removes any formatting and sets new string valueprotected void
protected static org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont
toCTFont
(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt pr) CTRPrElt --> CTFont adaptertoString()
Returns the plain string representation.
-
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:
applyFont
in 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:
applyFont
in 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:
applyFont
in interfaceRichTextString
- Parameters:
font
- The font to use.
-
applyFont
public void applyFont(short fontIndex) Applies the specified font to the entire string.- Specified by:
applyFont
in 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 ornull
if 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:
clearFormatting
in interfaceRichTextString
-
getIndexOfFormattingRun
public int getIndexOfFormattingRun(int index) The index within the string to which the specified formatting run applies.- Specified by:
getIndexOfFormattingRun
in 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:
getString
in interfaceRichTextString
-
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:
length
in interfaceRichTextString
- Returns:
- the number of characters in the font.
-
numFormattingRuns
public int numFormattingRuns()- Specified by:
numFormattingRuns
in 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 -
preserveSpaces
protected static void preserveSpaces(org.openxmlformats.schemas.spreadsheetml.x2006.main.STXstring xs) Add the xml:spaces="preserve" attribute if the string has leading or trailing spaces- Parameters:
xs
- the string to check
-