溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發布時間:2020-06-20 22:54:17 來源:網絡 閱讀:919 作者:E_iceblue 欄目:編程語言

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

使用工具Spire.Doc for .NET

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


表格添加方法一:動態地向Word添加表格行和單元格內容,需調用方法section. AddTable()、table. AddRowrow. 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");
        }
    }
}

效果示例:

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

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

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");
        }
    }
}

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


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

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女