C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C# Word插入列表

使用C#代码在Word文档中插入列表的操作方法

作者:2501_93070778

列表在 Word 文档中用于概述、排列和强调文本,使用户能够轻松扫描和理解一系列项目,在本文中,您将学习如何使用 Spire.Doc for .NET 在 C# 和 VB.NET 中将这些类型的列表插入到 Word 文档中,需要的朋友可以参考下

列表在 Word 文档中用于概述、排列和强调文本,使用户能够轻松扫描和理解一系列项目。Word 中有三种不同类型的列表,分别是编号列表、项目符号列表和多级列表。编号列表用于表示具有顺序或优先级的项目,例如一系列的指示。项目符号列表用于表示没有特定优先级的项目,例如功能或事实列表。多级列表用于存在顺序且希望每个段落单独编号的情况。

在本文中,您将学习如何使用 Spire.Doc for .NET 在 C# 和 VB.NET 中将这些类型的列表插入到 Word 文档中。

安装 Spire.Doc for .NET

首先,您需要将 Spire.Doc for .NET 包中包含的 DLL 文件添加为 .NET 项目的引用。这些 DLL 文件可以通过此链接下载,或者通过 NuGet 安装。

PM> Install-Package Spire.Doc

在 C# 和 VB.NET 中插入编号列表

Spire.Doc for .NET 提供了 ListStyle 类,您可以使用它来创建编号列表样式或项目符号样式。然后,可以通过 Paragraph.ListFormat.ApplyStyle() 方法将列表样式应用到段落中。

创建编号列表的示例代码如下:

using Spire.Doc;
using Spire.Doc.Documents;
 
namespace CreateOrderedList
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个 Document 对象
            Document document = new Document();
            
            //添加一个节
            Section section = document.AddSection();
 
            //创建一个编号列表样式
            ListStyle listStyle = new ListStyle(document, ListType.Numbered);
            listStyle.Name = "numberedList";
            listStyle.Levels[0].PatternType = ListPatternType.DecimalEnclosedParen;
            listStyle.Levels[0].TextPosition = 20;   
            document.ListStyles.Add(listStyle);
 
            //添加一个段落
            Paragraph paragraph = section.AddParagraph();
            paragraph.AppendText("Required Web Development Skills:");
            paragraph.Format.AfterSpacing = 5f;
 
            //添加一个段落并应用编号列表样式
            paragraph = section.AddParagraph();
            paragraph.AppendText("HTML");
            paragraph.ListFormat.ApplyStyle("numberedList");
            paragraph.ListFormat.ListLevelNumber = 0;
 
            //添加另外四个段落并应用编号列表样式
            paragraph = section.AddParagraph();
            paragraph.AppendText("CSS");
            paragraph.ListFormat.ApplyStyle("numberedList");
            paragraph.ListFormat.ListLevelNumber = 0;
 
            paragraph = section.AddParagraph();
            paragraph.AppendText("JavaScript");
            paragraph.ListFormat.ApplyStyle("numberedList");
            paragraph.ListFormat.ListLevelNumber = 0;
 
            paragraph = section.AddParagraph();
            paragraph.AppendText("Python");
            paragraph.ListFormat.ApplyStyle("numberedList");
            paragraph.ListFormat.ListLevelNumber = 0;
 
            paragraph = section.AddParagraph();
            paragraph.AppendText("MySQL");
            paragraph.ListFormat.ApplyStyle("numberedList");
            paragraph.ListFormat.ListLevelNumber = 0;
 
            //保存结果文件
            document.SaveToFile("NumberedList.docx", FileFormat.Docx);
        }
    }
}

在 C# 和 VB.NET 中插入项目符号列表

创建项目符号列表的过程与创建编号列表类似。不同之处在于,在创建列表样式时,必须将列表类型指定为“项目符号”,并为其设置一个项目符号符号。以下是相关代码示例:

using Spire.Doc;
using Spire.Doc.Documents;
 
namespace CreateUnorderedList
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个 Document 对象
            Document document = new Document();
 
            //添加一个节
            Section section = document.AddSection();
 
            //创建一个项目符号列表样式
            ListStyle listStyle = new ListStyle(document, ListType.Bulleted);
            listStyle.Name = "bulletedList";
            listStyle.Levels[0].BulletCharacter = "\x00B7";
            listStyle.Levels[0].CharacterFormat.FontName = "Symbol";
            listStyle.Levels[0].TextPosition = 20;
            document.ListStyles.Add(listStyle);
 
            //添加一个段落
            Paragraph paragraph = section.AddParagraph();
            paragraph.AppendText("Computer Science Subjects:");
            paragraph.Format.AfterSpacing = 5f;
 
            //添加一个段落并应用项目符号列表样式
            paragraph = section.AddParagraph();
            paragraph.AppendText("Data Structure");
            paragraph.ListFormat.ApplyStyle("bulletedList");
            paragraph.ListFormat.ListLevelNumber = 0;
 
            //添加另外五个段落并应用项目符号列表样式
            paragraph = section.AddParagraph();
            paragraph.AppendText("Algorithm");
            paragraph.ListFormat.ApplyStyle("bulletedList");
            paragraph.ListFormat.ListLevelNumber = 0;
 
            paragraph = section.AddParagraph();
            paragraph.AppendText("Computer Networks");
            paragraph.ListFormat.ApplyStyle("bulletedList");
            paragraph.ListFormat.ListLevelNumber = 0;
 
            paragraph = section.AddParagraph();
            paragraph.AppendText("Operating System");
            paragraph.ListFormat.ApplyStyle("bulletedList");
            paragraph.ListFormat.ListLevelNumber = 0;
 
            paragraph = section.AddParagraph();
            paragraph.AppendText("C Programming");
            paragraph.ListFormat.ApplyStyle("bulletedList");
            paragraph.ListFormat.ListLevelNumber = 0;
 
            paragraph = section.AddParagraph();
            paragraph.AppendText("Theory of Computations");
            paragraph.ListFormat.ApplyStyle("bulletedList");
            paragraph.ListFormat.ListLevelNumber = 0;
 
            //保存结果文件
            document.SaveToFile("BulletedList.docx", FileFormat.Docx);
        }
    }
}

申请临时许可证

如果您希望从生成的文档中移除评估信息,或解除功能限制,请申请一份 30 天的试用许可证。

到此这篇关于使用C#代码在Word文档中插入列表的操作方法的文章就介绍到这了,更多相关C# Word插入列表内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文