如何通过C#代码在Word文档中插入有序列表和无序列表

前言:

编辑 Word 文档时,使用有序列表或无序列表有助于我们更好地组织文档内容,使其逻辑关系更为直观易懂。例如在文中创建多个小标题,又或是列举多个同类型事务等等情况。 其中有序列表会按特定的顺序来排列内容,而无序列表中则没有特定的排列顺序,每个项目前面都有一个符号或标记。 以上两个列表均可以通过C#代码实现。下面是方法介绍。

准备工作

在这篇教程中,所使用的类库是Free Spire.Doc for .NET(免费版),支持在.NET平台处理Word文档。有以下两种方法安装:

方法一:

直接在Visual Studio中搜索安装

  • 先在Visual Studio中创建一个C#项目并打开。
  • 依次选择“解决方案资源管理器”>右键“引用”> “管理NuGet程序包”
  • 搜索Free Spire.Doc for .NET并安装。

方法二:

在程序中手引入Spire.doc.dll文件

  • 下载并安装Free Spire.Doc for .NET 
  • 在Visual Studio中创建一个C#项目并打开。
  • “解决方案源管理器”中右键“引用”。
  • 依次选择“添加引用”> 浏览,找到安装路径下BIN文件夹中的dll文件,点“确定”

推荐第二种安装方法,这样更便于在安装的“SampleCenter.exe”中查找各种参考代码。

参考代码:

有序列表

想要在Word文档中插入有序列表,可以通过Free Spire.Doc for .NET所提供的ListStyle 类创建一个Numbered列表样式。然后再调用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 section = document.AddSection();

            //创建一个Numbered列表样式
            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("2022中国城市GDP排名");
            paragraph.Format.AfterSpacing = 5f;

            //添加其他段落、应用列表样式
            paragraph = section.AddParagraph();
            paragraph.AppendText("上海");
            paragraph.ListFormat.ApplyStyle("numberedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            paragraph = section.AddParagraph();
            paragraph.AppendText("北京");
            paragraph.ListFormat.ApplyStyle("numberedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            paragraph = section.AddParagraph();
            paragraph.AppendText("深圳");
            paragraph.ListFormat.ApplyStyle("numberedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            paragraph = section.AddParagraph();
            paragraph.AppendText("重庆");
            paragraph.ListFormat.ApplyStyle("numberedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            paragraph = section.AddParagraph();
            paragraph.AppendText("广州");
            paragraph.ListFormat.ApplyStyle("numberedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            //保存结果文档
            document.SaveToFile("有序列表.docx", FileFormat.Docx);
        }
    }
}

以下是效果图:

无序列表

插入无序列表与插入有序列表的过程大致相似,不过在创建列表样式时,需要设置为Bulleted。然后同样的,调用Paragraph.ListFormat.ApplyStyle()方法将该样式应用到指定的段落文本中。

using Spire.Doc;
using Spire.Doc.Documents;

namespace CreateUnorderedList
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建Document对象
            Document document = new Document();

            //添加一个section
            Section section = document.AddSection();

            //创建一个Bulleted列表样式
            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("2022中国城市GDP排名");
            paragraph.Format.AfterSpacing = 5f;

            //添加其他段落、应用列表样式
            paragraph = section.AddParagraph();
            paragraph.AppendText("上海");
            paragraph.ListFormat.ApplyStyle("bulletedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            paragraph = section.AddParagraph();
            paragraph.AppendText("北京");
            paragraph.ListFormat.ApplyStyle("bulletedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            paragraph = section.AddParagraph();
            paragraph.AppendText("深圳");
            paragraph.ListFormat.ApplyStyle("bulletedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            paragraph = section.AddParagraph();
            paragraph.AppendText("重庆");
            paragraph.ListFormat.ApplyStyle("bulletedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            paragraph = section.AddParagraph();
            paragraph.AppendText("广州");
            paragraph.ListFormat.ApplyStyle("bulletedList");
            paragraph.ListFormat.ListLevelNumber = 0;

            //保存结果文档
            document.SaveToFile("无序列表.docx", FileFormat.Docx);
        }
    }
}

以下是效果图:

参考文章:

C#/VB.NET: Insert Lists in a Word Document

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值