如何利用Aspose.Words将格式应用于表,行和单元格?示例演示带你全面了解!

Aspose.Words For .Net是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

【下载Aspose.Words for .NET最新试用版】

接下来我们将进入“使用格式”的介绍,其中包括应用格式、介绍和创建表、添加和拆分表以及使用列和行。

将格式应用于表,行和单元格

表的每个元素都可以应用不同的格式。例如,表格格式将应用于整个表格,而行格式化仅影响特定行等。Aspose.Words提供了丰富的API来检索和应用格式化表格。您可以使用Table,RowFormat和CellFormat节点来设置格式。

▲在表级应用格式

要将格式应用于表,可以使用相应表节点上的可用属性。下面给出了Microsoft Word中表格格式功能的可视化视图及其在Aspose.Words中的相应属性。

Aspose.Words使用表格教程之应用格式——将格式应用于表,行和单元格

Aspose.Words使用表格教程之应用格式——将格式应用于表,行和单元格

下面的示例显示如何将轮廓边框应用于表:

Document doc = new Document(dataDir + "Table.EmptyTable.doc");
            
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
//将表格对齐到页面的中心。
table.Alignment = TableAlignment.Center;
// Clear any existing borders from the table.
table.ClearBorders();

//在桌子周围设置一个绿色的边框,但不要在里面。
table.SetBorder(BorderType.Left, LineStyle.Single, 1.5, Color.Green, true);
table.SetBorder(BorderType.Right, LineStyle.Single, 1.5, Color.Green, true);
table.SetBorder(BorderType.Top, LineStyle.Single, 1.5, Color.Green, true);
table.SetBorder(BorderType.Bottom, LineStyle.Single, 1.5, Color.Green, true);

// 用淡绿色填充单元格。
table.SetShading(TextureIndex.TextureSolid, Color.LightGreen, Color.Empty);
dataDir = dataDir + "Table.SetOutlineBorders_out.doc";
// 将文档保存到磁盘。
doc.Save(dataDir);

下面的示例展示了如何构建一个启用所有边框(网格)的表:

Document doc = new Document(dataDir + "Table.EmptyTable.doc");

Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
//清除表中任何现有的边框。
table.ClearBorders();
//在桌子周围和里面设置一个绿色边框。
table.SetBorders(LineStyle.Single, 1.5, Color.Green);

dataDir = dataDir + "Table.SetAllBorders_out.doc";
//将文档保存到磁盘。
doc.Save(dataDir);
 
▲在行级上应用格式

可以使用Row 的RowFormat属性控制行级别的格式。

Aspose.Words使用表格教程之应用格式——将格式应用于表,行和单元格

下面的示例演示如何修改表格行的格式:

Document doc = new Document(dataDir + "Table.Document.doc"); 
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);

// 检索表中的第一个单元格。
Cell firstCell = table.FirstRow.FirstCell;
//修改一些单元级属性。
firstCell.CellFormat.Width = 30; // In points
firstCell.CellFormat.Orientation = TextOrientation.Downward;
firstCell.CellFormat.Shading.ForegroundPatternColor = Color.LightGreen;
 
▲在单元级别上应用格式化

使用Cell 的CellFormat属性控制单元级别的格式。

Aspose.Words使用表格教程之应用格式——将格式应用于表,行和单元格

Aspose.Words使用表格教程之应用格式——将格式应用于表,行和单元格

下面的示例演示如何修改表格单元格的格式:

Document doc = new Document(dataDir + "Table.Document.doc"); 
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);

//检索表中的第一个单元格。
Cell firstCell = table.FirstRow.FirstCell;
//修改一些单元级属性。
firstCell.CellFormat.Width = 30; // In points
firstCell.CellFormat.Orientation = TextOrientation.Downward;
firstCell.CellFormat.Shading.ForegroundPatternColor = Color.LightGreen;

