Note, that in certain cases creating too many HSSFRichTextString cells may cause Excel 2003 and lower to crash when changing the color of the cells and then saving the Excel file. Compare two snippets that produce equivalent output:
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 );
and
//create a cell style and assign the first font to it
HSSFCellStyle style = workbook.createCellStyle();
style.setFont(font1);
HSSFCell hssfCell = row.createCell(idx);
hssfCell.setCellStyle(style);
//rich text consists of one run overriding the cell style
HSSFRichTextString richString = new HSSFRichTextString( "Hello, World!" );
richString.applyFont( 6, 13, font2 );
hssfCell.setCellValue( richString );
Excel always uses the latter approach: for a reach text containing N runs Excel saves the font of the first run in the cell's style and subsequent N-1 runs override this font.
For more information regarding this behavior please consult Bugzilla 47543: https://issues.apache.org/bugzilla/show_bug.cgi?id=47543
| Modifier and Type | Field and Description |
|---|---|
| private InternalWorkbook | |
| private LabelSSTRecord | |
| private UnicodeString | |
| public static final short | NO_FONT
Place holder for indicating that NO_FONT has been applied here |
| Access | Constructor and Description |
|---|---|
| public | |
| public | |
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| public void | applyFont(int
The start index to apply the font to (inclusive) startIndex, int The end index to apply the font to (exclusive) endIndex, short The font to use. fontIndex)Implements org. Applies a font to the specified characters of a string. |
| public void | applyFont(int
The start index to apply the font to (inclusive) startIndex, int The end index to apply to font to (exclusive) endIndex, Font The index of the font to use. font)Implements org. Applies a font to the specified characters of a string. |
| public void | applyFont(Font
The font to use. font)Implements org. Sets the font of the entire string. |
| public void | applyFont(short
the font to apply. fontIndex)Implements org. Applies the specified font to the entire string. |
| public void | clearFormatting()
Implements org. Removes any formatting that may have been applied to the string. |
| private UnicodeString | |
| public int | compareTo(HSSFRichTextString
the object to be compared. r)Implements java. Compares one rich text string to another. |
| public boolean | equals(Object
the reference object with which to compare. o)Overrides java. Indicates whether some other object is "equal to" this one. |
| public short | Returns: 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.The index. index)Returns the font in use at a particular index. |
| public short | Returns: the font number used.the index of the formatting run index)Gets the font used in a particular formatting run. |
| public int | Returns: the index within the string.the index of the formatting run index)Implements org. The index within the string to which the specified formatting run applies. |
| pack-priv UnicodeString | |
| public String | getString()
Implements org. Returns the plain string representation. |
| pack-priv UnicodeString | |
| public int | |
| public int | Returns: the number of characters in the text.Implements org.
|
| public int | Returns: The number of formatting runs used. There will always be at least one of font NO_FONT.Implements org.
|
| pack-priv void | |
| pack-priv void | setWorkbookReferences(InternalWorkbook book, LabelSSTRecord record)
This must be called to setup the internal work book references whenever a RichTextString is added to a cell |
| public String | Returns: the plain text representation of this string.Overrides java. Returns a string representation of the object. |
| _book | back to summary |
|---|---|
| private InternalWorkbook _book | |
| _record | back to summary |
|---|---|
| private LabelSSTRecord _record | |
| _string | back to summary |
|---|---|
| private UnicodeString _string | |
| NO_FONT | back to summary |
|---|---|
| public static final short NO_FONT Place holder for indicating that NO_FONT has been applied here | |
| HSSFRichTextString | back to summary |
|---|---|
| public HSSFRichTextString() | |
| HSSFRichTextString | back to summary |
|---|---|
| public HSSFRichTextString(String string) | |
| HSSFRichTextString | back to summary |
|---|---|
| pack-priv HSSFRichTextString(InternalWorkbook book, LabelSSTRecord record) | |
| addToSSTIfRequired | back to summary |
|---|---|
| private void addToSSTIfRequired() | |
| applyFont | back to summary |
|---|---|
| public void applyFont(int startIndex, int endIndex, short fontIndex) Implements org. Applies a font to the specified characters of a string.
| |
| applyFont | back to summary |
|---|---|
| public void applyFont(int startIndex, int endIndex, Font font) Implements org. Applies a font to the specified characters of a string. | |
| applyFont | back to summary |
|---|---|
| public void applyFont(Font font) Implements org. Sets the font of the entire string. | |
| applyFont | back to summary |
|---|---|
| public void applyFont(short fontIndex) Implements org. Applies the specified font to the entire string.
| |
| clearFormatting | back to summary |
|---|---|
| public void clearFormatting() Implements org. Removes any formatting that may have been applied to the string.
| |
| cloneStringIfRequired | back to summary |
|---|---|
| private UnicodeString cloneStringIfRequired() Called whenever the unicode string is modified. When it is modified we need to create a new SST index, so that other LabelSSTRecords will not be affected by changes that we make to this string. | |
| compareTo | back to summary |
|---|---|
| public int compareTo(HSSFRichTextString r) Implements java. Compares one rich text string to another.
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object o) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes. | |
| getFontAtIndex | back to summary |
|---|---|
| public short getFontAtIndex(int index) Returns the font in use at a particular index.
| |
| getFontOfFormattingRun | back to summary |
|---|---|
| public short getFontOfFormattingRun(int index) Gets the font used in a particular formatting run.
| |
| getIndexOfFormattingRun | back to summary |
|---|---|
| public int getIndexOfFormattingRun(int index) Implements org. The index within the string to which the specified formatting run applies.
| |
| getRawUnicodeString | back to summary |
|---|---|
| pack-priv UnicodeString getRawUnicodeString() Returns the raw, probably shared Unicode String. Used when tweaking the styles, eg updating font positions. Changes to this string may well effect other RichTextStrings too! | |
| getString | back to summary |
|---|---|
| public String getString() Implements org. Returns the plain string representation.
| |
| getUnicodeString | back to summary |
|---|---|
| pack-priv UnicodeString getUnicodeString() Used internally by the HSSFCell to get the internal string value. Will ensure the string is not shared | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for this object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| length | back to summary |
|---|---|
| public int length() Implements org.
| |
| numFormattingRuns | back to summary |
|---|---|
| public int numFormattingRuns() Implements org. | |
| setUnicodeString | back to summary |
|---|---|
| pack-priv void setUnicodeString(UnicodeString str) Used internally by the HSSFCell to set the internal string value | |
| setWorkbookReferences | back to summary |
|---|---|
| pack-priv void setWorkbookReferences(InternalWorkbook book, LabelSSTRecord record) This must be called to setup the internal work book references whenever a RichTextString is added to a cell | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non-
| |