富文本格式由 unvell.ReoGrid.Drawing.RichText 类表示,可以添加到单元格或浮动对象中显示和打印。

aardio 示例代码:
import dotNet.ReoGrid;
var sheet1 = grid.CurrentWorksheet;
sheet1.MergeRange("B2:F6");
// 创建富文本对象并设置样式
sheet1["B2"] = ReoGrid.Drawing.RichText()
.Regular("The ")
.Bold("Rich Text Format")
.Regular(" (often abbreviated ")
.Bold("RTF")
.Regular(") is a proprietary")
.Superscript("[6][7][8]")
.Regular(" document file format with published specification developed by Microsoft Corporation from ")
.Span("1987", { textColor = ReoGrid.Graphics.SolidColor.OrangeRed })
.Span(" until ", { textColor = ReoGrid.Graphics.SolidColor.Black })
.Span("2008", { textColor = ReoGrid.Graphics.SolidColor.OrangeRed })
.Span(" for cross-platform document interchange with Microsoft products.", { textColor = ReoGrid.Graphics.SolidColor.Black });
设置单元格样式实现自动换行或强制换行。
示例:不换行的文本

示例:自动换行的文本

aardio 代码:
// 设置单元格文本自动换行
sheet1.Cells["B2"].Style.TextWrap = ReoGrid.TextWrapMode.WordBreak;
通过设置单元格的水平对齐和垂直对齐样式来改变富文本的对齐方式。

aardio 代码:
// 垂直居中
sheet1.Cells["B2"].Style.VAlign = ReoGrid.ReoGridVerAlign.Middle;
设置右对齐:

aardio 代码:
// 水平右对齐
sheet1.Cells["B2"].Style.HAlign = ReoGrid.ReoGridHorAlign.Right;

使用 Span 方法添加无特殊样式的文本:
sheet1["B2"] = ReoGrid.Drawing.RichText()
.Span("The Apple II (styled as apple ][) is an 8-bit home computer.");

aardio 代码:
sheet1["B2"] = ReoGrid.Drawing.RichText()
.Span("The ")
.Bold("Apple II")
.Regular(" (styled as apple ][) is an 8-bit home computer.");
使用 Bold 方法加粗文本,Regular 方法恢复默认样式。斜体、下划线、删除线等样式的使用方法与加粗类似。
使用 NewLine 方法换行:

aardio 代码:
sheet1["B2"] = ReoGrid.Drawing.RichText()
.Span("The ")
.Bold("Apple II")
.Regular(" (styled as apple ][) is an 8-bit home computer.")
.NewLine()
.Span("One of the first highly successful mass-produced microcomputer products,[2] designed primarily by Steve Wozniak.");
NewLine 方法会创建新段落:

可以调整段落间距和行高:

使用 SetStyles 方法修改当前段落或文本段的样式:

aardio 代码:
sheet1["B2"] = ReoGrid.Drawing.RichText()
.SetStyles({ paragraphSpacing = 3.0 })
.Span("The ")
.Bold("Apple II")
.Regular(" (styled as apple ][) is an 8-bit home computer.")
.NewLine()
.SetStyles({ lineHeight = 2.0 })
.Span("One of the first highly successful mass-produced microcomputer products,[2] designed primarily by Steve Wozniak.");
注意:Excel 不支持调整段落间距和行高样式。

aardio 代码:
sheet1["B2"] = ReoGrid.Drawing.RichText()
.Span("The ")
.Bold("Apple II")
.Regular(" (styled as apple ][) is an 8-bit home computer.")
.NewLine()
.Span("One of the first highly successful mass-produced microcomputer products,[2] designed primarily by Steve Wozniak.")
.NewLine()
.SetStyles({ halign = ReoGrid.ReoGridHorAlign.Right })
.Span("- wikipedia.org");
注意:Excel 不支持每段独立设置对齐方式。
使用 Superscript 方法创建上标:

aardio 代码:
sheet1["B2"] = ReoGrid.Drawing.RichText()
.Span("The ")
.Bold("Apple II")
.Regular(" (styled as apple ][) is an 8-bit home computer.")
.NewLine()
.Span("One of the first highly successful mass-produced microcomputer products,")
.Superscript("[2]")
.Regular(" designed primarily by Steve Wozniak.")
.NewLine()
.SetStyles({ halign = ReoGrid.ReoGridHorAlign.Right })
.Span("- wikipedia.org");
要在绘图对象中显示富文本,创建 RichText 实例并添加到绘图对象中。
ReoGrid 支持从 Excel 文件导入和导出富文本格式。下图是在 ReoGrid 和 Excel 中显示的相同文本:
