翻譯|使用教程|編輯:李顯亮|2019-08-30 14:47:22.800|閱讀 1852 次
概述:表是word文檔中常見的元素。它們允許在具有行和列的網(wǎng)格結(jié)構(gòu)中清晰地組織和顯示大量信息。它們還經(jīng)常用作頁面布局工具,并且是顯示選項卡數(shù)據(jù)(帶有選項卡停止)的更好選擇,因為它們允許更好地控制內(nèi)容的設(shè)計和布局。本文將介紹如何創(chuàng)建表格。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Aspose.Words For .Net是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔,而無需在跨平臺應(yīng)用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導(dǎo)出或轉(zhuǎn)換為固定布局文件格式和最常用的圖像/多媒體格式。
【下載Aspose.Words for .NET最新試用版】
接下來我們將進入“使用格式”的介紹,其中包括應(yīng)用格式、介紹和創(chuàng)建表、添加和拆分表以及使用列和行。
表是word文檔中常見的元素。它們允許在具有行和列的網(wǎng)格結(jié)構(gòu)中清晰地組織和顯示大量信息。它們還經(jīng)常用作頁面布局工具,并且是顯示選項卡數(shù)據(jù)(帶有選項卡停止)的更好選擇,因為它們允許更好地控制內(nèi)容的設(shè)計和布局。
表由Cell,Row和Column等元素組成。這些概念通常適用于所有表,無論它們來自Microsoft Word文檔還是HTML文檔,完全支持Aspose.Words中的表。您可以自由編輯,更改,添加和刪除表格。還支持高保真表格的渲染。
在Aspose.Words中,通常使用DocumentBuilder插入表。以下方法用于構(gòu)建表。其他方法也將用于將內(nèi)容插入表格單元格。
下面的示例演示如何使用具有默認格式的DocumentBuilder創(chuàng)建簡單表。
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // 我們稱這種方法開始構(gòu)建表. builder.StartTable(); builder.InsertCell(); builder.Write("Row 1, Cell 1 Content."); //構(gòu)建第二個單元格 builder.InsertCell(); builder.Write("Row 1, Cell 2 Content."); //調(diào)用以下方法結(jié)束行并開始新行。 builder.EndRow(); //構(gòu)建第二行的第一個單元格。 builder.InsertCell(); builder.Write("Row 2, Cell 1 Content"); //構(gòu)建第二個單元格。 builder.InsertCell(); builder.Write("Row 2, Cell 2 Content."); builder.EndRow(); //表示我們已經(jīng)完成了構(gòu)建表的信號。 builder.EndTable(); dataDir = dataDir + "DocumentBuilder.CreateSimpleTable_out.doc"; // 將文檔保存到磁盤。 doc.Save(dataDir);
下面的示例演示如何使用DocumentBuilder創(chuàng)建格式化表。
Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); Table table = builder.StartTable(); //制作標題行 builder.InsertCell(); //設(shè)置表格的左縮進。必須在之后應(yīng)用表格寬格式 //表格中至少有一行。 table.LeftIndent = 20.0; //設(shè)置高度并定義標題行的高度規(guī)則。 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(); //設(shè)置其他行和單元格的功能。 builder.CellFormat.Shading.BackgroundPatternColor = Color.White; builder.CellFormat.Width = 100.0; builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; //重置高度并為表體定義不同的高度規(guī)則 builder.RowFormat.Height = 30.0; builder.RowFormat.HeightRule = HeightRule.Auto; builder.InsertCell(); //重置字體格式。 builder.Font.Size = 12; builder.Font.Bold = false; //構(gòu)建其他單元格。 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); // 構(gòu)建外部表。 Cell cell = builder.InsertCell(); builder.Writeln("Outer Table Cell 1"); builder.InsertCell(); builder.Writeln("Outer Table Cell 2"); //為了在第一個表中創(chuàng)建嵌套表,此調(diào)用很重要 //如果沒有此調(diào)用,下面插入的單元格將附加到外部表格。 builder.EndTable(); // 移動到外部表的第一個單元格。 builder.MoveTo(cell.FirstParagraph); //構(gòu)建內(nèi)部表 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);
要將表直接插入到特定節(jié)點位置的DOM中,使用DocumentBuilder創(chuàng)建表時使用相同的表默認值。 要在不使用DocumentBuilder的情況下從頭構(gòu)建新表,首先使用適當?shù)臉?gòu)造函數(shù)創(chuàng)建一個新的Table 節(jié)點,然后將其添加到文檔樹中。
下面的示例演示如何使用節(jié)點的構(gòu)造函數(shù)插入表。
//文檔目錄的路徑。 string dataDir = RunExamples.GetDataDir_WorkingWithTables(); Document doc = new Document(); //我們從創(chuàng)建表對象開始。請注意我們必須如何傳遞文檔對象 // 到每個節(jié)點的構(gòu)造函數(shù)。這是因為我們創(chuàng)建的每個節(jié)點都必須屬于 //對某些文件 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); //我們現(xiàn)在可以應(yīng)用任何自動調(diào)整設(shè)置。 table.AutoFit(AutoFitBehavior.FixedColumnWidths); //創(chuàng)建一個單元格并將其添加到行中 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); //然后,我們將對表中的其他單元格和行重復(fù)此過程。 //我們還可以通過克隆現(xiàn)有的單元格和行來加快速度。 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);
通常有時您在文檔中有現(xiàn)有表并希望添加此表的副本然后應(yīng)用一些修改。在保留所有格式的同時復(fù)制表的最簡單方法是使用Table.Clone方法克隆表節(jié)點。下面的示例演示如何使用節(jié)點的構(gòu)造函數(shù)插入表。
Document doc = new Document(dataDir + "Table.SimpleTable.doc"); //檢索文檔中的第一個表 Table table = (Table)doc.GetChild(NodeType.Table, 0, true); //創(chuàng)建表的克隆。 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方法將內(nèi)容從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正版授權(quán)可聯(lián)系哦~
ASPOSE技術(shù)交流QQ群已開通,各類資源及時分享,歡迎交流討論!(掃描下方二維碼加入群聊)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn