翻譯|使用教程|編輯:李顯亮|2019-08-30 14:47:22.800|閱讀 1852 次
概述:表是word文檔中常見的元素。它們允許在具有行和列的網格結構中清晰地組織和顯示大量信息。它們還經常用作頁面布局工具,并且是顯示選項卡數據(帶有選項卡停止)的更好選擇,因為它們允許更好地控制內容的設計和布局。本文將介紹如何創建表格。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.Words For .Net是一種高級Word文檔處理API,用于執行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
【下載Aspose.Words for .NET最新試用版】
接下來我們將進入“使用格式”的介紹,其中包括應用格式、介紹和創建表、添加和拆分表以及使用列和行。
表是word文檔中常見的元素。它們允許在具有行和列的網格結構中清晰地組織和顯示大量信息。它們還經常用作頁面布局工具,并且是顯示選項卡數據(帶有選項卡停止)的更好選擇,因為它們允許更好地控制內容的設計和布局。
表由Cell,Row和Column等元素組成。這些概念通常適用于所有表,無論它們來自Microsoft Word文檔還是HTML文檔,完全支持Aspose.Words中的表。您可以自由編輯,更改,添加和刪除表格。還支持高保真表格的渲染。
在Aspose.Words中,通常使用DocumentBuilder插入表。以下方法用于構建表。其他方法也將用于將內容插入表格單元格。
下面的示例演示如何使用具有默認格式的DocumentBuilder創建簡單表。
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // 我們稱這種方法開始構建表. builder.StartTable(); builder.InsertCell(); builder.Write("Row 1, Cell 1 Content."); //構建第二個單元格 builder.InsertCell(); builder.Write("Row 1, Cell 2 Content."); //調用以下方法結束行并開始新行。 builder.EndRow(); //構建第二行的第一個單元格。 builder.InsertCell(); builder.Write("Row 2, Cell 1 Content"); //構建第二個單元格。 builder.InsertCell(); builder.Write("Row 2, Cell 2 Content."); builder.EndRow(); //表示我們已經完成了構建表的信號。 builder.EndTable(); dataDir = dataDir + "DocumentBuilder.CreateSimpleTable_out.doc"; // 將文檔保存到磁盤。 doc.Save(dataDir);
下面的示例演示如何使用DocumentBuilder創建格式化表。
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.StartTable(); //制作標題行 builder.InsertCell(); //設置表格的左縮進。必須在之后應用表格寬格式 //表格中至少有一行。 table.LeftIndent = 20.0; //設置高度并定義標題行的高度規則。 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"); //我們不需要指定此單元格的寬度,因為它是從前一個單元格繼承的。 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; //重置高度并為表體定義不同的高度規則 builder.RowFormat.Height = 30.0; builder.RowFormat.HeightRule = HeightRule.Auto; builder.InsertCell(); //重置字體格式。 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(); dataDir = dataDir + "DocumentBuilder.CreateFormattedTable_out.doc"; //將文檔保存到磁盤。 doc.Save(dataDir);
下面的示例演示如何使用DocumentBuilder插入嵌套表。
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"); //為了在第一個表中創建嵌套表,此調用很重要 //如果沒有此調用,下面插入的單元格將附加到外部表格。 builder.EndTable(); // 移動到外部表的第一個單元格。 builder.MoveTo(cell.FirstParagraph); //構建內部表 builder.InsertCell(); builder.Writeln("Inner Table Cell 1"); builder.InsertCell(); builder.Writeln("Inner Table Cell 2"); builder.EndTable(); dataDir = dataDir + "DocumentBuilder.InsertNestedTable_out.doc"; //將文檔保存到磁盤。 doc.Save(dataDir);
要將表直接插入到特定節點位置的DOM中,使用DocumentBuilder創建表時使用相同的表默認值。 要在不使用DocumentBuilder的情況下從頭構建新表,首先使用適當的構造函數創建一個新的Table 節點,然后將其添加到文檔樹中。
下面的示例演示如何使用節點的構造函數插入表。
//文檔目錄的路徑。 string dataDir = RunExamples.GetDataDir_WorkingWithTables(); Document doc = new 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, therefore this method creates them for us. // 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 algorthim for example. Row row = new Row(doc); row.RowFormat.AllowBreakAcrossPages = true; table.AppendChild(row); //我們現在可以應用任何自動調整設置。 table.AutoFit(AutoFitBehavior.FixedColumnWidths); //創建一個單元格并將其添加到行中 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); //然后,我們將對表中的其他單元格和行重復此過程。 //我們還可以通過克隆現有的單元格和行來加快速度。 row.AppendChild(cell.Clone(false)); row.LastCell.AppendChild(new Paragraph(doc)); row.LastCell.FirstParagraph.AppendChild(new Run(doc, "Row 1, Cell 2 Text")); dataDir = dataDir + "Table.InsertTableUsingNodes_out.doc"; //將文檔保存到磁盤。 doc.Save(dataDir);
通常有時您在文檔中有現有表并希望添加此表的副本然后應用一些修改。在保留所有格式的同時復制表的最簡單方法是使用Table.Clone方法克隆表節點。下面的示例演示如何使用節點的構造函數插入表。
Document doc = new Document(dataDir + "Table.SimpleTable.doc"); //檢索文檔中的第一個表 Table table = (Table)doc.GetChild(NodeType.Table, 0, true); //創建表的克隆。 Table tableClone = (Table)table.Clone(true); //將克隆表插入原始文檔后的文檔中 table.ParentNode.InsertAfter(tableClone, table); //在兩個表之間插入一個空段,否則它們將合并為一個 // Upon save. This has to do with document validation. table.ParentNode.InsertAfter(new Paragraph(doc), table); dataDir = dataDir + "Table.CloneTableAndInsert_out.doc"; //將文檔保存到磁盤。 doc.Save(dataDir);
Aspose.Words支持使用DocumentBuilder.InsertHtml方法將內容從HTML源插入到文檔中。輸入可以是完整的HTML頁面,也可以只是部分代碼段。使用此方法,我們可以使用表元素(例如
,,
)將表插入到文檔中。下面的示例演示如何從包含HTML標記的字符串中插入文檔中的表。 |
//文檔目錄的路徑。 string dataDir = RunExamples.GetDataDir_WorkingWithTables(); Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); //從HTML插入表格。請注意,AutoFitSettings不適用于表 //從HTML插入 builder.InsertHtml("" + "" + "Row 1, Cell 1" + "Row 1, Cell 2" + "" + "" + "Row 2, Cell 2" + "Row 2, Cell 2" + "" + ""); dataDir = dataDir + "DocumentBuilder.InsertTableFromHtml_out.doc"; //將文檔保存到磁盤。 doc.Save(dataDir);
*想要獲取Aspose.Words正版授權可聯系哦~
ASPOSE技術交流QQ群已開通,各類資源及時分享,歡迎交流討論!(掃描下方二維碼加入群聊)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn