翻译|使用教程|编辑:颜馨|2023-05-23 09:38:50.960|阅读 152 次
概述:本章介绍如何在 Word 文档中插入列表,欢迎查阅~
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。
E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式
技术交流Q群(767755948)
Word 文档中使用列表来勾勒、排列和强调文本,使用户能够轻松扫描和理解一系列项目。Word中有三种不同类型的列表,即编号列表,项目符号列表和多级列表。编号列表用于具有序列或优先级的项目,例如一系列说明。项目符号列表用于没有特定优先级的项目,例如函数或事实列表。多级列表用于存在序列且您希望每个段落单独编号的情况。
Spire.Doc for .NET 提供了 ListStyle 类,可用于创建编号列表样式或项目符号样式。然后,可以使用 Paragraph.ListFormat.ApplyStyle() 方法将列表样式应用于段落。创建编号列表的步骤如下。
【C# 】
using Spire.Doc;
using Spire.Doc.Documents;
namespace CreateOrderedList
{
class Program
{
static void Main(string[] args)
{
//Create a Document object
Document document = new Document();
//Add a section
Section section = document.AddSection();
//Create a numbered list style
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);
//Add a paragraph
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Required Web Development Skills:");
paragraph.Format.AfterSpacing = 5f;
//Add a paragraph and apply the numbered list style to it
paragraph = section.AddParagraph();
paragraph.AppendText("HTML");
paragraph.ListFormat.ApplyStyle("numberedList");
paragraph.ListFormat.ListLevelNumber = 0;
//Add another four paragraphs and apply the numbered list style to them
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;
//Save the document to file
document.SaveToFile("NumberedList.docx", FileFormat.Docx);
}
}
}
【VB.NET】
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace CreateOrderedList
Class Program
Shared Sub Main(ByVal args() As String)
'Create a Document object
Dim document As Document = New Document()
'Add a section
Dim section As Section = document.AddSection()
'Create a numbered list style
Dim listStyle As ListStyle = New ListStyle(document,ListType.Numbered)
listStyle.Name = "numberedList"
listStyle.Levels(0).PatternType = ListPatternType.DecimalEnclosedParen
listStyle.Levels(0).TextPosition = 20
document.ListStyles.Add(listStyle)
'Add a paragraph
Dim paragraph As Paragraph = section.AddParagraph()
paragraph.AppendText("Required Web Development Skills:")
paragraph.Format.AfterSpacing = 5f
'Add a paragraph and apply the numbered list style to it
paragraph = section.AddParagraph()
paragraph.AppendText("HTML")
paragraph.ListFormat.ApplyStyle("numberedList")
paragraph.ListFormat.ListLevelNumber = 0
'Add another four paragraphs and apply the numbered list style to them
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
'Save the document to file
document.SaveToFile("NumberedList.docx", FileFormat.Docx)
End Sub
End Class
End Namespace
创建项目符号列表的过程类似于创建编号列表的过程。不同之处在于,在创建列表样式时,必须将列表类型指定为"项目符号",并为其设置项目符号。以下是详细步骤。
【C# 】
using Spire.Doc;
using Spire.Doc.Documents;
namespace CreateUnorderedList
{
class Program
{
static void Main(string[] args)
{
//Create a Document object
Document document = new Document();
//Add a section
Section section = document.AddSection();
//Create a bulleted list style
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);
//Add a paragraph
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Computer Science Subjects:");
paragraph.Format.AfterSpacing = 5f;
//Add a paragraph and apply the bulleted list style to it
paragraph = section.AddParagraph();
paragraph.AppendText("Data Structure");
paragraph.ListFormat.ApplyStyle("bulletedList");
paragraph.ListFormat.ListLevelNumber = 0;
//Add another five paragraphs and apply the bulleted list style to them
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;
//Save the document to file
document.SaveToFile("BulletedList.docx", FileFormat.Docx);
}
}
}
【VB.NET】
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace CreateUnorderedList
Class Program
Shared Sub Main(ByVal args() As String)
'Create a Document object
Dim document As Document = New Document()
'Add a section
Dim section As Section = document.AddSection()
'Create a bulleted list style
Dim listStyle As 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)
'Add a paragraph
Dim paragraph As Paragraph = section.AddParagraph()
paragraph.AppendText("Computer Science Subjects:")
paragraph.Format.AfterSpacing = 5f
'Add a paragraph and apply the bulleted list style to it
paragraph = section.AddParagraph()
paragraph.AppendText("Data Structure")
paragraph.ListFormat.ApplyStyle("bulletedList")
paragraph.ListFormat.ListLevelNumber = 0
'Add another five paragraphs and apply the bulleted list style to them
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
'Save the document to file
document.SaveToFile("BulletedList.docx", FileFormat.Docx)
End Sub
End Class
End Namespace
多级列表至少包含两个不同的级别。嵌套列表的每个级别都由 ListStyle.Levels[index] 属性表示,通过该属性可以设置特定级别的编号类型和前缀。以下是在 Word 中创建多级编号列表的步骤。
【C# 】
using Spire.Doc;
using Spire.Doc.Documents;
namespace CreateMultiLevelList
{
class Program
{
static void Main(string[] args)
{
//Create a Document object
Document document = new Document();
//Add a section
Section section = document.AddSection();
//Create a numbered list style, specifying number prefix and pattern type of each level
ListStyle listStyle = new ListStyle(document, ListType.Numbered);
listStyle.Name = "levelstyle";
listStyle.Levels[0].PatternType = ListPatternType.Arabic;
listStyle.Levels[0].TextPosition = 20;
listStyle.Levels[1].NumberPrefix = "\x0000.";
listStyle.Levels[1].PatternType = ListPatternType.Arabic;
listStyle.Levels[2].NumberPrefix = "\x0000.\x0001.";
listStyle.Levels[2].PatternType = ListPatternType.Arabic;
document.ListStyles.Add(listStyle);
//Add a paragraph
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Here's a Multi-Level Numbered List:");
paragraph.Format.AfterSpacing = 5f;
//Add a paragraph and apply the numbered list style to it
paragraph = section.AddParagraph();
paragraph.AppendText("The first item");
paragraph.ListFormat.ApplyStyle("levelstyle");
paragraph.ListFormat.ListLevelNumber = 0;
//Add another five paragraphs and apply the numbered list stype to them
paragraph = section.AddParagraph();
paragraph.AppendText("The second item");
paragraph.ListFormat.ApplyStyle("levelstyle");
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("The first sub-item");
paragraph.ListFormat.ApplyStyle("levelstyle");
paragraph.ListFormat.ListLevelNumber = 1;
paragraph = section.AddParagraph();
paragraph.AppendText("The second sub-item");
paragraph.ListFormat.ContinueListNumbering();
paragraph.ListFormat.ApplyStyle("levelstyle");
paragraph = section.AddParagraph();
paragraph.AppendText("A sub-sub-item");
paragraph.ListFormat.ApplyStyle("levelstyle");
paragraph.ListFormat.ListLevelNumber = 2;
paragraph = section.AddParagraph();
paragraph.AppendText("The third item");
paragraph.ListFormat.ApplyStyle("levelstyle");
paragraph.ListFormat.ListLevelNumber = 0;
//Save the document to file
document.SaveToFile("MultilevelNumberedList.docx", FileFormat.Docx);
}
}
}
【VB.NET】
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace CreateMultiLevelList
Class Program
Shared Sub Main(ByVal args() As String)
'Create a Document object
Dim document As Document = New Document()
'Add a section
Dim section As Section = document.AddSection()
'Create a numbered list style, specifying number prefix and pattern type of each level
Dim listStyle As ListStyle = New ListStyle(document,ListType.Numbered)
listStyle.Name = "levelstyle"
listStyle.Levels(0).PatternType = ListPatternType.Arabic
listStyle.Levels(0).TextPosition = 20
listStyle.Levels(1).NumberPrefix = "\x0000."
listStyle.Levels(1).PatternType = ListPatternType.Arabic
listStyle.Levels(2).NumberPrefix = "\x0000.\x0001."
listStyle.Levels(2).PatternType = ListPatternType.Arabic
document.ListStyles.Add(listStyle)
'Add a paragraph
Dim paragraph As Paragraph = section.AddParagraph()
paragraph.AppendText("Here's a Multi-Level Numbered List:")
paragraph.Format.AfterSpacing = 5f
'Add a paragraph and apply the numbered list style to it
paragraph = section.AddParagraph()
paragraph.AppendText("The first item")
paragraph.ListFormat.ApplyStyle("levelstyle")
paragraph.ListFormat.ListLevelNumber = 0
'Add another five paragraphs and apply the numbered list stype to them
paragraph = section.AddParagraph()
paragraph.AppendText("The second item")
paragraph.ListFormat.ApplyStyle("levelstyle")
paragraph.ListFormat.ListLevelNumber = 0
paragraph = section.AddParagraph()
paragraph.AppendText("The first sub-item")
paragraph.ListFormat.ApplyStyle("levelstyle")
paragraph.ListFormat.ListLevelNumber = 1
paragraph = section.AddParagraph()
paragraph.AppendText("The second sub-item")
paragraph.ListFormat.ContinueListNumbering()
paragraph.ListFormat.ApplyStyle("levelstyle")
paragraph = section.AddParagraph()
paragraph.AppendText("A sub-sub-item")
paragraph.ListFormat.ApplyStyle("levelstyle")
paragraph.ListFormat.ListLevelNumber = 2
paragraph = section.AddParagraph()
paragraph.AppendText("The third item")
paragraph.ListFormat.ApplyStyle("levelstyle")
paragraph.ListFormat.ListLevelNumber = 0
'Save the document to file
document.SaveToFile("MultilevelNumberedList.docx", FileFormat.Docx)
End Sub
End Class
End Namespace
在某些情况下,您可能希望在多级列表中混合使用数字和符号项目符号点。要创建混合类型列表,您只需创建编号列表样式和项目符号列表样式,并将它们应用于不同的段落。具体步骤如下。
【C# 】
using Spire.Doc;
using Spire.Doc.Documents;
namespace CreateMultilevelMixedList
{
class Program
{
static void Main(string[] args)
{
//Create a Document object
Document document = new Document();
//Add a section
Section section = document.AddSection();
//Create a numbered list style
ListStyle numberedListStyle = new ListStyle(document, ListType.Numbered);
numberedListStyle.Name = "numberedStyle";
numberedListStyle.Levels[0].PatternType = ListPatternType.Arabic;
numberedListStyle.Levels[0].TextPosition = 20;
numberedListStyle.Levels[1].PatternType = ListPatternType.LowLetter;
document.ListStyles.Add(numberedListStyle);
//Create a bulleted list style
ListStyle bulletedListStyle = new ListStyle(document, ListType.Bulleted);
bulletedListStyle.Name = "bulltedStyle";
bulletedListStyle.Levels[2].BulletCharacter = "\x002A";
bulletedListStyle.Levels[2].CharacterFormat.FontName = "Symbol";
document.ListStyles.Add(bulletedListStyle);
//Add a paragraph
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Here's a Multi-Level Mixed List:");
paragraph.Format.AfterSpacing = 5f;
//Add a paragraph and apply the numbered list style to it
paragraph = section.AddParagraph();
paragraph.AppendText("The first item");
paragraph.ListFormat.ApplyStyle("numberedStyle");
paragraph.ListFormat.ListLevelNumber = 0;
//Add another five paragraphs and apply different list stype to them
paragraph = section.AddParagraph();
paragraph.AppendText("The first sub-item");
paragraph.ListFormat.ApplyStyle("numberedStyle");
paragraph.ListFormat.ListLevelNumber = 1;
paragraph = section.AddParagraph();
paragraph.AppendText("The second sub-item");
paragraph.ListFormat.ListLevelNumber = 1;
paragraph.ListFormat.ApplyStyle("numberedStyle");
paragraph = section.AddParagraph();
paragraph.AppendText("The first sub-sub-item");
paragraph.ListFormat.ApplyStyle("bulltedStyle");
paragraph.ListFormat.ListLevelNumber = 2;
paragraph = section.AddParagraph();
paragraph.AppendText("The second sub-sub-item");
paragraph.ListFormat.ApplyStyle("bulltedStyle");
paragraph.ListFormat.ListLevelNumber = 2;
paragraph = section.AddParagraph();
paragraph.AppendText("The second item");
paragraph.ListFormat.ApplyStyle("numberedStyle");
paragraph.ListFormat.ListLevelNumber = 0;
//Save the document to file
document.SaveToFile("MultilevelMixedList.docx", FileFormat.Docx);
}
}
}
【VB.NET】
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace CreateMultilevelMixedList
Class Program
Shared Sub Main(ByVal args() As String)
'Create a Document object
Dim document As Document = New Document()
'Add a section
Dim section As Section = document.AddSection()
'Create a numbered list style
Dim numberedListStyle As ListStyle = New ListStyle(document,ListType.Numbered)
numberedListStyle.Name = "numberedStyle"
numberedListStyle.Levels(0).PatternType = ListPatternType.Arabic
numberedListStyle.Levels(0).TextPosition = 20
numberedListStyle.Levels(1).PatternType = ListPatternType.LowLetter
document.ListStyles.Add(numberedListStyle)
'Create a bulleted list style
Dim bulletedListStyle As ListStyle = New ListStyle(document,ListType.Bulleted)
bulletedListStyle.Name = "bulltedStyle"
bulletedListStyle.Levels(2).BulletCharacter = "\x002A"
bulletedListStyle.Levels(2).CharacterFormat.FontName = "Symbol"
document.ListStyles.Add(bulletedListStyle)
'Add a paragraph
Dim paragraph As Paragraph = section.AddParagraph()
paragraph.AppendText("Here's a Multi-Level Mixed List:")
paragraph.Format.AfterSpacing = 5f
'Add a paragraph and apply the numbered list style to it
paragraph = section.AddParagraph()
paragraph.AppendText("The first item")
paragraph.ListFormat.ApplyStyle("numberedStyle")
paragraph.ListFormat.ListLevelNumber = 0
'Add another five paragraphs and apply different list stype to them
paragraph = section.AddParagraph()
paragraph.AppendText("The first sub-item")
paragraph.ListFormat.ApplyStyle("numberedStyle")
paragraph.ListFormat.ListLevelNumber = 1
paragraph = section.AddParagraph()
paragraph.AppendText("The second sub-item")
paragraph.ListFormat.ListLevelNumber = 1
paragraph.ListFormat.ApplyStyle("numberedStyle")
paragraph = section.AddParagraph()
paragraph.AppendText("The first sub-sub-item")
paragraph.ListFormat.ApplyStyle("bulltedStyle")
paragraph.ListFormat.ListLevelNumber = 2
paragraph = section.AddParagraph()
paragraph.AppendText("The second sub-sub-item")
paragraph.ListFormat.ApplyStyle("bulltedStyle")
paragraph.ListFormat.ListLevelNumber = 2
paragraph = section.AddParagraph()
paragraph.AppendText("The second item")
paragraph.ListFormat.ApplyStyle("numberedStyle")
paragraph.ListFormat.ListLevelNumber = 0
'Save the document to file
document.SaveToFile("MultilevelMixedList.docx", FileFormat.Docx)
End Sub
End Class
End Namespace
以上便是如何在 Word 文档中插入列表的教程,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。
欢迎下载|体验更多E-iceblue产品
获取更多信息请咨询 ;技术交流Q群(767755948)
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn