Word控件Spire.Doc 【段落处理】教程(八):如何在 C#、VB.NET 中的 Word 中创建多级列表编

详细步骤:

第 1 步 : 创建一个新的 Word 文档,添加一个部分。

Document document = new Document();Section section = document.AddSection();

第 2 步定义新的多级列表样式。设置其名称为levelstyle,设置ListType 为Numbered,设置level 属性的参数。此列表级别独立于样式运行并控制数字格式。

ListStyle listStyle = new ListStyle(document, ListType.Numbered);listStyle.Name = "levelstyle";listStyle.Levels[0].PatternType = ListPatternType.Arabic;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);

第 3 步创建一个段落,将文本附加到段落并使用 Heading1 应用段落样式。将定义的列表样式“levelstyle”应用于此段落。

Paragraph paragraph = section.AddParagraph();paragraph.AppendText("The first item");paragraph.ApplyStyle(BuiltinStyle.Heading1);paragraph.ListFormat.ApplyStyle("levelstyle");

第 4 步重复第 3 步以向该文档添加更多段落。在这里,您需要将不同的 ListLevelNumber 设置为 1 或 2,以确保将子段落和子段落分配给不同的列表模板。

paragraph = section.AddParagraph();paragraph.AppendText("The second item");paragraph.ApplyStyle(BuiltinStyle.Heading1);paragraph.ListFormat.ApplyStyle("levelstyle");paragraph = section.AddParagraph();paragraph.AppendText("The first sub-item");paragraph.ApplyStyle(BuiltinStyle.Heading2);paragraph.ListFormat.ListLevelNumber = 1;paragraph.ListFormat.ApplyStyle("levelstyle");paragraph = section.AddParagraph();paragraph.AppendText("The second sub-item");paragraph.ApplyStyle(BuiltinStyle.Heading2);paragraph.ListFormat.ContinueListNumbering();paragraph.ListFormat.ApplyStyle("levelstyle");paragraph = section.AddParagraph();paragraph.AppendText("A sub-sub-item");paragraph.ApplyStyle(BuiltinStyle.Heading5);paragraph.ListFormat.ListLevelNumber = 2;paragraph.ListFormat.ApplyStyle("levelstyle");paragraph = section.AddParagraph();paragraph.AppendText("The third item");paragraph.ApplyStyle(BuiltinStyle.Heading1);paragraph.ListFormat.ApplyStyle("levelstyle");

第 5 步保存文件。

document.SaveToFile(@”result.docx”, FileFormat.Docx);

结果

How to Create Multi-level List Numbering in Word in C#, VB.NET

完整代码

[C#]

using Spire.Doc;using Spire.Doc.Documents;namespace CreatList{class Program{static void Main(string[] args){Document document = new Document();Section section = document.AddSection();ListStyle listStyle = new ListStyle(document, ListType.Numbered);listStyle.Name = "levelstyle";listStyle.Levels[0].PatternType = ListPatternType.Arabic;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);Paragraph paragraph = section.AddParagraph();paragraph.AppendText("The first item");paragraph.ApplyStyle(BuiltinStyle.Heading1);paragraph.ListFormat.ApplyStyle("levelstyle");paragraph = section.AddParagraph();paragraph.AppendText("The second item");paragraph.ApplyStyle(BuiltinStyle.Heading1);paragraph.ListFormat.ApplyStyle("levelstyle");paragraph = section.AddParagraph();paragraph.AppendText("The first sub-item");paragraph.ApplyStyle(BuiltinStyle.Heading2);paragraph.ListFormat.ListLevelNumber = 1;paragraph.ListFormat.ApplyStyle("levelstyle");paragraph = section.AddParagraph();paragraph.AppendText("The second sub-item");paragraph.ApplyStyle(BuiltinStyle.Heading2);paragraph.ListFormat.ContinueListNumbering();paragraph.ListFormat.ApplyStyle("levelstyle");paragraph = section.AddParagraph();paragraph.AppendText("A sub-sub-item");paragraph.ApplyStyle(BuiltinStyle.Heading5);paragraph.ListFormat.ListLevelNumber = 2;paragraph.ListFormat.ApplyStyle("levelstyle");paragraph = section.AddParagraph();paragraph.AppendText("The third item");paragraph.ApplyStyle(BuiltinStyle.Heading1);paragraph.ListFormat.ApplyStyle("levelstyle");document.SaveToFile(@"result.docx", FileFormat.Docx);}}}

[VB.NET]

Imports Spire.DocImports Spire.Doc.DocumentsNamespace CreatListClass ProgramPrivate Shared Sub Main(args As String())Dim document As New Document()Dim section As Section = document.AddSection()Dim listStyle As New ListStyle(document, ListType.Numbered)listStyle.Name = "levelstyle"listStyle.Levels(0).PatternType = ListPatternType.ArabiclistStyle.Levels(1).NumberPrefix = vbNullChar & "."listStyle.Levels(1).PatternType = ListPatternType.ArabiclistStyle.Levels(2).NumberPrefix = vbNullChar & "." & ChrW(1) & "."listStyle.Levels(2).PatternType = ListPatternType.Arabicdocument.ListStyles.Add(listStyle)Dim paragraph As Paragraph = section.AddParagraph()paragraph.AppendText("The first item")paragraph.ApplyStyle(BuiltinStyle.Heading1)paragraph.ListFormat.ApplyStyle("levelstyle")paragraph = section.AddParagraph()paragraph.AppendText("The second item")paragraph.ApplyStyle(BuiltinStyle.Heading1)paragraph.ListFormat.ApplyStyle("levelstyle")paragraph = section.AddParagraph()paragraph.AppendText("The first sub-item")paragraph.ApplyStyle(BuiltinStyle.Heading2)paragraph.ListFormat.ListLevelNumber = 1paragraph.ListFormat.ApplyStyle("levelstyle")paragraph = section.AddParagraph()paragraph.AppendText("The second sub-item")paragraph.ApplyStyle(BuiltinStyle.Heading2)paragraph.ListFormat.ContinueListNumbering()paragraph.ListFormat.ApplyStyle("levelstyle")paragraph = section.AddParagraph()paragraph.AppendText("A sub-sub-item")paragraph.ApplyStyle(BuiltinStyle.Heading5)paragraph.ListFormat.ListLevelNumber = 2paragraph.ListFormat.ApplyStyle("levelstyle")paragraph = section.AddParagraph()paragraph.AppendText("The third item")paragraph.ApplyStyle(BuiltinStyle.Heading1)paragraph.ListFormat.ApplyStyle("levelstyle")document.SaveToFile("result.docx", FileFormat.Docx)End SubEnd ClassEnd Namespace

欢迎下载|体验更多E-iceblue产品

如需获取更多产品相关信息请咨询在线客服  


标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年4月10日
下一篇 2022年4月10日

相关推荐

发表回复

登录后才能评论