1. 程式人生 > >如何利用Aspose.Words將格式應用於表,行和單元格?示例演示帶你全面瞭解!

如何利用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.do