zoukankan      html  css  js  c++  java
  • 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

    以上两种方法中,鉴于文章篇幅,示例中只添加了比较简单的表格,在实际运用中,你可以根据自己的需要添加内容或者设置内容格式等。如果觉得对你有用的话,欢迎转载!

    感谢阅读。

  • 相关阅读:
    asp.net 启动关闭iis
    vue 界面关闭触发事件 ---实例销毁之前调用
    ElmentUI 设置禁止点击遮罩关闭 el-dialog 弹窗
    C#反射
    SQL Server 创建游标(cursor)
    文件解压缩
    文件流操作
    Linq查询
    C#线程 多线程 进程
    匿名类型和反射
  • 原文地址:https://www.cnblogs.com/Yesi/p/7838630.html
Copyright © 2011-2022 走看看