public final class HSSFRichTextString extends java.lang.Object implements java.lang.Comparable<HSSFRichTextString>, RichTextString
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:
andHSSFCell 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 );
//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
(富文本 unicode 字符串。这些字符串可以将字体应用于字符串的任意部分。请注意,在某些情况下,创建太多 HSSFRichTextString 单元格可能会导致 Excel 2003 及更低版本在更改单元格颜色然后保存 Excel 文件时崩溃。比较产生等效输出的两个片段: HSSFCell hssfCell = row.createCell(idx); //富文本由两个运行组成 HSSFRichTextString richString = new HSSFRichTextString( "Hello, World!" ); richString.applyFont(0, 6, font1); richString.applyFont(6, 13, font2); hssfCell.setCellValue(richString);和//创建单元格样式并将第一个字体分配给它 HSSFCellStyle style = workbook.createCellStyle(); style.setFont(font1); HSSFCell hssfCell = row.createCell(idx); hssfCell.setCellStyle(style); //富文本包含一个覆盖单元格样式的运行 HSSFRichTextString richString = new HSSFRichTextString( "Hello, World!" ); richString.applyFont(6, 13, font2); hssfCell.setCellValue(richString); Excel 始终使用后一种方法:对于包含 N 次运行的到达文本,Excel 将第一次运行的字体保存在单元格的样式中,随后的 N-1 次运行会覆盖此字体。有关此行为的更多信息,请参阅 Bugzilla 47543:https://issues.apache.org/bugzilla/show_bug.cgi?id=47543)Modifier and Type | Field and Description |
---|---|
static short |
NO_FONT
Place holder for indicating that NO_FONT has been applied here
(用于指示此处已应用 NO_FONT 的占位符)
|
Constructor and Description |
---|
HSSFRichTextString() |
HSSFRichTextString(java.lang.String string) |
Modifier and Type | Method and Description |
---|---|
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.
(删除可能已应用于字符串的任何格式。)
|
int |
compareTo(HSSFRichTextString r)
Compares one rich text string to another.
(将一个富文本字符串与另一个进行比较。)
|
boolean |
equals(java.lang.Object o) |
short |
getFontAtIndex(int index)
Returns the font in use at a particular index.
(返回特定索引处使用的字体。)
|
short |
getFontOfFormattingRun(int index)
Gets the font used in a particular formatting run.
(获取在特定格式运行中使用的字体。)
|
int |
getIndexOfFormattingRun(int index)
The index within the string to which the specified formatting run applies.
(指定格式运行应用到的字符串中的索引。)
|
java.lang.String |
getString()
Returns the plain string representation.
(返回纯字符串表示。)
|
int |
hashCode() |
int |
length() |
int |
numFormattingRuns() |
java.lang.String |
toString() |
public static final short NO_FONT
public HSSFRichTextString()
public HSSFRichTextString(java.lang.String string)
public void applyFont(int startIndex, int endIndex, short fontIndex)
applyFont
in interface
RichTextString
(接口 RichTextString 中的 applyFont)
startIndex
- The start index to apply the font to (inclusive)
(startIndex - 应用字体的起始索引(包括))
endIndex
- The end index to apply the font to (exclusive)
(endIndex - 应用字体的结束索引(不包括))
fontIndex
- The font to use.
(fontIndex - 要使用的字体。)
public void applyFont(int startIndex, int endIndex, Font font)
applyFont
in interface
RichTextString
(接口 RichTextString 中的 applyFont)
startIndex
- The start index to apply the font to (inclusive)
(startIndex - 应用字体的起始索引(包括))
endIndex
- The end index to apply to font to (exclusive)
(endIndex - 应用于字体的结束索引(不包括))
font
- The index of the font to use.
(font - 要使用的字体的索引。)
public void applyFont(Font font)
applyFont
in interface
RichTextString
(接口 RichTextString 中的 applyFont)
font
- The font to use.
(font - 要使用的字体。)
public void clearFormatting()
clearFormatting
in interface
RichTextString
(接口 RichTextString 中的 clearFormatting)
public java.lang.String getString()
getString
in interface
RichTextString
(接口 RichTextString 中的 getString)
public int length()
length
in interface
RichTextString
(接口 RichTextString 中的长度)
public short getFontAtIndex(int index)
index
- The index.
(索引 - 索引。)
public int numFormattingRuns()
numFormattingRuns
in interface
RichTextString
(接口 RichTextString 中的 numFormattingRuns)
NO_FONT
public int getIndexOfFormattingRun(int index)
getIndexOfFormattingRun
in interface
RichTextString
(接口 RichTextString 中的 getIndexOfFormattingRun)
index
- the index of the formatting run
(index - 格式化运行的索引)
public short getFontOfFormattingRun(int index)
index
- the index of the formatting run
(index - 格式化运行的索引)
public int compareTo(HSSFRichTextString r)
compareTo
in interface
java.lang.Comparable<HSSFRichTextString>
(接口 java.lang.Comparable 中的 compareTo
public boolean equals(java.lang.Object o)
equals
in class
java.lang.Object
public int hashCode()
hashCode
in class
java.lang.Object
public java.lang.String toString()
toString
in class
java.lang.Object
public void applyFont(short fontIndex)
applyFont
in interface
RichTextString
(接口 RichTextString 中的 applyFont)
fontIndex
- the font to apply.
(fontIndex - 要应用的字体。)
Copyright 2021 The Apache Software Foundation or its licensors, as applicable.