建立表格
Aspose.Words 允許使用者從頭開始建立文件中的表格,並提供幾種不同的方法來做。 這篇文章會提供如何在每個方法中加入格式化的表格到你的文件的細節,並會在文章的最後比較每個方法。
預設表格樣式
新建立的表格具有與在 Microsoft Word 中使用的值類似的預設值。
桌面属性 | 預設在 Aspose.Words |
---|---|
Border Style |
Single |
Border Width |
1/2 pt |
Border Color |
Black |
Left and Right Padding |
5.4 pts |
AutoFit Mode |
AutoFit to Window |
Allow AutoFit |
True |
透過DocumentBuilder建立一張表格
在 Aspose.Words 中,使用者可以用 DocumentBuilder 在文件中建立一個表格。 建立表格的基本演算法如下:
- 開始該表,以 StartTable 開頭
- 透過 InsertCell 將一列新增至表格中–這會自動開始新的資料列。 3。 可選地,使用 CellFormat 屬性來指定單元格格式化
- 透過合適的 DocumentBuilder 方法插入細胞內容,例如 Writeln、InsertImage 等。
- 重覆步驟2-4,直到該列完成
- 呼叫 EndRow 來結束當前的列
- 工 作 可選地,使用 RowFormat 屬性來指定列格式
- 在完成桌子之前重複步驟2到7
- 打電話給 EndTable 完成製作桌子。
重要細節:
- StartTable 在一個細胞內也可以被叫出,這種情況下它會開始建立一個細胞內的嵌套表格。
- 經 InsertCell 調用後,會建立新的細胞。使用 DocumentBuilder 類別中其他方法加入的任何內容都會新增到目前的細胞。 要在同一列中建立新的單元格,請再次呼叫 InsertCell。
- 如果 InsertCell 在 EndRow 的緊接之後立即被調用,而列的結束時也同時發生,表將繼續在一新列中。
- 第 EndTable 種結束表格的方法只應該在調用 EndRow 之後一次調用。 點選 EndTable 移動光標從目前的細胞到表格下方的位置。
建立表格的過程可以在下列圖片中清楚地看到:
接下來的程式碼範例顯示了如何使用 DocumentBuilder 來建立一個簡單的表格,並使用預設的格式:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Start building the table. | |
builder.StartTable(); | |
builder.InsertCell(); | |
builder.Write("Row 1, Cell 1 Content."); | |
// Build the second cell. | |
builder.InsertCell(); | |
builder.Write("Row 1, Cell 2 Content."); | |
// Call the following method to end the row and start a new row. | |
builder.EndRow(); | |
// Build the first cell of the second row. | |
builder.InsertCell(); | |
builder.Write("Row 2, Cell 1 Content"); | |
// Build the second cell. | |
builder.InsertCell(); | |
builder.Write("Row 2, Cell 2 Content."); | |
builder.EndRow(); | |
// Signal that we have finished building the table. | |
builder.EndTable(); | |
doc.Save(ArtifactsDir + "WorkingWithTables.CreateSimpleTable.docx"); |
接下來的程式碼範例示範了如何透過 DocumentBuilder 建立格式化表:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Table table = builder.StartTable(); | |
builder.InsertCell(); | |
// Table wide formatting must be applied after at least one row is present in the table. | |
table.LeftIndent = 20.0; | |
// Set height and define the height rule for the header row. | |
builder.RowFormat.Height = 40.0; | |
builder.RowFormat.HeightRule = HeightRule.AtLeast; | |
builder.CellFormat.Shading.BackgroundPatternColor = Color.FromArgb(198, 217, 241); | |
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; | |
builder.Font.Size = 16; | |
builder.Font.Name = "Arial"; | |
builder.Font.Bold = true; | |
builder.CellFormat.Width = 100.0; | |
builder.Write("Header Row,\n Cell 1"); | |
// We don't need to specify this cell's width because it's inherited from the previous cell. | |
builder.InsertCell(); | |
builder.Write("Header Row,\n Cell 2"); | |
builder.InsertCell(); | |
builder.CellFormat.Width = 200.0; | |
builder.Write("Header Row,\n Cell 3"); | |
builder.EndRow(); | |
builder.CellFormat.Shading.BackgroundPatternColor = Color.White; | |
builder.CellFormat.Width = 100.0; | |
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; | |
// Reset height and define a different height rule for table body. | |
builder.RowFormat.Height = 30.0; | |
builder.RowFormat.HeightRule = HeightRule.Auto; | |
builder.InsertCell(); | |
// Reset font formatting. | |
builder.Font.Size = 12; | |
builder.Font.Bold = false; | |
builder.Write("Row 1, Cell 1 Content"); | |
builder.InsertCell(); | |
builder.Write("Row 1, Cell 2 Content"); | |
builder.InsertCell(); | |
builder.CellFormat.Width = 200.0; | |
builder.Write("Row 1, Cell 3 Content"); | |
builder.EndRow(); | |
builder.InsertCell(); | |
builder.CellFormat.Width = 100.0; | |
builder.Write("Row 2, Cell 1 Content"); | |
builder.InsertCell(); | |
builder.Write("Row 2, Cell 2 Content"); | |
builder.InsertCell(); | |
builder.CellFormat.Width = 200.0; | |
builder.Write("Row 2, Cell 3 Content."); | |
builder.EndRow(); | |
builder.EndTable(); | |
doc.Save(ArtifactsDir + "WorkingWithTables.FormattedTable.docx"); |
接下來的程式碼範例示範了如何透過 DocumentBuilder 插入嵌套的表格。
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Cell cell = builder.InsertCell(); | |
builder.Writeln("Outer Table Cell 1"); | |
builder.InsertCell(); | |
builder.Writeln("Outer Table Cell 2"); | |
// This call is important to create a nested table within the first table. | |
// Without this call, the cells inserted below will be appended to the outer table. | |
builder.EndTable(); | |
// Move to the first cell of the outer table. | |
builder.MoveTo(cell.FirstParagraph); | |
// Build the inner table. | |
builder.InsertCell(); | |
builder.Writeln("Inner Table Cell 1"); | |
builder.InsertCell(); | |
builder.Writeln("Inner Table Cell 2"); | |
builder.EndTable(); | |
doc.Save(ArtifactsDir + "WorkingWithTables.NestedTable.docx"); |
透過 DOM ( Document Object Model )建立資料表
您可以直接在 DOM 中插入資料表,方法是將新的 Table 節點加入特定位置。
請注意,在表結點創建後立即,表本身將完全是空的,也就是它尚未包含行和單元格。 若要將列和セル插入到表中,請將適當的 Row 和 Cell 子節點新增到 DOM。
接下來這段程式碼示例展示了如何從頭建立新的表格,透過向文件樹中加入適當的子節點:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
// We start by creating the table object. Note that we must pass the document object | |
// to the constructor of each node. This is because every node we create must belong | |
// to some document. | |
Table table = new Table(doc); | |
doc.FirstSection.Body.AppendChild(table); | |
// Here we could call EnsureMinimum to create the rows and cells for us. This method is used | |
// to ensure that the specified node is valid. In this case, a valid table should have at least one Row and one cell. | |
// Instead, we will handle creating the row and table ourselves. | |
// This would be the best way to do this if we were creating a table inside an algorithm. | |
Row row = new Row(doc); | |
row.RowFormat.AllowBreakAcrossPages = true; | |
table.AppendChild(row); | |
Cell cell = new Cell(doc); | |
cell.CellFormat.Shading.BackgroundPatternColor = Color.LightBlue; | |
cell.CellFormat.Width = 80; | |
cell.AppendChild(new Paragraph(doc)); | |
cell.FirstParagraph.AppendChild(new Run(doc, "Row 1, Cell 1 Text")); | |
row.AppendChild(cell); | |
// We would then repeat the process for the other cells and rows in the table. | |
// We can also speed things up by cloning existing cells and rows. | |
row.AppendChild(cell.Clone(false)); | |
row.LastCell.AppendChild(new Paragraph(doc)); | |
row.LastCell.FirstParagraph.AppendChild(new Run(doc, "Row 1, Cell 2 Text")); | |
// We can now apply any auto fit settings. | |
table.AutoFit(AutoFitBehavior.FixedColumnWidths); | |
doc.Save(ArtifactsDir + "WorkingWithTables.InsertTableDirectly.docx"); |
從 HTML 建立表格
Aspose.Words 支援透過 InsertHtml 方法從 HTML 來源將內容插入到文件中。 輸入可以是完整的 HTML 頁面,也可以是部分片段。
使用 InsertHtml 方法,使用者可以用像 <table>
、<tr>
和 <td>
的表格標籤來在文件中插入表格。
以下範例展示了如何從包含 HTML 標籤的文字中插入一張表格到文件中:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Note that AutoFitSettings does not apply to tables inserted from HTML. | |
builder.InsertHtml("<table>" + | |
"<tr>" + | |
"<td>Row 1, Cell 1</td>" + | |
"<td>Row 1, Cell 2</td>" + | |
"</tr>" + | |
"<tr>" + | |
"<td>Row 2, Cell 2</td>" + | |
"<td>Row 2, Cell 2</td>" + | |
"</tr>" + | |
"</table>"); | |
doc.Save(ArtifactsDir + "WorkingWithTables.InsertTableFromHtml.docx"); |
插入現有表格的副本
在您需要根據一張已經存在的文件表格來建立新的表格時,經常會碰到這種情況。 最簡單的複製桌面方法,同時保留所有格式,就是透過 Clone 方法複製Table節點。
相同的技術可用於將現有的行或列複製到表格中。
以下範例展示了如何使用節點生成器複製一張表格:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Tables.docx"); | |
Table table = (Table) doc.GetChild(NodeType.Table, 0, true); | |
// Clone the table and insert it into the document after the original. | |
Table tableClone = (Table) table.Clone(true); | |
table.ParentNode.InsertAfter(tableClone, table); | |
// Insert an empty paragraph between the two tables, | |
// or else they will be combined into one upon saving this has to do with document validation. | |
table.ParentNode.InsertAfter(new Paragraph(doc), table); | |
doc.Save(ArtifactsDir + "WorkingWithTables.CloneCompleteTable.docx"); |
以下範例示出了如何複製一張表的最後一行並附加到表中:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git. | |
Document doc = new Document(MyDir + "Tables.docx"); | |
Table table = (Table) doc.GetChild(NodeType.Table, 0, true); | |
Row clonedRow = (Row) table.LastRow.Clone(true); | |
// Remove all content from the cloned row's cells. This makes the row ready for new content to be inserted into. | |
foreach (Cell cell in clonedRow.Cells) | |
cell.RemoveAllChildren(); | |
table.AppendChild(clonedRow); | |
doc.Save(ArtifactsDir + "WorkingWithTables.CloneLastRow.docx"); |
如果您正在建立一個由資料源中各筆資料動態成長的文件中的表格,那麼上述方法不建議使用。 取而代之,我們更容易透過使用具有區域之 Mail merge 來達到預期的輸出。 您可以在 Mail Merge with Regions部分中了解更多有關此技術的資訊。
比較創建一張表格的方式
Aspose.Words 提供幾個方法來在文件中建立新表。 每個方法都有其優缺點,因此選擇要使用的方法通常取決於具體的情況。
讓我們更仔細地看這些創造表格的方式,並比較它们的優缺點:
方法 | 優點 | 缺點 |
---|---|---|
Via DocumentBuilder |
插入表格和其他文書內容的標準方法 | 有時很難在一個建造器實例下同時建立許多不同的樣式。 |
透過 DOM | 直接將節點插入到 DOM 中,而不使用 DocumentBuilder 的程式碼會更好。 | 該表格是空的:在執行大多數作業之前,您必須呼叫 EnsureMinimum 以建立任何缺失的子節點。 |
來自HTML | 可以用標籤 <table> 、<tr> 、<td> 等從 HTML 來源來建立新的資料表。 |
並非所有可能的 Microsoft Word 表格格式都能適用於 HTML |
複製現有的資料表 | 您可以建立一個現有資料表的複製,同時保留所有列和格子的格式。 | 在使用前必須先移除適當的子節點。 |