C#借助Spire.Doc实现Word表格自动排版
作者:缺点内向
在日常开发中,利用C#自动化生成Word报告是一项常见需求。然而,许多开发者都曾遇到过排版难题:默认生成的表格列宽往往不尽如人意,文字拥挤或布局松散。要精准控制表格外观,关键在于如何高效地调整表格列宽。本文将介绍使用 Spire.Doc for .NET 组件,通过C#代码精确设置 C# Word 表格列宽的实用方法。
准备工作
在开始编码前,请确保在项目中引用了 Spire.Doc 的 DLL 文件。该组件无需安装 Microsoft Word,即可在 .NET 环境下高效操作 Word 文档。我们将通过设置单元格宽度来实现列宽的控制,主要使用 SetCellWidth 方法,它可以指定宽度数值和单位类型(如磅值或百分比)。
核心代码实现
以下代码展示了如何创建一个表格,并将第一列的宽度固定为 200 磅,第二列设置为页面宽度的 50%。
// 创建Document对象
Document doc = new Document();
Section section = doc.AddSection();
// 添加一个2行2列的表格
Table table = section.AddTable(true);
table.ResetCells(2, 2);
// 遍历每一行,设置第一列宽度为200磅
for (int i = 0; i < table.Rows.Count; i++)
{
// 获取行中的第一个单元格并设置宽度
// 参数:宽度值, 宽度类型(点/磅)
table.Rows[i].Cells[0].SetCellWidth(200, CellWidthType.Point);
// 设置第二列宽度为50%
table.Rows[i].Cells[1].SetCellWidth(50, CellWidthType.Percentage);
}
// 添加一些示例内容
table[0, 0].AddParagraph().AppendText("固定宽度列");
table[0, 1].AddParagraph().AppendText("自适应百分比列");
// 保存文档
doc.SaveToFile("SetColumnWidth.docx", FileFormat.Docx);
在上述代码中,CellWidthType.Point 用于指定绝对宽度(适用于对齐要求高的场景),而 CellWidthType.Percentage 则用于相对宽度,更适合响应式布局。
知识扩展
下面小编为大家整理了C#设置 Word 表格的格式的其他方法,希望对大家有所帮助
表格样式设置
1.内置样式设置
//载入文档
Document document = new Document("Table.docx");
//获取第一个节
Section section = document.Sections[0];
//获取第一个表格
Table table = section.Tables[0] as Table;
//给表格应用内置样式
table.ApplyStyle(DefaultTableStyle.LightGridAccent3);
//保存文档
document.SaveToFile("BuiltinStyle.docx", FileFormat.Docx2013);2.自定义段落样式设置
//载入文档
Document document = new Document("Table.docx");
//获取第一个节
Section section = document.Sections[0];
//获取第一个表格
Table table = section.Tables[0] as Table;
//设置自定义样式
ParagraphStyle style = new ParagraphStyle(document);
style.Name = "TableStyle";
style.CharacterFormat.FontSize = 14;
style.CharacterFormat.TextColor = Color.SeaGreen;
style.CharacterFormat.HighlightColor = Color.Yellow;
//将自定义样式添加到文档
document.Styles.Add(style);
//给表格第一行第一个单元格的第一个段落应用自定义样式
table[0,0].Paragraphs[0].ApplyStyle(style.Name);
//保存文档
document.SaveToFile("CustomStyle.docx", FileFormat.Docx2013);3.自适应方式设置
//载入文档
Document document = new Document("Table.docx");
//获取第一个节
Section section = document.Sections[0];
//获取第一个表格
Table table = section.Tables[0] as Table;
//自适应内容
table.AutoFit(AutoFitBehaviorType.AutoFitToContents);
//自适应窗口
//table.AutoFit(AutoFitBehaviorType.AutoFitToWindow);
//固定列宽
//table.AutoFit(AutoFitBehaviorType.FixedColumnWidths);
//保存文档
document.SaveToFile("AutofitMode.docx", FileFormat.Docx2013);表格对齐方式设置
//载入文档
Document document = new Document("Tables.docx");
//获取第一个节
Section section = document.Sections[0];
//获取第一、二、三个表格
Table table1 = section.Tables[0] as Table;
Table table2 = section.Tables[1] as Table;
Table table3 = section.Tables[2] as Table;
//设置第一个表格居左
table1.TableFormat.HorizontalAlignment = RowAlignment.Left;
table1.TableFormat.LeftIndent = 34;
//设置第二个表格居中
table2.TableFormat.HorizontalAlignment = RowAlignment.Center;
table2.TableFormat.LeftIndent = 34;
//设置第三个表格居右
table3.TableFormat.HorizontalAlignment = RowAlignment.Right;
table3.TableFormat.LeftIndent = 34;
//保存文档
document.SaveToFile("TableAlignment.docx", FileFormat.Docx2013);边框设置
//载入文档
Document document = new Document("Table.docx");
//获取第一个节
Section section = document.Sections[0];
//获取第一个表格
Table table = section.Tables[0] as Table;
//设置表格的上边框
table.TableFormat.Borders.Top.BorderType = BorderStyle.Double;
table.TableFormat.Borders.Top.LineWidth = 1.0F;
table.TableFormat.Borders.Top.Color = Color.YellowGreen;
//设置表格的左边框
table.TableFormat.Borders.Left.BorderType = BorderStyle.Double;
table.TableFormat.Borders.Left.LineWidth = 1.0F;
table.TableFormat.Borders.Left.Color = Color.YellowGreen;
//设置表格的右边框
table.TableFormat.Borders.Right.BorderType = BorderStyle.Double;
table.TableFormat.Borders.Right.LineWidth = 1.0F;
table.TableFormat.Borders.Right.Color = Color.YellowGreen;
//设置表格的下边框
table.TableFormat.Borders.Bottom.BorderType = BorderStyle.Double;
table.TableFormat.Borders.Bottom.LineWidth = 1.0F;
table.TableFormat.Borders.Bottom.Color = Color.YellowGreen;
//设置表格的水平和垂直边框
table.TableFormat.Borders.Horizontal.BorderType = BorderStyle.Hairline;
table.TableFormat.Borders.Horizontal.Color = Color.Orange;
table.TableFormat.Borders.Vertical.BorderType = BorderStyle.Hairline;
table.TableFormat.Borders.Vertical.Color = Color.Orange;
//保存文档
document.SaveToFile("TableBorder.docx", FileFormat.Docx2013);总结
通过 Spire.Doc for .NET,我们可以灵活地通过遍历行来批量设置单元格属性,从而间接控制整列宽度。这种方法逻辑清晰,不仅能解决排版痛点,还能确保生成的文档在不同环境下保持一致性。希望这段代码能为你的文档自动化任务提供有力支持。
到此这篇关于C#借助Spire.Doc实现Word表格自动排版的文章就介绍到这了,更多相关C# Word表格排版内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
