(本文为转载文章,可在此处查看原文:http://www.cnblogs.com/Yesi/p/8658711.html)
在Word文档中,对于有多条并列的信息内容或者段落时,我们常以添加项目标号的形式来使文档条理化,在阅读时,文档也更具美观性。另外,对于在逻辑上存在一定层级结构的内容时,也可以通过多级编号列表来标明文档内容的层次,并且,在修改、编辑文档时也增加了灵活性。因此,在本篇文档中,将介绍如何在C#中通过使用类库Free Spire.Doc for .NET 来创建项目编号列表和多级编号列表的方法。
使用工具: Free Spire.Doc for .NET(社区版)
使用方法:在安装该类库后,在项目中引用Spire.Doc.dll即可(dll文件可在安装路径下的Bin文件夹中获取)
一、创建Word符号列表
C#
using Spire.Doc; using Spire.Doc.Documents; namespace WordBullets { class Program { static void Main(string[] args) { //初始化Document类实例,并添加section Document doc = new Document(); Section section = doc.AddSection(); //添加七个段落并分别添加文字 Paragraph para1 = section.AddParagraph(); para1.AppendText("国际政治类组织"); Paragraph para2 = section.AddParagraph(); para2.AppendText("欧洲联盟(欧盟)"); Paragraph para3 = section.AddParagraph(); para3.AppendText("独立国家联合体(独联体)"); Paragraph para4 = section.AddParagraph(); para4.AppendText("上海合作组织"); Paragraph para5 = section.AddParagraph(); para5.AppendText("阿拉伯会议联盟"); Paragraph para6 = section.AddParagraph(); para6.AppendText("国际生态安全合作组织"); Paragraph para7 = section.AddParagraph(); para7.AppendText("阿拉伯国家联盟"); //创建段落格式(字体) ParagraphStyle style = new ParagraphStyle(doc); style.Name = "fontStyle"; style.CharacterFormat.FontName = "宋体"; style.CharacterFormat.FontSize = 12f; doc.Styles.Add(style); //遍历所有段落 for (int i = 0; i < section.Paragraphs.Count; i++) { //从第二段开始应用项目符号排列 if (i != 0) { section.Paragraphs[i].ApplyStyle(BuiltinStyle.ListBullet2); } //应用字体格式到每一段 section.Paragraphs[i].ApplyStyle("fontStyle"); } //保存并打开文档 doc.SaveToFile("项目列表.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("项目列表.docx"); } } }
创建结果:
VB.NET
Imports Spire.Doc Imports Spire.Doc.Documents Namespace WordBullets Class Program Private Shared Sub Main(ByVal args As String()) Dim doc As Document = New Document() Dim section As Section = doc.AddSection() Dim para1 As Paragraph = section.AddParagraph() para1.AppendText("国际政治类组织") Dim para2 As Paragraph = section.AddParagraph() para2.AppendText("欧洲联盟(欧盟)") Dim para3 As Paragraph = section.AddParagraph() para3.AppendText("独立国家联合体(独联体)") Dim para4 As Paragraph = section.AddParagraph() para4.AppendText("上海合作组织") Dim para5 As Paragraph = section.AddParagraph() para5.AppendText("阿拉伯会议联盟") Dim para6 As Paragraph = section.AddParagraph() para6.AppendText("国际生态安全合作组织") Dim para7 As Paragraph = section.AddParagraph() para7.AppendText("阿拉伯国家联盟") Dim style As ParagraphStyle = New ParagraphStyle(doc) style.Name = "fontStyle" style.CharacterFormat.FontName = "宋体" style.CharacterFormat.FontSize = 12F doc.Styles.Add(style) For i As Integer = 0 To section.Paragraphs.Count - 1 If i <> 0 Then section.Paragraphs(i).ApplyStyle(BuiltinStyle.ListBullet2) End If section.Paragraphs(i).ApplyStyle("fontStyle") Next doc.SaveToFile("项目列表.docx", FileFormat.Docx2013) System.Diagnostics.Process.Start("项目列表.docx") End Sub End Class End Namespace
二、创建Word多级项目编号列表
C#
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace Multi_levelList_Doc { class Program { static void Main(string[] args) { //新建Word文档 Document doc = new Document(); Section section = doc.AddSection(); //初始化ListStyle对象,指定List类型为数字列表并命名 ListStyle listStyle = new ListStyle(doc, 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; //在ListStyles集合中添加新建的list style doc.ListStyles.Add(listStyle); //创建字体格式 Spire.Doc.Formatting.CharacterFormat format = new Spire.Doc.Formatting.CharacterFormat(doc); format.FontName = "宋体"; //添加段落,设置一级序列 Paragraph paragraph = section.AddParagraph(); TextRange tr = paragraph.AppendText("主要组织机构"); tr.ApplyCharacterFormat(format); //应用字体格式 paragraph.ApplyStyle(BuiltinStyle.Heading1); //应用标题1样式 paragraph.ListFormat.ApplyStyle("levelstyle"); //应用列表样式 //添加段落,设置一级序列 paragraph = section.AddParagraph(); tr = paragraph.AppendText("主要职能"); tr.ApplyCharacterFormat(format); paragraph.ApplyStyle(BuiltinStyle.Heading1); paragraph.ListFormat.ApplyStyle("levelstyle"); //添加段落,设置二级序列 paragraph = section.AddParagraph(); tr = paragraph.AppendText("基本职能"); tr.ApplyCharacterFormat(format); paragraph.ApplyStyle(BuiltinStyle.Heading2); paragraph.ListFormat.ListLevelNumber = 1; //设置等级为第二等级 paragraph.ListFormat.ApplyStyle("levelstyle"); //添加段落,设置二级序列 paragraph = section.AddParagraph(); tr = paragraph.AppendText("5大职能"); tr.ApplyCharacterFormat(format); paragraph.ApplyStyle(BuiltinStyle.Heading2); paragraph.ListFormat.ContinueListNumbering(); paragraph.ListFormat.ApplyStyle("levelstyle"); //添加段落,设置三级序列 paragraph = section.AddParagraph(); tr = paragraph.AppendText("管理职能 \n 组织职能 \n 协调职能 \n 调节职能 \n 提供职能"); tr.ApplyCharacterFormat(format); paragraph.ApplyStyle(BuiltinStyle.Heading5); paragraph.ListFormat.ListLevelNumber = 2; //设置等级为第三等级 paragraph.ListFormat.ApplyStyle("levelstyle"); //添加段落,设置一级序列 paragraph = section.AddParagraph(); tr = paragraph.AppendText("基本原则"); tr.ApplyCharacterFormat(format); paragraph.ApplyStyle(BuiltinStyle.Heading1); paragraph.ListFormat.ApplyStyle("levelstyle"); //保存并打开文档 doc.SaveToFile("多级列表.docx", FileFormat.Docx); System.Diagnostics.Process.Start("多级列表.docx"); } } }
创建结果:
VB.NET
Imports Spire.Doc Imports Spire.Doc.Documents Imports Spire.Doc.Fields Namespace Multi_levelList_Doc Class Program Private Shared Sub Main(ByVal args As String()) Dim doc As Document = New Document() Dim section As Section = doc.AddSection() Dim listStyle As ListStyle = New ListStyle(doc, ListType.Numbered) listStyle.Name = "levelstyle" listStyle.Levels(0).PatternType = ListPatternType.Arabic listStyle.Levels(1).NumberPrefix = vbNullChar & "." listStyle.Levels(1).PatternType = ListPatternType.Arabic listStyle.Levels(2).NumberPrefix = vbNullChar & "." & ChrW(1) & "." listStyle.Levels(2).PatternType = ListPatternType.Arabic doc.ListStyles.Add(listStyle) Dim format As Spire.Doc.Formatting.CharacterFormat = New Spire.Doc.Formatting.CharacterFormat(doc) format.FontName = "宋体" Dim paragraph As Paragraph = section.AddParagraph() Dim tr As TextRange = paragraph.AppendText("主要组织机构") tr.ApplyCharacterFormat(format) paragraph.ApplyStyle(BuiltinStyle.Heading1) paragraph.ListFormat.ApplyStyle("levelstyle") paragraph = section.AddParagraph() tr = paragraph.AppendText("主要职能") tr.ApplyCharacterFormat(format) paragraph.ApplyStyle(BuiltinStyle.Heading1) paragraph.ListFormat.ApplyStyle("levelstyle") paragraph = section.AddParagraph() tr = paragraph.AppendText("基本职能") tr.ApplyCharacterFormat(format) paragraph.ApplyStyle(BuiltinStyle.Heading2) paragraph.ListFormat.ListLevelNumber = 1 paragraph.ListFormat.ApplyStyle("levelstyle") paragraph = section.AddParagraph() tr = paragraph.AppendText("5大职能") tr.ApplyCharacterFormat(format) paragraph.ApplyStyle(BuiltinStyle.Heading2) paragraph.ListFormat.ContinueListNumbering() paragraph.ListFormat.ApplyStyle("levelstyle") paragraph = section.AddParagraph() tr = paragraph.AppendText("管理职能 " & vbLf & " 组织职能 " & vbLf & " 协调职能 " & vbLf & " 调节职能 " & vbLf & " 提供职能") tr.ApplyCharacterFormat(format) paragraph.ApplyStyle(BuiltinStyle.Heading5) paragraph.ListFormat.ListLevelNumber = 2 paragraph.ListFormat.ApplyStyle("levelstyle") paragraph = section.AddParagraph() tr = paragraph.AppendText("基本原则") tr.ApplyCharacterFormat(format) paragraph.ApplyStyle(BuiltinStyle.Heading1) paragraph.ListFormat.ApplyStyle("levelstyle") doc.SaveToFile("多级列表.docx", FileFormat.Docx) System.Diagnostics.Process.Start("多级列表.docx") End Sub End Class End Namespace
相关推荐
C#/VB .NET Coding Guidelines
c/c++/c#/vb/vb.net全局钩子模块
最新 雷电(Leidian) C#/VB.Net 操作模块,无依赖,可以直接使用,测试可用,可以开发自己的和 雷电(Leidian) 模拟器相关的晓游雷电操作模块
本程序支持对sln(解决方案文件)、csproj(c#项目文件)、vbproj(vb.net项目文件)下所包含的源代码进行代码行数统计工作. 支持vs2003\2005\2008等版本所生成的解决方案文件或者项目文件 程序针对的语言为.net ...
C#代码项目转换VB.NET项目代码(可整个项目转换)C#代码项目转换VB.NET项目代码(可整个项目转换)
我收藏的AE+C#/VB/C++/VB.net/RES 开发的地图查询应用的源代码,很实用哦~~
一个完全免费的WEB打印插件控件,支持:C#/vb.net/asp/PHP/JSP,也有实例
最新 Genymotion C#/VB.Net 操作类,测试可用,可以开发自己的和模拟器Genymotion相关的程序
一款小巧的NET代码生成工具,可生成各种数据库连接代码,支持VB、VB.NET、C#、C#.NET...
基恩士激光位移传感器C#/C++/VB.net的demo 很难找 找了很久才找到
2、VB.net/C#工程需要.net framwork 4.8(可以根据你自己的环境修改工程属性) 3、默认选中了x64配置,所有的DLL/组件/调用客户端都生成Windows x64代码,不要混用32位和64位配置。并总是在配置管理器中确认全部编译...
SkinSharp 1.0.6.6 破解版 For C#/VB/VC .NET
ASP.Net MVC下开发示例 1.VS2012的解决方案,用到SuperWebSocket 2.是一个考场向页面发送数据的示例 3.先运行考场连接服务器,再运行页面进行连接
[Vb.Net/C#/J#] 需求:My_FastReport.dll/adodb.dll/Interop.My_FastReport.dll [C++] 需求:My_FastReport.dll/adodb.dll/Interop.My_FastReport.1.0.dll .net 本案例基于.net2005开发平台 QQ:122391458 E-mail:Hz...
一个支持VS2005和2008的重构工具,很好用的Refactor tool,但是只支持C#和VB.NET,安装完毕以后在菜单栏会出现一个“重构”选项,点击鼠标右键也可以看见!语言和你的VS一致! 附上源网址,感兴趣的可以自己去看...
最新 雷电(Leidian) C#/VB.Net 操作类,测试可用,可以开发自己的和 雷电(Leidian) 模拟器相关的程序
主要介绍了C#/VB.NET 如何在Word中添加条码、二维码,代码中将分为在Word正文段落中、页眉页脚中等情况来添加。感兴趣的朋友可以了解下
[Vb.Net/C#/J#] 需求:My_FastReport.dll/adodb.dll/Interop.My_FastReport.dll [C++] 需求:My_FastReport.dll/adodb.dll/Interop.My_FastReport.1.0.dll .net 本案例基于.net2005开发平台 QQ:122391458 E-mail:Hz...
C#转换成vb.net软件解压后双击ConvertCSharp2VB.exe即可使用 将c#的代码粘贴到窗口中 点击右下角Convert即可看到VB.NET的代码 正确率90%左右 !
把程序背景图同步为对应位置的桌面壁纸。 支持WIN7的各种窗口边框尺寸,必须开启Aero特效,其他系统自测或者自己改。 不支持各种动态壁纸。