在 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
在 Word 中插入编号列表
Spire.Doc for .NET 提供了 ListStyle 类,您可以使用该类创建编号列表样式或项目符号样式。然后,可以使用 Paragraph.ListFormat.ApplyStyle() 方法将列表样式应用于段落。创建编号列表的步骤如下。
- 创建一个 Document 对象。
- 使用 Document.AddSection() 方法添加一个节。
- 创建 ListStyle 类的实例,指定列表类型。
- 通过 ListStyle.ListRef.Levels [index] 属性获取列表的特定级别,并通过 ListLevel.PatternType 属性设置编号类型。
- 使用 Section.AddParagraph() 方法向文档添加几个段落。
- 使用 Paragraph.ListFormat.ApplyStyle() 方法将列表样式应用于特定段落。
- 通过 Paragraph.ListFormat.ListLevelNumber 属性指定列表级别。
- 使用 Document.SaveToFile() 方法将文档保存为 Word 文件。
- C#
- VB.NET
//创建一个Document对象
Document document = new Document();
//添加一个节
Section section = document.AddSection();
//创建编号列表样式
ListStyle listStyle = document.Styles.Add(ListType.Numbered, "numberList");
ListLevelCollection Levels = listStyle.ListRef.Levels;
Levels[0].PatternType = ListPatternType.Arabic;
Levels[0].TextPosition = 20;
//添加一个段落
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("必需的Web开发技能:");
paragraph.Format.AfterSpacing = 5f;
//添加段落并对其应用编号列表样式
paragraph = section.AddParagraph();
paragraph.AppendText("HTML");
paragraph.ListFormat.ApplyStyle(listStyle);
paragraph.ListFormat.ListLevelNumber = 0;
//再添加四个段落,并将编号列表样式应用于特定段落
paragraph = section.AddParagraph();
paragraph.AppendText("CSS");
paragraph.ListFormat.ApplyStyle(listStyle);
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("JavaScript");
paragraph.ListFormat.ApplyStyle(listStyle);
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("Python");
paragraph.ListFormat.ApplyStyle(listStyle);
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("MySQL");
paragraph.ListFormat.ApplyStyle(listStyle);
paragraph.ListFormat.ListLevelNumber = 0;
//将文档保存为Word文件
document.SaveToFile("插入编号列表.docx", FileFormat.Docx);
'创建一个Document对象 Dim document As New Document() '添加一个节
Dim section As Section = document.AddSection()
'创建编号列表样式 Dim listStyle As ListStyle = document.Styles.Add(ListType.Numbered,"numberList") Dim Levels As ListLevelCollection = listStyle.ListRef.Levels Levels(0).PatternType = ListPatternType.Arabic Levels(0).TextPosition = 20 '添加一个段落
Dim paragraph As Paragraph = section.AddParagraph()
paragraph.AppendText("必需的Web开发技能:")
paragraph.Format.AfterSpacing = 5f
'添加段落并对其应用编号列表样式 paragraph = section.AddParagraph() paragraph.AppendText("HTML") paragraph.ListFormat.ApplyStyle(listStyle) paragraph.ListFormat.ListLevelNumber = 0 '再添加四个段落,并将编号列表样式应用于特定段落
paragraph = section.AddParagraph()
paragraph.AppendText("CSS")
paragraph.ListFormat.ApplyStyle(listStyle)
paragraph.ListFormat.ListLevelNumber = 0
paragraph = section.AddParagraph()
paragraph.AppendText("JavaScript")
paragraph.ListFormat.ApplyStyle(listStyle)
paragraph.ListFormat.ListLevelNumber = 0
paragraph = section.AddParagraph()
paragraph.AppendText("Python")
paragraph.ListFormat.ApplyStyle(listStyle)
paragraph.ListFormat.ListLevelNumber = 0
paragraph = section.AddParagraph()
paragraph.AppendText("MySQL")
paragraph.ListFormat.ApplyStyle(listStyle)
paragraph.ListFormat.ListLevelNumber = 0
'将文档保存为Word文件 document.SaveToFile("插入编号列表.docx",FileFormat.Docx)
在 Word 中插入项目符号列表
创建项目符号列表的过程与创建编号列表的过程类似。不同之处在于,创建列表样式时,必须将列表类型指定为“项目符号”,并为其设置项目符号。以下是详细步骤。
- 创建一个 Document对象。
- 使用 Document.AddSection()方法添加一个节。
- 创建 ListStyle类的实例,指定列表类型。
- 通过 ListStyle. ListRef.Levels [index]属性获取列表的特定级别,并通过 ListLevel.BulletCharacter属性设置项目符号。
- 使用 Section.AddParagraph()方法向文档添加几个段落。
- 使用 Paragraph.ListFormat.ApplyStyle()方法将列表样式应用于特定段落。
- 通过 Paragraph.ListFormat.ListLevelNumber属性指定列表级别。
- 使用 Document.SaveToFile()方法将文档保存为 Word文件。
- C#
- VB.NET
//创建一个Document对象 Document document = new Document();//添加一个节 Section section = document.AddSection();//创建项目符号列表样式 ListStyle listStyle = document.Styles.Add(ListType.Bulleted,"bulletedList");ListLevelCollection Levels = listStyle.ListRef.Levels;Levels[0].BulletCharacter = "\x00B7";Levels[0].CharacterFormat.FontName = "Symbol";Levels[0].TextPosition = 20;//添加一个段落 Paragraph paragraph = section.AddParagraph();paragraph.AppendText("计算机科学科目:");paragraph.Format.AfterSpacing = 5f;//添加段落并对其应用项目符号列表样式 paragraph = section.AddParagraph();paragraph.AppendText("数据结构");paragraph.ListFormat.ApplyStyle(listStyle);paragraph.ListFormat.ListLevelNumber = 0;//再添加五个段落,并将项目符号列表样式应用于特定段落 paragraph = section.AddParagraph();paragraph.AppendText("演算法");paragraph.ListFormat.ApplyStyle(listStyle);paragraph.ListFormat.ListLevelNumber = 0;paragraph = section.AddParagraph();paragraph.AppendText("计算机网络");paragraph.ListFormat.ApplyStyle(listStyle);paragraph.ListFormat.ListLevelNumber = 0;paragraph = section.AddParagraph();paragraph.AppendText("操作系统");paragraph.ListFormat.ApplyStyle(listStyle);paragraph.ListFormat.ListLevelNumber = 0;paragraph = section.AddParagraph();paragraph.AppendText("C语言程序设计");paragraph.ListFormat.ApplyStyle(listStyle);paragraph.ListFormat.ListLevelNumber = 0;paragraph = section.AddParagraph();paragraph.AppendText("计算理论");paragraph.ListFormat.ApplyStyle(listStyle);paragraph.ListFormat.ListLevelNumber = 0;//保存结果文档 document.SaveToFile("项目符号列表.docx",FileFormat.Docx);
'创建一个Document对象
Dim document As New Document()
'添加一个节 Dim section As Section = document.AddSection() '创建项目符号列表样式
Dim listStyle As ListStyle = document.Styles.Add(ListType.Bulleted, "bulletedList")
Dim Levels As ListLevelCollection = listStyle.ListRef.Levels
Levels(0).BulletCharacter = ChrW(&H00B7).ToString()
Levels(0).CharacterFormat.FontName = "Symbol"
Levels(0).TextPosition = 20
'添加一个段落 Dim paragraph As Paragraph = section.AddParagraph() paragraph.AppendText("计算机科学科目:") paragraph.Format.AfterSpacing = 5f '添加段落并对其应用项目符号列表样式
paragraph = section.AddParagraph()
paragraph.AppendText("数据结构")
paragraph.ListFormat.ApplyStyle(listStyle)
paragraph.ListFormat.ListLevelNumber = 0
'再添加五个段落,并将项目符号列表样式应用于特定段落 paragraph = section.AddParagraph() paragraph.AppendText("演算法") paragraph.ListFormat.ApplyStyle(listStyle) paragraph.ListFormat.ListLevelNumber = 0 paragraph = section.AddParagraph() paragraph.AppendText("计算机网络") paragraph.ListFormat.ApplyStyle(listStyle) paragraph.ListFormat.ListLevelNumber = 0 paragraph = section.AddParagraph() paragraph.AppendText("操作系统") paragraph.ListFormat.ApplyStyle(listStyle) paragraph.ListFormat.ListLevelNumber = 0 paragraph = section.AddParagraph() paragraph.AppendText("C语言程序设计") paragraph.ListFormat.ApplyStyle(listStyle) paragraph.ListFormat.ListLevelNumber = 0 paragraph = section.AddParagraph() paragraph.AppendText("计算理论") paragraph.ListFormat.ApplyStyle(listStyle) paragraph.ListFormat.ListLevelNumber = 0 '保存结果文档
document.SaveToFile("项目符号列表.docx", FileFormat.Docx)
在 Word 中插入多级编号列表
多级列表至少由两个不同的级别组成。嵌套列表的每个级别都由 ListStyle.Levels[index] 属性表示,通过该属性可以设置某个级别的编号类型和前缀。以下是在 Word 中创建多级编号列表的步骤。
- 创建一个 Document 对象。
- 使用 Document.AddSection() 方法添加一个节。
- 创建 ListStyle 类的实例,将列表类型指定为 Numbered。
- 通过 ListStyle.ListRef.Levels [index] 属性获取列表的特定级别,并设置编号类型和前缀。
- 使用 Section.AddParagraph() 方法将多个段落添加到文档中。
- 使用 Paragraph.ListFormat.ApplyStyle() 方法将列表样式应用于特定段落。
- 通过 Paragraph.ListFormat.ListLevelNumber 属性指定列表级别。
- 使用 Document.SaveToFile() 方法将文档保存到 Word 文件中。
- C#
- VB.NET
//创建一个Document对象
Document document = new Document();
//添加一个节
Section section = document.AddSection();
//创建编号列表样式,指定每个级别的编号前缀和图案类型
ListStyle listStyle = document.Styles.Add(ListType.Numbered, "levelstyle");
ListLevelCollection Levels = listStyle.ListRef.Levels;
Levels[0].PatternType = ListPatternType.Arabic;
Levels[0].TextPosition = 20;
Levels[1].NumberPrefix = "\x0000.";
Levels[1].PatternType = ListPatternType.Arabic;
Levels[2].NumberPrefix = "\x0000.\x0001.";
Levels[2].PatternType = ListPatternType.Arabic;
//添加一个段落
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("这是一个多级编号列表:");
paragraph.Format.AfterSpacing = 5f;
//添加段落并对其应用编号列表样式
paragraph = section.AddParagraph();
paragraph.AppendText("第一项");
paragraph.ListFormat.ApplyStyle(listStyle);
paragraph.ListFormat.ListLevelNumber = 0;
//再添加五个段落,并将编号列表样式应用于特定段落
paragraph = section.AddParagraph();
paragraph.AppendText("第二项");
paragraph.ListFormat.ApplyStyle(listStyle);
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("第一个子项");
paragraph.ListFormat.ApplyStyle(listStyle);
paragraph.ListFormat.ListLevelNumber = 1;
paragraph = section.AddParagraph();
paragraph.AppendText("第二个子项");
paragraph.ListFormat.ContinueListNumbering();
paragraph.ListFormat.ApplyStyle(listStyle);
paragraph = section.AddParagraph();
paragraph.AppendText("一个子项");
paragraph.ListFormat.ApplyStyle(listStyle);
paragraph.ListFormat.ListLevelNumber = 2;
paragraph = section.AddParagraph();
paragraph.AppendText("第三项");
paragraph.ListFormat.ApplyStyle(listStyle);
paragraph.ListFormat.ListLevelNumber = 0;
//保存结果文档
document.SaveToFile("多级编号列表.docx", FileFormat.Docx);
'创建一个Document对象 Dim document As New Document() '添加一个节
Dim section As Section = document.AddSection()
'创建编号列表样式,指定每个级别的编号前缀和图案类型 Dim listStyle As ListStyle = document.Styles.Add(ListType.Numbered,"levelstyle") Dim Levels As ListLevelCollection = listStyle.ListRef.Levels Levels(0).PatternType = ListPatternType.Arabic Levels(0).TextPosition = 20 Levels(1).NumberPrefix = ChrW(&H0000).ToString() & "." Levels(1).PatternType = ListPatternType.Arabic Levels(2).NumberPrefix = ChrW(&H0000).ToString() & "." & ChrW(&H0001).ToString() & "." Levels(2).PatternType = ListPatternType.Arabic '添加一个段落
Dim paragraph As Paragraph = section.AddParagraph()
paragraph.AppendText("这是一个多级编号列表:")
paragraph.Format.AfterSpacing = 5f
'添加段落并对其应用编号列表样式 paragraph = section.AddParagraph() paragraph.AppendText("第一项") paragraph.ListFormat.ApplyStyle(listStyle) paragraph.ListFormat.ListLevelNumber = 0 '再添加五个段落,并将编号列表样式应用于特定段落
paragraph = section.AddParagraph()
paragraph.AppendText("第二项")
paragraph.ListFormat.ApplyStyle(listStyle)
paragraph.ListFormat.ListLevelNumber = 0
paragraph = section.AddParagraph()
paragraph.AppendText("第一个子项")
paragraph.ListFormat.ApplyStyle(listStyle)
paragraph.ListFormat.ListLevelNumber = 1
paragraph = section.AddParagraph()
paragraph.AppendText("第二个子项")
paragraph.ListFormat.ContinueListNumbering()
paragraph.ListFormat.ApplyStyle(listStyle)
paragraph = section.AddParagraph()
paragraph.AppendText("一个子项")
paragraph.ListFormat.ApplyStyle(listStyle)
paragraph.ListFormat.ListLevelNumber = 2
paragraph = section.AddParagraph()
paragraph.AppendText("第三项")
paragraph.ListFormat.ApplyStyle(listStyle)
paragraph.ListFormat.ListLevelNumber = 0
'保存结果文档 document.SaveToFile("多级编号列表.docx",FileFormat.Docx)
在 Word 中插入多级混合类型列表
在某些情况下,您可能希望在多级列表中混合数字和符号项目符号。要创建混合类型列表,只需要创建编号列表样式和项目符号列表样式,并将它们应用于不同的段落。具体步骤如下。
- 创建一个 Document对象。
- 使用 Document.AddSection()方法添加一个节。
- 创建编号列表样式和项目符号列表样式。
- 使用 Section.AddParagraph()方法将多个段落添加到文档中。
- 使用 Paragraph.ListFormat.ApplyStyle()方法将不同的列表样式应用于不同的段落。
- 使用 Document.SaveToFile()方法将文档保存到 Word 文件中。
- C#
- VB.NET
//创建一个Document对象 Document document = new Document();//添加一个节 Section section = document.AddSection();//创建编号列表样式 ListStyle listStyle = document.Styles.Add(ListType.Numbered,"numberedStyle");ListLevelCollection Levels = listStyle.ListRef.Levels;Levels[0].PatternType = ListPatternType.Arabic;Levels[0].TextPosition = 20;Levels[1].PatternType = ListPatternType.LowLetter;//创建项目符号列表样式 ListStyle bulletedListStyle = document.Styles.Add(ListType.Bulleted,"bulltedStyle");ListLevelCollection bulletedLevels = listStyle.ListRef.Levels;bulletedLevels[2].BulletCharacter = "\x002A";bulletedLevels[2].CharacterFormat.FontName = "Symbol";//添加段落 Paragraph paragraph = section.AddParagraph();paragraph.AppendText("这是一个多级混合列表:");paragraph.Format.AfterSpacing = 5f;//添加段落并对其应用编号列表样式 paragraph = section.AddParagraph();paragraph.AppendText("第一项");paragraph.ListFormat.ApplyStyle(listStyle);paragraph.ListFormat.ListLevelNumber = 0;//再添加五个段落,并对其应用不同的列表样式 paragraph = section.AddParagraph();paragraph.AppendText("第一个子项");paragraph.ListFormat.ApplyStyle(listStyle);paragraph.ListFormat.ListLevelNumber = 1;paragraph = section.AddParagraph();paragraph.AppendText("第二个子项");paragraph.ListFormat.ListLevelNumber = 1;paragraph.ListFormat.ApplyStyle(listStyle);paragraph = section.AddParagraph();paragraph.AppendText("子项1");paragraph.ListFormat.ApplyStyle(bulletedListStyle);paragraph.ListFormat.ListLevelNumber = 2;paragraph = section.AddParagraph();paragraph.AppendText("子项2");paragraph.ListFormat.ApplyStyle(bulletedListStyle);paragraph.ListFormat.ListLevelNumber = 2;paragraph = section.AddParagraph();paragraph.AppendText("第二项");paragraph.ListFormat.ApplyStyle(listStyle);paragraph.ListFormat.ListLevelNumber = 0;//保存结果文档 document.SaveToFile("多级混合类型列表.docx",FileFormat.Docx);
'创建一个Document对象
Dim document As New Document()
'添加一个节 Dim section As Section = document.AddSection() '创建编号列表样式
Dim listStyle As ListStyle = document.Styles.Add(ListType.Numbered, "numberedStyle")
Dim Levels As ListLevelCollection = listStyle.ListRef.Levels
Levels(0).PatternType = ListPatternType.Arabic
Levels(0).TextPosition = 20
Levels(1).PatternType = ListPatternType.LowLetter
'创建项目符号列表样式 Dim bulletedListStyle As ListStyle = document.Styles.Add(ListType.Bulleted,"bulltedStyle") Dim bulletedLevels As ListLevelCollection = listStyle.ListRef.Levels bulletedLevels(2).BulletCharacter = ChrW(&H002A).ToString() bulletedLevels(2).CharacterFormat.FontName = "Symbol" '添加段落
Dim paragraph As Paragraph = section.AddParagraph()
paragraph.AppendText("这是一个多级混合列表:")
paragraph.Format.AfterSpacing = 5f
'添加段落并对其应用编号列表样式 paragraph = section.AddParagraph() paragraph.AppendText("第一项") paragraph.ListFormat.ApplyStyle(listStyle) paragraph.ListFormat.ListLevelNumber = 0 '再添加五个段落,并对其应用不同的列表样式
paragraph = section.AddParagraph()
paragraph.AppendText("第一个子项")
paragraph.ListFormat.ApplyStyle(listStyle)
paragraph.ListFormat.ListLevelNumber = 1
paragraph = section.AddParagraph()
paragraph.AppendText("第二个子项")
paragraph.ListFormat.ListLevelNumber = 1
paragraph.ListFormat.ApplyStyle(listStyle)
paragraph = section.AddParagraph()
paragraph.AppendText("子项1")
paragraph.ListFormat.ApplyStyle(bulletedListStyle)
paragraph.ListFormat.ListLevelNumber = 2
paragraph = section.AddParagraph()
paragraph.AppendText("子项2")
paragraph.ListFormat.ApplyStyle(bulletedListStyle)
paragraph.ListFormat.ListLevelNumber = 2
paragraph = section.AddParagraph()
paragraph.AppendText("第二项")
paragraph.ListFormat.ApplyStyle(listStyle)
paragraph.ListFormat.ListLevelNumber = 0
'保存结果文档 document.SaveToFile("多级混合类型列表.docx",FileFormat.Docx)
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。