[]
        
(Showing Draft Content)

富文本

GcExcel Java支持在工作表的单元格中应用富文本格式。默认情况下,当在单元格中输入文本信息时,显示的字母没有任何格式样式。富文本功能允许您通过使用不同的颜色,字体系列,字体效果(粗体,下划线,双下划线,删除线,下标,上标)和字体大小等来突出显示重要字符或字母,从而将多种样式应用于文本。

假设您正在使用电子表格,其中的单元格包含一些字符,需要对其进行较大程度的突出显示,以便重点关注关键信息,例如组织名称,公司的旗舰产品,数字或其他其他敏感数据。在这种情况下,在单元格中设置多种样式时,富文本功能会派上用场。

在下面的示例中,单元格A1包含一个已应用富文本格式的字符串。Documents一词的格式为自定义字体大小,下划线样式和蓝色。同样,文本“ GrapeCity”和“ Excel”已使用多种样式设置了格式。


Rich text


用户可以使用以下任何一种方式在工作表的单元格中设置富文本格式-

IRichText接口的add方法可用于将特定区域的文本添加到IText运行的RichText集合中。

用例代码

请参考以下示例代码,以便使用IRichText界面在工作表的单元格中设置富文本。

// Setting column "A" width
worksheet.getRange("A1").setColumnWidth(70);

// Using IRichText interface to add rich text in cell range A1

// Fetch the IRichText object associated with the cell range
IRichText richText = worksheet.getRange("A1").getRichText();

// Add string "GrapeCity " to IRichText object and apply formatting
ITextRun run1 = richText.add("GrapeCity ");
run1.getFont().setColor(Color.GetRed());
run1.getFont().setBold(true);
run1.getFont().setSize(20);

// Append string "Documents" to IRichText object and apply formatting
ITextRun run2 = richText.add("Documents");
run2.getFont().setThemeFont(ThemeFont.Major);
run2.getFont().setThemeColor(ThemeColor.Accent1);
run2.getFont().setSize(30);
run2.getFont().setUnderline(UnderlineType.Single);

// Append string " for " to IRichText object
richText.add(" for ");

// Append string "Excel" to IRichText object and apply formatting
ITextRun run3 = richText.add("Excel");
run3.getFont().setName("Arial Black");
run3.getFont().setColor(Color.GetLightGreen());
run3.getFont().setSize(36);
run3.getFont().setItalic(true);

使用IRange.characters()

IRange接口的 characters()方法 可用于表示在单元格中输入的文本内的一系列字符。仅当以字符串格式输入单元格值时才能调用此方法。

用例代码

请参考以下示例代码,以便在工作表的单元格中设置富文本。

        
// Setting column "A" width
worksheet.getRange("A1").setColumnWidth(70);

// Use IRange.Characters() to add rich text

// Setting Cell Text
worksheet.getRange("A1").setValue("GrapeCity Documents for Excel");

// Extracting character ranges from cell text and applying different formatting rules to each range

// Formatting string "Grapecity"
ITextRun run1 = worksheet.getRange("A1").characters(0, 9);
run1.getFont().setColor(Color.GetRed());
run1.getFont().setBold(true);
run1.getFont().setSize(20);

// Formatting string "Documents"
ITextRun run2 = worksheet.getRange("A1").characters(10, 9);
run2.getFont().setThemeFont(ThemeFont.Major);
run2.getFont().setThemeColor(ThemeColor.Accent1);
run2.getFont().setSize(30);
run2.getFont().setUnderline(UnderlineType.Single);

// Formatting string "Excel"
ITextRun run3 = worksheet.getRange("A1").characters(24, 5);
run3.getFont().setName("Arial Black");
run3.getFont().setColor(Color.GetLightGreen());
run3.getFont().setSize(36);
run3.getFont().setItalic(true);

使用IRange.characters()配置字体

您还可以使用IRange接口的characters()方法在 工作表的单元格中插入富文本 格式。使用此方法,可以跨多个运行配置字体,然后将它们合并为一个实体。

用例代码

请参考以下示例代码,以便在工作表的单元格中设置富文本。

        
// Setting column "A" width
worksheet.getRange("A1").setColumnWidth(75);

// Use IRange.Characters() to config font across several runs

// Fetch the IRichText object associated with the cell range
IRichText richText = worksheet.getRange("A1").getRichText();

// Add string "GrapeCity " to IRichText object and apply formatting
ITextRun run1 = richText.add("GrapeCity ");
run1.getFont().setColor(Color.GetRed());
run1.getFont().setBold(true);
run1.getFont().setSize(20);

// Append string "Documents" to IRichText object and apply formatting
ITextRun run2 = richText.add("Documents");
run2.getFont().setThemeFont(ThemeFont.Major);
run2.getFont().setThemeColor(ThemeColor.Accent1);
run2.getFont().setSize(30);
run2.getFont().setUnderline(UnderlineType.Single);

// Append string " for " to IRichText object
richText.add(" for ");

// Append string "Excel" to IRichText object and apply formatting
ITextRun run3 = richText.add("Excel");
run3.getFont().setName("Arial Black");
run3.getFont().setColor(Color.GetLightGreen());
run3.getFont().setSize(36);
run3.getFont().setItalic(true);

// Create composite run
// Extract character range composed of "City" word from run1 and " for" word and apply formatting
ITextRun compositeRun = worksheet.getRange("A1").characters(5, 18);
compositeRun.getFont().setBold(true);
compositeRun.getFont().setItalic(true);
compositeRun.getFont().setThemeColor(ThemeColor.Accent1);
        

使用ITextRun.insertAfter() 和 ITextRun.insertBefore()

ITextRun 接口提供的属性和方法添加和自定义工作表的单元格中输入的富文本。ITextRun接口的insertAfter() 和 insertBefore()方法可用于分别在一系列字符之后和之前插入富文本。另外,您可以使用ITextRun接口的delete()方法来删除单元格中插入的富文本。

用例代码

请参考以下示例代码,以便在工作表的单元格中设置富文本。

        
// Setting column "A" width
worksheet.getRange("A1").setColumnWidth(70);

// Use ITextRun.insertAfter() and insertBefore() to add rich text

// Fetch the IRichText object associated with the cell range
IRichText richText = worksheet.getRange("A1").getRichText();

// Add string " for " to IRichText object
ITextRun run1 = richText.add(" for ");

// Use InsertBefore() to add string "Documents" to run1 and apply formatting
ITextRun run2 = run1.insertBefore("Documents");
run2.getFont().setThemeFont(ThemeFont.Major);
run2.getFont().setThemeColor(ThemeColor.Accent1);
run2.getFont().setSize(30);
run2.getFont().setUnderline(UnderlineType.Single);

// Use InsertBefore() to add string "GrapeCity " to run2 and apply formatting
ITextRun run3 = run2.insertBefore("GrapeCity ");
run3.getFont().setColor(Color.GetRed());
run3.getFont().setBold(true);
run3.getFont().setSize(20);

// Use InsertAfter() to add string "Excel" to run1 and apply formatting
ITextRun run4 = run1.insertAfter("Excel");
run4.getFont().setName("Arial Black");
run4.getFont().setColor(Color.GetLightGreen());
run4.getFont().setSize(36);
run4.getFont().setItalic(true);