public class XSSFRichTextString extends java.lang.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.
(富文本 unicode 字符串。这些字符串可以将字体应用于字符串的任意部分。工作簿中的大多数字符串都在单元格级别应用了格式,也就是说,单元格中的整个字符串都应用了相同的格式。在这些情况下,单元格的格式存储在样式部分中,并且单元格的字符串可以在工作簿中共享。以下代码说明了该示例。 cell1.setCellValue(new XSSFRichTextString("Apache POI")); cell2.setCellValue(new XSSFRichTextString("Apache POI")); cell3.setCellValue(new XSSFRichTextString("Apache POI"));在上面的示例中,所有三个单元格都将使用缓存在工作簿级别的相同字符串。工作簿中的某些字符串可能在比单元格级别更精细的级别应用了格式。例如,字符串中的特定字符可能加粗、着色、斜体等。在这些情况下,格式与字符串表中的文本一起存储,并被视为工作簿中的唯一条目。以下 xml 和代码片段说明了这一点。 XSSFRichTextString s1 = new XSSFRichTextString("Apache POI"); s1.applyFont(boldArial); cell1.setCellValue(s1); XSSFRichTextString s2 = new XSSFRichTextString("Apache POI"); s2.applyFont(italicCourier); cell2.setCellValue(s2);)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(java.lang.String str)
Create a rich text string
|
Modifier and Type | Method and Description |
---|---|
void |
append(java.lang.String text)
Append new text to this text run
(将新文本附加到此文本运行)
|
void |
append(java.lang.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
(返回底层 xml bean)
|
XSSFFont |
getFontAtIndex(int index)
Return a copy of the font in use at a particular index.
(返回特定索引处正在使用的字体的副本。)
|
XSSFFont |
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.
(返回此格式运行涵盖的字符数。)
|
java.lang.String |
getString()
Returns the plain string representation.
(返回纯字符串表示。)
|
boolean |
hasFormatting()
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 |
numFormattingRuns() |
protected static void |
preserveSpaces(org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STXstring xs)
Add the xml:spaces="preserve" attribute if the string has leading or trailing spaces
(如果字符串有前导或尾随空格,则添加 xml:spaces="preserve" 属性)
|
void |
setString(java.lang.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
(CTRPrElt --> CTFont 适配器)
|
java.lang.String |
toString()
Returns the plain string representation.
(返回纯字符串表示。)
|
public XSSFRichTextString(java.lang.String str)
public XSSFRichTextString()
@Internal public XSSFRichTextString(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst st)
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 applyFont(short fontIndex)
applyFont
in interface
RichTextString
(接口 RichTextString 中的 applyFont)
fontIndex
- the font to apply.
(fontIndex - 要应用的字体。)
public void append(java.lang.String text, XSSFFont font)
text
- the text to append
(text - 要附加的文本)
font
- the font to apply to the appended text or
null
if no formatting is required
(font - 应用于附加文本的字体,如果不需要格式化,则为 null)
public void append(java.lang.String text)
text
- the text to append
(text - 要附加的文本)
public boolean hasFormatting()
public void clearFormatting()
clearFormatting
in interface
RichTextString
(接口 RichTextString 中的 clearFormatting)
public int getIndexOfFormattingRun(int index)
getIndexOfFormattingRun
in interface
RichTextString
(接口 RichTextString 中的 getIndexOfFormattingRun)
index
- the index of the formatting run
(index - 格式化运行的索引)
public int getLengthOfFormattingRun(int index)
index
- the index of the formatting run
(index - 格式化运行的索引)
public java.lang.String getString()
getString
in interface
RichTextString
(接口 RichTextString 中的 getString)
public void setString(java.lang.String s)
s
- new string value
(s - 新的字符串值)
public java.lang.String toString()
toString
in class
java.lang.Object
public int length()
length
in interface
RichTextString
(接口 RichTextString 中的长度)
public int numFormattingRuns()
numFormattingRuns
in interface
RichTextString
(接口 RichTextString 中的 numFormattingRuns)
public XSSFFont getFontOfFormattingRun(int index)
index
- the index of the formatting run
(index - 格式化运行的索引)
public 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.officeDocument.x2006.sharedTypes.STXstring xs)
xs
- the string to check
(xs - 要检查的字符串)
Copyright 2021 The Apache Software Foundation or its licensors, as applicable.