下面的代码示例显示了如何设置要添加到单元格内容的左/上/右/下的空间量(以磅为单位):

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.StartTable();
builder.InsertCell();

//设置要添加到单元格内容的左侧/顶部/右侧/底部的空间量(以点为单位)。 
builder.CellFormat.SetPaddings(30, 50, 30, 50);
builder.Writeln("I'm a wonderful formatted cell.");

builder.EndRow();
builder.EndTable();

dataDir = dataDir + "Table.SetCellPadding_out.doc";

//将文档保存到磁盘。
doc.Save(dataDir);
 
▲指定行高度

使用高度和高度规则属性控制表格行的高度。 这些可以针对表中的每一行进行不同的设置,以允许对每行的高度进行广泛控制。 在Aspose.Words中,这些由给定Row的RowFormat.Height和RowFormat.HeightRule属性表示。

 

 

高度价值描述
Auto这是给予新行的默认高度规则。 从技术上讲,这意味着没有定义高度规则。 该行的大小适合该行单元格中的最大内容。
At Least使用此设置,行的高度将增大以适应行的内容,但永远不会小于RowFormat.Height中的指定大小。
Exactly行的大小完全设置为RowFormat.Height中找到的值,并且不会增长到适合内容。

 

下面的示例显示如何创建包含单个单元格的表并应用行格式化:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Table table = builder.StartTable();
builder.InsertCell();

//设置行格式
RowFormat rowFormat = builder.RowFormat;
rowFormat.Height = 100;
rowFormat.HeightRule = HeightRule.Exactly;
//这些格式化属性设置在表上,并应用于表中的所有行。
table.LeftPadding = 30;
table.RightPadding = 30;
table.TopPadding = 30;
table.BottomPadding = 30;

builder.Writeln("I'm a wonderful formatted row.");

builder.EndRow();
builder.EndTable();

dataDir = dataDir + "Table.ApplyRowFormatting_out.doc";

//将文档保存到磁盘。
doc.Save(dataDir);
 
▲应用边框和阴影

边框和着色可以使用Table.SetBorder,Table.SetBorders和Table.SetShading在表格范围内应用,也可以仅使用CellFormat.Borders和CellFormat.Shading应用于特定单元格。 另外,可以使用RowFormat.Borders在一行上设置边框,但是不能以这种方式应用着色。

Aspose.Words使用表格教程之应用格式——将格式应用于表,行和单元格

Aspose.Words使用表格教程之应用格式——将格式应用于表,行和单元格

下面的示例演示如何使用不同的边框和阴影格式化表格和单元格。

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Table table = builder.StartTable();
builder.InsertCell();

// 为整个表设置边框。
table.SetBorders(LineStyle.Single, 2.0, Color.Black);
//为这个单元格设置单元格阴影。
builder.CellFormat.Shading.BackgroundPatternColor = Color.Red;
builder.Writeln("Cell #1");

builder.InsertCell();
//为第二个单元格指定不同的单元格着色。
builder.CellFormat.Shading.BackgroundPatternColor = Color.Green;
builder.Writeln("Cell #2");

//结束这一行。
builder.EndRow();

//清除以前操作中的单元格格式。
builder.CellFormat.ClearFormatting();

// 创建第二行。
builder.InsertCell();

//为该行的第一个单元格创建更大的边框。这将是不同的。
//与为表设置的边框相比。
builder.CellFormat.Borders.Left.LineWidth = 4.0;
builder.CellFormat.Borders.Right.LineWidth = 4.0;
builder.CellFormat.Borders.Top.LineWidth = 4.0;
builder.CellFormat.Borders.Bottom.LineWidth = 4.0;
builder.Writeln("Cell #3");

builder.InsertCell();
//清除前一个单元格的单元格格式。
builder.CellFormat.ClearFormatting();
builder.Writeln("Cell #4");
//保存完成文档。
doc.Save(dataDir + "Table.SetBordersAndShading_out.doc");

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值