public class XSSFRichTextString extends Object implements 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.
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 and Description |
---|
XSSFRichTextString()
Create empty rich text string and initialize it with empty string
|
XSSFRichTextString(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst st)
Create a rich text string from the supplied XML bean
|
XSSFRichTextString(String str)
Create a rich text string
|
Modifier and Type | Method and Description |
---|---|
void |
addRun(String text,
XSSFFont font) |
void |
append(String text)
Append new text to this text run
|
void |
append(String text,
XSSFFont font)
Append new text to this text run and apply the specify font to it
|
void |
applyFont(Font font)
Sets the font of the entire string.
|
void |
applyFont(int startIndex,
int endIndex,
Font font)
Applies a font to the specified characters of a string.
|
void |
applyFont(int startIndex,
int endIndex,
short fontIndex)
Applies a font to the specified characters of a string.
|
void |
applyFont(short fontIndex)
Applies the specified font to the entire string.
|
void |
clearFormatting()
Removes any formatting that may have been applied to the string.
|
org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst |
getCTRst()
Return the underlying xml bean
|
XSSFFont |
getFontAtIndex(int index)
Return a copy of the font in use at a particular index.
|
XSSFFont |
getFontOfFormattingRun(XSSFWorkbook book,
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.
|
String |
getRawString() |
String |
getString()
Returns the plain string representation.
|
String |
getStringAt(int runIndex)
Returns the plain string representation of a run where _xHHHH_ is decoded
to Java character already.
|
int |
length()
Returns the number of characters in this string.
|
int |
numFormattingRuns() |
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
|
void |
setString(String s)
Removes any formatting and sets new string value
|
protected void |
setStylesTableReference(StylesTable tbl) |
protected static org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont |
toCTFont(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt pr)
CTRPrElt --> CTFont adapter
|
String |
toString()
Returns the plain string representation.
|
static String |
utfEncode(String value)
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.
|
public XSSFRichTextString(String str)
public XSSFRichTextString()
public XSSFRichTextString(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst st)
public void applyFont(int startIndex, int endIndex, short fontIndex)
applyFont
in interface RichTextString
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.public void applyFont(int startIndex, int endIndex, Font font)
applyFont
in interface RichTextString
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.public void applyFont(Font font)
applyFont
in interface RichTextString
font
- The font to use.public void applyFont(short fontIndex)
applyFont
in interface RichTextString
fontIndex
- the font to apply.public void append(String text, XSSFFont font)
text
- the text to appendfont
- the font to apply to the appended text or null
if no formatting is requiredpublic void append(String text)
text
- the text to appendpublic void clearFormatting()
clearFormatting
in interface RichTextString
public int getIndexOfFormattingRun(int index)
getIndexOfFormattingRun
in interface RichTextString
index
- the index of the formatting runpublic int getLengthOfFormattingRun(int index)
index
- the index of the formatting runpublic String getString()
getString
in interface RichTextString
public void setString(String s)
s
- new string valuepublic String toString()
public int length()
length
in interface RichTextString
public int numFormattingRuns()
numFormattingRuns
in interface RichTextString
public XSSFFont getFontOfFormattingRun(XSSFWorkbook book, int index)
index
- the index of the formatting runpublic XSSFFont getFontAtIndex(int index)
index
- The index.@Internal public org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst getCTRst()
protected void setStylesTableReference(StylesTable tbl)
protected static org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont toCTFont(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt pr)
protected static void preserveSpaces(org.openxmlformats.schemas.spreadsheetml.x2006.main.STXstring xs)
xs
- the string to checkpublic String getStringAt(int runIndex)
RichTextString
getStringAt
in interface RichTextString
public static String utfEncode(String value)
Example: The Unicode character 0D is invalid in an XML 1.0 document,
so it shall be escaped as _x000D_
.
value
- the string to encodepublic String getRawString()
Copyright © 2020. All rights reserved.