前言:
编辑 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
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。