使用表格
使用表格
表格在 Aspose.Words FOSS 用于 .NET 由以下表示 Aspose.Words.Tables 命名空间: Table, Row,以及 Cell 形成一个三层复合节点层次结构,位于文档树中与段落并列。此页面涵盖使用 DocumentBuilder,格式化行和单元格,控制整表布局,以及合并单元格。
表格对象模型
一个 Table 是一个包含 Row 节点的 CompositeNode,每个 Row 是一个包含 Cell 节点的 CompositeNode;Cell 则包含段落,并且可能包含嵌套表格。Table.Rows 返回一个 RowCollection,Row.Cells 返回一个 CellCollection,而 Body 或其他 Story 通过其 Tables 属性(一个 TableCollection)公开其中的每个顶层表格。所有这三种集合都支持 Add()、Insert()、Remove()、RemoveAt()、Contains() 和 IndexOf()。Table.EnsureMinimum()、Row.EnsureMinimum() 和 Cell.EnsureMinimum() 分别在编程编辑后留下空缺时,填充该类型节点所需的最小有效内容(空行、空单元格或空段落)。
使用 DocumentBuilder 构建表格
DocumentBuilder.StartTable() 在光标处开始一个新表格,InsertCell() 向当前行添加单元格,EndRow() 关闭当前行,EndTable() 关闭表格。在调用 InsertCell() 或 EndRow() 之前,设置 DocumentBuilder.CellFormat 和 DocumentBuilder.RowFormat 以对创建时的单元格和行应用默认格式——这些属性充当构建器随后插入内容的模板,而不是对文档中已有单元格的事后更改。
单元格和行的格式化
Cell.CellFormat 是一个 CellFormat 实例(Borders、Shading、Width、WrapText、FitText、HorizontalMerge、VerticalMerge、VerticalAlignment、PreferredWidth、LeftPadding、RightPadding、TopPadding、BottomPadding、HideMark、Orientation),带有 ClearFormatting() 用于重置它,SetPaddings() 用于一次性设置四个单元格内边距。Row.RowFormat 是一个 RowFormat 实例(Borders、Height、HeightRule、AllowBreakAcrossPages、HeadingFormat),拥有其自己的 ClearFormatting()。
表格整体格式化和 AutoFit
Table 本身公开布局属性——Alignment(TableAlignment)、Bidi、LeftIndent、PreferredWidth、Style、StyleIdentifier、StyleName、StyleOptions(TableStyleOptions)、TextWrapping,以及通过 Title 和 Description 提供的可访问性元数据。Table.AutoFit(AutoFitBehavior) 根据给定的行为调整表格及其单元格的大小。SetBorder() 设置单侧边框,SetBorders() 将整张表的所有边框设置为相同的线型、宽度和颜色;ClearBorders() 和 ClearShading() 用于重置这些设置。ConvertToHorizontallyMergedCells() 将因宽度匹配而合并的单元格转换为显式标记了 HorizontalMerge 的单元格。
首选宽度和单元格宽度
PreferredWidth 表示宽度及其单位,使用静态工厂方法 PreferredWidth.FromPercent() 或 PreferredWidth.FromPoints() 创建;PreferredWidthType 用于识别给定 PreferredWidth 所持有的值类型(Auto、Percent 或 Points)。Table.PreferredWidth 和 CellFormat.PreferredWidth 都接受这些值。
合并单元格
CellFormat.HorizontalMerge 和 CellFormat.VerticalMerge(CellMerge)标记单元格在水平或垂直合并中如何与相邻单元格参与。CellVerticalAlignment 控制单元格内内容的垂直对齐方式,独立于任何合并状态。
技巧与最佳实践
- 在每行关闭每个
StartTable()时使用相匹配的EndRow(),并在最后使用EndTable()—— 未关闭的行或表格会导致构建器的内部状态(以及生成的文档树)出现错误。 - 如果同一表格中的不同行或单元格需要不同的格式,请在每次调用
InsertCell()或EndRow()之前设置DocumentBuilder.CellFormat和DocumentBuilder.RowFormat;构建器会在插入时使用当时已设置的值。 - 在以编程方式删除内容后调用
Table.EnsureMinimum()、Row.EnsureMinimum()或Cell.EnsureMinimum(),以确保节点保持 Word 所期望的最小有效结构。 - 对于应随可用页面宽度自适应的表格,使用
PreferredWidth.FromPercent();对于固定布局宽度的表格,使用PreferredWidth.FromPoints()。 - 在调用
Table.AutoFit()时会应用AutoFitBehavior—— 它不会在你添加或删除内容时持续重新执行,因此如果需要重新调整表格,请在进行重大编辑后再次调用它。
常见问题
| 问题 | 原因 | 修复 |
|---|---|---|
| 表格看起来格式错误或在保存时抛出错误 | 某行或表格从未使用 EndRow() / EndTable() 关闭 | 始终将 StartTable() 与最终的 EndTable() 配对,并使用 EndRow() 关闭每一行 |
| 新单元格未采用我设置的格式 | CellFormat / RowFormat 在单元格已插入后于构建器中被更改 | 在调用 InsertCell() / EndRow() 处理该内容之前,先设置 DocumentBuilder.CellFormat / RowFormat |
| 表格在编辑后未按预期调整大小 | AutoFit() 反映表格在调用时的状态,而不是持续的 | 在结构编辑后再次调用 Table.AutoFit(AutoFitBehavior) |
| 看似合并的相邻单元格实际上表现为独立的单元格 | 单元格仅按宽度匹配,而未使用 CellMerge 标记 | 调用 Table.ConvertToHorizontallyMergedCells(),或显式设置 CellFormat.HorizontalMerge / VerticalMerge |
FAQ
如何使用 DocumentBuilder 从头构建表格?
调用 StartTable(),然后对行中的每个单元格使用 InsertCell(),使用 EndRow() 关闭行,重复其他行,最后使用 EndTable() 关闭表格。
如何将表格宽度设置为百分比而不是固定宽度?
分配 Table.PreferredWidth = PreferredWidth.FromPercent(value) 而不是 PreferredWidth.FromPoints(value)。
如何重置单元格或行的所有格式?
在单元格的 CellFormat 上调用 CellFormat.ClearFormatting(),或在行的 RowFormat 上调用 RowFormat.ClearFormatting()。
如何一次性为整个表格设置边框和底纹?
使用 Table.SetBorders() 为每一侧设置统一边框,使用 Table.SetBorder() 可单独设置一侧,使用 Table.SetShading() 设置统一底纹;ClearBorders() 和 ClearShading() 可重置它们。
API Reference 摘要
| 类 / 枚举 | 描述 |
|---|---|
Table | 表节点; Rows, AutoFit(), SetBorders(), SetShading(), PreferredWidth |
Row | 表行节点; Cells, RowFormat, EnsureMinimum() |
Cell | 表格单元节点; CellFormat, EnsureMinimum() |
TableCollection / RowCollection / CellCollection | 类型化的 Table / Row / Cell 节点集合 |
CellFormat | 每个单元格的格式设置:边框、底纹、内边距、合并状态、垂直对齐 |
RowFormat | 每行的格式设置:边框、高度、分页行为 |
PreferredWidth / PreferredWidthType | 表格或单元格宽度值及其单位(auto、percent、points) |
AutoFitBehavior | Table.AutoFit() 如何调整表格及其单元格的大小 |
CellMerge / CellVerticalAlignment | 水平/垂直单元格合并状态和垂直内容对齐 |
TableAlignment / TableStyleOptions / TextWrapping | 全表对齐、样式应用以及自动换行行为 |
DocumentBuilder.StartTable / InsertCell / EndRow / EndTable | 基于光标的表格构建 |