C# 如何添加表格到Word文檔

來源:互聯網
上載者:User

標籤:c#   .net   添加表格   word   

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

使用工具:Spire.Doc for .NET

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


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

using System;using Spire.Doc;using Spire.Doc.Documents;using Spire.Doc.Fields;using System.Drawing;namespace CreateTable_Doc{    class Program    {        static void Main(string[] args)        {            //建立一個Document類執行個體,並添加section            Document doc = new Document();            Section section = doc.AddSection();            //添加表格            Table table = section.AddTable(true);            //添加表格第1行            TableRow row1 = table.AddRow();            //添加第1個儲存格到第1行            TableCell cell1 = row1.AddCell();            cell1.AddParagraph().AppendText("序號");            //添加第2個儲存格到第1行            TableCell cell2 = row1.AddCell();            cell2.AddParagraph().AppendText("裝置名稱");            //添加第3個儲存格到第1行            TableCell cell3 = row1.AddCell();            cell3.AddParagraph().AppendText("裝置型號");            //添加第4個儲存格到第1行            TableCell cell4 = row1.AddCell();            cell4.AddParagraph().AppendText("裝置數量");            //添加第5個儲存格到第1行            TableCell cell5 = row1.AddCell();            cell5.AddParagraph().AppendText("裝置價格");            //添加表格第2行            TableRow row2 = table.AddRow(true, false);            //添加第6個儲存格到第2行            TableCell cell6 = row2.AddCell();            cell6.AddParagraph().AppendText("1");            //添加第7個儲存格到第2行            TableCell cell7 = row2.AddCell();            cell7.AddParagraph().AppendText("機床");            //添加第8個儲存格到第2行            TableCell cell8 = row2.AddCell();            cell8.AddParagraph().AppendText("M170010");            //添加第9個儲存格到第2行            TableCell cell9 = row2.AddCell();            cell9.AddParagraph().AppendText("12");            //添加第10個儲存格到第2行            TableCell cell10 = row2.AddCell();            cell10.AddParagraph().AppendText("8W");            table.AutoFitBehavior(AutoFitBehaviorType.wdAutoFitWindow);            //儲存文檔            doc.SaveToFile("Table.docx");        }    }}

效果樣本:

650) this.width=650;" src="https://s2.51cto.com/oss/201711/15/9060ea4603536622881ebf1522817a3d.png" title="21.png" alt="9060ea4603536622881ebf1522817a3d.png" />

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

using System;using Spire.Doc;using Spire.Doc.Fields;using System.Drawing;namespace CreateTable2_Word{    class Program    {        static void Main(string[] args)        {            //建立一個Document類執行個體,並添加section            Document document = new Document();            Section section = document.AddSection();            //添加表格指定表格的行數和列數(2行,5列)            Table table = section.AddTable(true);            table.ResetCells(2, 5);            //擷取儲存格(第1行第1個儲存格)並添加常值內容,設定字型字型大小顏色等(儲存格中內容及個人化可以根據需要來進行調整)            TextRange range = table[0, 0].AddParagraph().AppendText("序號");            range.CharacterFormat.FontName = "Arial";            range.CharacterFormat.FontSize = 12;            range.CharacterFormat.TextColor = Color.Brown;            range.CharacterFormat.Bold = true;            //擷取儲存格(第1行第2個儲存格)並添加文本            range = table[0, 1].AddParagraph().AppendText("裝置名稱");            range.CharacterFormat.FontName = "Arial";            range.CharacterFormat.FontSize = 12;            range.CharacterFormat.TextColor = Color.Brown;            range.CharacterFormat.Bold = true;            //擷取儲存格(第1行第3個儲存格)並添加文本            range = table[0, 2].AddParagraph().AppendText("裝置型號");            range.CharacterFormat.FontName = "Arial";            range.CharacterFormat.FontSize = 12;            range.CharacterFormat.TextColor = Color.Brown;            range.CharacterFormat.Bold = true;            //擷取儲存格(第1行第4個儲存格)並添加文本            range = table[0, 3].AddParagraph().AppendText("裝置數量");            range.CharacterFormat.FontName = "Arial";            range.CharacterFormat.FontSize = 12;            range.CharacterFormat.TextColor = Color.Brown;            range.CharacterFormat.Bold = true;            //擷取儲存格(第1行第5個儲存格)並添加文本            range = table[0, 4].AddParagraph().AppendText("裝置價格");            range.CharacterFormat.FontName = "Arial";            range.CharacterFormat.FontSize = 12;            range.CharacterFormat.TextColor = Color.Brown;            range.CharacterFormat.Bold = true;            //擷取儲存格(第2行第1個儲存格)並添加文本            range = table[1, 0].AddParagraph().AppendText("1");            range.CharacterFormat.FontName = "Arial";            range.CharacterFormat.FontSize = 12;            //擷取儲存格(第2行第2個儲存格)並添加文本            range = table[1, 1].AddParagraph().AppendText("機床");            range.CharacterFormat.FontName = "Arial";            range.CharacterFormat.FontSize = 12;            //擷取儲存格(第2行第3個儲存格)並添加文本            range = table[1, 2].AddParagraph().AppendText("M170010");            range.CharacterFormat.FontName = "Arial";            range.CharacterFormat.FontSize = 12;            //擷取儲存格(第2行第4個儲存格)並添加文本            range = table[1, 3].AddParagraph().AppendText("12");            range.CharacterFormat.FontName = "Arial";            range.CharacterFormat.FontSize = 12;            //擷取儲存格(第2行第5個儲存格)並添加文本            range = table[1, 4].AddParagraph().AppendText("8W");            range.CharacterFormat.FontName = "Arial";            range.CharacterFormat.FontSize = 12;            //儲存文檔            document.SaveToFile("Table2.docx");        }    }}

650) this.width=650;" src="https://s4.51cto.com/oss/201711/15/92a210f29b963d31e842a8b1d2c9605b.png" title="22.png" alt="92a210f29b963d31e842a8b1d2c9605b.png" />


以上介紹的兩種方法中,你可以根據自己的需要新增內容或者設定內容格式等。如果覺得對你有用的話,歡迎轉載!感謝閱讀。

本文出自 “E-iceblue” 部落格,請務必保留此出處http://eiceblue.blog.51cto.com/13438008/1981983

C# 如何添加表格到Word文檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.