天天看點

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

表格是組織整理資料的一種重要手段,應在生活中的方方面面。在Word文檔中将繁雜的文字表述内容表格化,能快速、直接地擷取關鍵内容資訊。那麼,通過C#,我們也可以在Word文檔中添加表格,這裡将介紹兩種不同的表格添加方法。

使用工具:Spire.Doc for .NET

使用方法:安裝後,添加引用dll檔案到項目中即可

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

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

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

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

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

C# 在Word中添加表格的方法
C# 在Word中添加表格的方法
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 }      
C# 在Word中添加表格的方法

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

感謝閱讀。