C# 在Word中添加表格的方法


表格是組織整理數據的一種重要手段,應在生活中的方方面面。在Word文檔中將繁雜的文字表述內容表格化,能快速、直接地獲取關鍵內容信息。那么,通過C#,我們也可以在Word文檔中添加表格,這里將介紹兩種不同的表格添加方法。

使用工具Spire.Doc for .NET

使用方法:安裝后,添加引用dll文件到項目中即可

表格添加方法一:動態地向Word添加表格行和單元格內容,需調用方法section. AddTable()、table. AddRow和row. AddCell()

 1 using System;
 2 using Spire.Doc;
 3 using Spire.Doc.Documents;
 4 using Spire.Doc.Fields;
 5 using System.Drawing;
 6 
 7 
 8 namespace CreateTable_Doc
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             //創建一個Document類實例,並添加section
15             Document doc = new Document();
16             Section section = doc.AddSection();
17 
18             //添加表格
19             Table table = section.AddTable(true);
20 
21             //添加表格第1行
22             TableRow row1 = table.AddRow();
23 
24             //添加第1個單元格到第1行
25             TableCell cell1 = row1.AddCell();
26             cell1.AddParagraph().AppendText("序列號");
27 
28             //添加第2個單元格到第1行
29             TableCell cell2 = row1.AddCell();
30             cell2.AddParagraph().AppendText("設備名稱");
31 
32             //添加第3個單元格到第1行
33             TableCell cell3 = row1.AddCell();
34             cell3.AddParagraph().AppendText("設備型號");
35 
36             //添加第4個單元格到第1行
37             TableCell cell4 = row1.AddCell();
38             cell4.AddParagraph().AppendText("設備數量");
39 
40             //添加第5個單元格到第1行
41             TableCell cell5 = row1.AddCell();
42             cell5.AddParagraph().AppendText("設備價格");
43 
44 
45             //添加表格第2行
46             TableRow row2 = table.AddRow(true, false);
47 
48             //添加第6個單元格到第2行
49             TableCell cell6 = row2.AddCell();
50             cell6.AddParagraph().AppendText("1");
51 
52             //添加第7個單元格到第2行
53             TableCell cell7 = row2.AddCell();
54             cell7.AddParagraph().AppendText("機床");
55 
56             //添加第8個單元格到第2行
57             TableCell cell8 = row2.AddCell();
58             cell8.AddParagraph().AppendText("M170010");
59 
60             //添加第9個單元格到第2行
61             TableCell cell9 = row2.AddCell();
62             cell9.AddParagraph().AppendText("12");
63 
64             //添加第10個單元格到第2行
65             TableCell cell10 = row2.AddCell();
66             cell10.AddParagraph().AppendText("8W");
67             table.AutoFitBehavior(AutoFitBehaviorType.wdAutoFitWindow);
68 
69             //保存文檔
70             doc.SaveToFile("Table.docx");
71         }
72     }
73 }
View Code

表格添加方法二:預定義表格行和列

 1 using System;
 2 using Spire.Doc;
 3 using Spire.Doc.Fields;
 4 using System.Drawing;
 5 
 6 namespace CreateTable2_Word
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             //創建一個Document類實例,並添加section
13             Document document = new Document();
14             Section section = document.AddSection();
15 
16             //添加表格指定表格的行數和列數(2行,5列)
17             Table table = section.AddTable(true);
18             table.ResetCells(2, 5);
19 
20             //獲取單元格(第1行第1個單元格)並添加文本內容,設置字體字號顏色等(單元格中內容及個性化設置可以根據需要來進行調整)
21             TextRange range = table[0, 0].AddParagraph().AppendText("序列號");
22             range.CharacterFormat.FontName = "Arial";
23             range.CharacterFormat.FontSize = 12;
24             range.CharacterFormat.TextColor = Color.Brown;
25             range.CharacterFormat.Bold = true;
26 
27             //獲取單元格(第1行第2個單元格)並添加文本
28             range = table[0, 1].AddParagraph().AppendText("設備名稱");
29             range.CharacterFormat.FontName = "Arial";
30             range.CharacterFormat.FontSize = 12;
31             range.CharacterFormat.TextColor = Color.Brown;
32             range.CharacterFormat.Bold = true;
33 
34             //獲取單元格(第1行第3個單元格)並添加文本
35             range = table[0, 2].AddParagraph().AppendText("設備型號");
36             range.CharacterFormat.FontName = "Arial";
37             range.CharacterFormat.FontSize = 12;
38             range.CharacterFormat.TextColor = Color.Brown;
39             range.CharacterFormat.Bold = true;
40 
41             //獲取單元格(第1行第4個單元格)並添加文本
42             range = table[0, 3].AddParagraph().AppendText("設備數量");
43             range.CharacterFormat.FontName = "Arial";
44             range.CharacterFormat.FontSize = 12;
45             range.CharacterFormat.TextColor = Color.Brown;
46             range.CharacterFormat.Bold = true;
47 
48             //獲取單元格(第1行第5個單元格)並添加文本
49             range = table[0, 4].AddParagraph().AppendText("設備價格");
50             range.CharacterFormat.FontName = "Arial";
51             range.CharacterFormat.FontSize = 12;
52             range.CharacterFormat.TextColor = Color.Brown;
53             range.CharacterFormat.Bold = true;
54 
55             //獲取單元格(第2行第1個單元格)並添加文本
56             range = table[1, 0].AddParagraph().AppendText("1");
57             range.CharacterFormat.FontName = "Arial";
58             range.CharacterFormat.FontSize = 12;
59 
60             //獲取單元格(第2行第2個單元格)並添加文本
61             range = table[1, 1].AddParagraph().AppendText("機床");
62             range.CharacterFormat.FontName = "Arial";
63             range.CharacterFormat.FontSize = 12;
64 
65             //獲取單元格(第2行第3個單元格)並添加文本
66             range = table[1, 2].AddParagraph().AppendText("M170010");
67             range.CharacterFormat.FontName = "Arial";
68             range.CharacterFormat.FontSize = 12;
69 
70             //獲取單元格(第2行第4個單元格)並添加文本
71             range = table[1, 3].AddParagraph().AppendText("12");
72             range.CharacterFormat.FontName = "Arial";
73             range.CharacterFormat.FontSize = 12;
74 
75             //獲取單元格(第2行第5個單元格)並添加文本
76             range = table[1, 4].AddParagraph().AppendText("8W");
77             range.CharacterFormat.FontName = "Arial";
78             range.CharacterFormat.FontSize = 12;
79 
80             //保存文檔
81             document.SaveToFile("Table2.docx");
82         }
83     }
84 }
View Code

以上兩種方法中,鑒於文章篇幅,示例中只添加了比較簡單的表格,在實際運用中,你可以根據自己的需要添加內容或者設置內容格式等。如果覺得對你有用的話,歡迎轉載!

感謝閱讀。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM