在Word文档中,对于有多条并列的信息内容或者段落时,我们常以添加项目标号的形式来使文档条理化,在于都是,文档更具美观性。另外,对于在逻辑上存在一定层级结构的内容时,也可以通过多级编号列表来标明文档内容的层次,并且,在修改、编辑文档时也增加了灵活性。因此,在本篇文档中,将介绍如何在C#中通过使用类库Free Spire.Doc for .NET 来创建项目编号列表和多级编号列表的方法。
使用工具: Free Spire.Doc for .NET(社区版)
使用方法:在安装该类库后,在项目中引用Spire.Doc.dll即可(dll文件可在安装路径下的Bin文件夹中获取)
一、创建项目标号
【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"); } } }
实现效果:
二、创建多级编号列表
【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"); } } }
实现效果:
感谢阅读!
相关推荐
C#创建word并保存 可以保存datatable的数据到word
一个完整的C#word代码例子,代码简洁易懂,很容易上手,是初学者很好的入门教程。
Word VBA+C# 开发 多级列表样式设定代码
C# 、WORD文档 成功创建。 整理了网上的多种创建方式。解决创建过程中的错误。 在VS2010 操作WORD2010 测试成功。
使用了免费版的Spire.Doc组件 来使用C#操作Word文档 该组件的好处在于开发环境和部署环境均无需安装MS Word
C#操作word文档 C#实现Word中表格信息读取
win10下 VS2019 C#创建web项目并发布部署在iis
2、如何使用C#在Word文档中插入图片以及读取Word文档中的图片?3、提取word 文档中的图片信息;4、提取WORD中的所有InlineShape图片并保存成文件;5、C#编程实现动态生成Word文档{1、2};6、C#读取Word文档的详细...
创建Word、Word排版代码,表格,段落,项目编号,字体,格式,word的一般操作
C#读取Word内容 源码 C#读取Word内容 源码 C#读取Word内容 源码 C#读取Word内容 源码 C#读取Word内容 源码
C#得到word里面的书签,并向书签插入内容,将DateGridView内容作为表格插入到word书签中。
C#实现通过模板自动创建Word文档的方法,详细讲述了C#生成Word文档的实现方法,是非常实用的技巧,需要的朋友可以参考下 实例讲述了C#实现通过模板自动创建Word文档的方法,是非常实用的技巧。分享给大家供大家参考。 ...
本程序可读取word文档内容、创建新word文档
c#调用word中的vba宏!这个可以很方便的来实现罢工自动化!
通过c#创建基于模板的Word文档通过c#创建基于模板的Word文档
C#实现word文档打印及预览,下载后可以直接复制到程序中使用
C#程序通过模板自动创建Word文档 前段时间有项目要用c#生成Word格式的计算报告,通过网络查找到很多内容,但是都很凌乱,于是自己决定将具体的步骤总结整理出来,以便于更好的交流和以后相似问题可以迅速的解决! ...
使用 C# 创建一个 FTP 服务器,供大家一起共同分享学习。
C# 可以读写word,可以插入表格,用标签写入word文件 1. 通过模板创建新文档 2. 保存新文件 3、 在书签处插入值 4插入表格,bookmark书签 5、修改word样式
C# 读取word文档和图片,有需求的下载