翻譯|使用教程|編輯:胡濤|2023-02-21 11:16:21.530|閱讀 253 次
概述:本文介紹了如何在 C# 中使用 Spire.Doc 創建嵌套表,歡迎查閱
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發,不依賴第三方軟件,不受其他國家的技術或法律法規限制,同時適配國產操作系統如中科方德、中標麒麟等,兼容國產文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
嵌套表是一個表放在另一個表的內部,其中較大的表充當較小表的容器。嵌套表格允許您分組排列不同的數據集以顯示給客戶。
本文介紹了如何在 C# 中使用 Spire.Doc 創建嵌套表。
第 1 步:創建一個新的 PDF 文檔并向其中添加一個部分。
Document doc = new Document(); Section section = doc.AddSection();
第 2 步:將表格添加到該部分。
Table table = section.AddTable(true); table.ResetCells(2, 3);
第 3 步:調整列。
table.Rows[0].Cells[0].Width = table.Rows[0].Cells[2].Width = 50F; table.Rows[1].Cells[0].Width = table.Rows[1].Cells[2].Width = 50F; table.AutoFitBehavior(AutoFitBehaviorType.wdAutoFitWindow);
第 4 步:將內容插入表格的單元格。
table[0, 0].AddParagraph().AppendText("SI.No."); string text = "Earthwork excavation for foundation of buildings, water supply, " + "sanitary lines and electrical conduits either in pits or in " + "trenches 1.5m and above in width, in ordinary soil not exceeding " + "1.5m in depth including dressing the bottom and sides of pits and " + "trenches, stacking the excavated soil clear."; table[0, 1].AddParagraph().AppendText(text); table[0, 2].AddParagraph().AppendText("Qty");
第 5 步:將嵌套表格插入單元格(第一行,第二列)。
Table nestedTable= table[0, 1].AddTable(true); nestedTable.ResetCells(3, 4); nestedTable.AutoFitBehavior(AutoFitBehaviorType.wdAutoFitContents);
第 6 步:向嵌套單元格添加內容。
nestedTable[0, 0].AddParagraph().AppendText("SI.No."); nestedTable[0, 1].AddParagraph().AppendText("Item"); nestedTable[0, 2].AddParagraph().AppendText("Qty"); nestedTable[0, 3].AddParagraph().AppendText("Rate"); nestedTable[1, 0].AddParagraph().AppendText("1"); nestedTable[1, 1].AddParagraph().AppendText("Sand"); nestedTable[1, 2].AddParagraph().AppendText("30"); nestedTable[1, 3].AddParagraph().AppendText("45"); nestedTable[2, 0].AddParagraph().AppendText("2"); nestedTable[2, 1].AddParagraph().AppendText("Cement"); nestedTable[2, 2].AddParagraph().AppendText("30"); nestedTable[2, 3].AddParagraph().AppendText("50");
第 7 步:保存文件。
doc.SaveToFile("Nested_Table.docx", FileFormat.Docx2013);
輸出:
完整代碼:
//create a new pdf document Document doc = new Document(); Section section = doc.AddSection(); //add a table Table table = section.AddTable(true); table.ResetCells(2, 3); //set column width table.Rows[0].Cells[0].Width = table.Rows[0].Cells[2].Width = 50F; table.Rows[1].Cells[0].Width = table.Rows[1].Cells[2].Width = 50F; table.AutoFitBehavior(AutoFitBehaviorType.wdAutoFitWindow); //insert content to cells table[0, 0].AddParagraph().AppendText("SI.No."); string text = "Earthwork excavation for foundation of buildings, water supply, " + "sanitary lines and electrical conduits either in pits or in " + "trenches 1.5m and above in width, in ordinary soil not exceeding " + "1.5m in depth including dressing the bottom and sides of pits and " + "trenches, stacking the excavated soil clear."; table[0, 1].AddParagraph().AppendText(text); table[0, 2].AddParagraph().AppendText("Qty"); //add a nested table to cell(first row, second column) Table nestedTable= table[0, 1].AddTable(true); nestedTable.ResetCells(3, 4); nestedTable.AutoFitBehavior(AutoFitBehaviorType.wdAutoFitContents); //add content to nested cells nestedTable[0, 0].AddParagraph().AppendText("SI.No."); nestedTable[0, 1].AddParagraph().AppendText("Item"); nestedTable[0, 2].AddParagraph().AppendText("Qty"); nestedTable[0, 3].AddParagraph().AppendText("Rate"); nestedTable[1, 0].AddParagraph().AppendText("1"); nestedTable[1, 1].AddParagraph().AppendText("Sand"); nestedTable[1, 2].AddParagraph().AppendText("30"); nestedTable[1, 3].AddParagraph().AppendText("45"); nestedTable[2, 0].AddParagraph().AppendText("2"); nestedTable[2, 1].AddParagraph().AppendText("Cement"); nestedTable[2, 2].AddParagraph().AppendText("30"); nestedTable[2, 3].AddParagraph().AppendText("50"); //save doc.SaveToFile("Nested_Table.docx", FileFormat.Docx2013);
以上便是如何在 C# 中的 Word 中創建嵌套表格,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn