C# 创建Word项目标号列表、多级编号列表

30阅读 0评论2018-05-02 Mia张
分类:C#/.net

在Word文档中,对于有多条并列的信息内容或者段落时,我们常以添加项目标号的形式来使文档条理化,在阅读时,文档也更具美观性。另外,对于在逻辑上存在一定层级结构的内容时,也可以通过多级编号列表来标明文档内容的层次,并且,在修改、编辑文档时也增加了灵活性。因此,在本篇文档中,将介绍如何在C#中通过使用类库Free Spire.Doc for .NET 来创建项目编号列表和多级编号列表的方法。
使用工具: Free Spire.Doc for .NET(社区版)
使用方法:在安装该类库后,在项目中引用Spire.Doc.dll即可(dll文件可在安装路径下的Bin文件夹中获取)

一、 创建项目标号列表

C#

  1. using Spire.Doc;
  2. using Spire.Doc.Documents;

  3. namespace WordBullets
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             //初始化Document类实例,并添加section
  10.             Document doc = new Document();
  11.             Section section = doc.AddSection();

  12.             //添加七个段落并分别添加文字
  13.             Paragraph para1 = section.AddParagraph();
  14.             para1.AppendText("国际政治类组织");
  15.             Paragraph para2 = section.AddParagraph();
  16.             para2.AppendText("欧洲联盟(欧盟)");
  17.             Paragraph para3 = section.AddParagraph();
  18.             para3.AppendText("独立国家联合体(独联体)");
  19.             Paragraph para4 = section.AddParagraph();
  20.             para4.AppendText("上海合作组织");
  21.             Paragraph para5 = section.AddParagraph();
  22.             para5.AppendText("阿拉伯会议联盟");
  23.             Paragraph para6 = section.AddParagraph();
  24.             para6.AppendText("国际生态安全合作组织");
  25.             Paragraph para7 = section.AddParagraph();
  26.             para7.AppendText("阿拉伯国家联盟");

  27.             //创建段落格式(字体)
  28.             ParagraphStyle style = new ParagraphStyle(doc);
  29.             style.Name = "fontStyle";
  30.             style.CharacterFormat.FontName = "宋体";
  31.             style.CharacterFormat.FontSize = 12f;
  32.             doc.Styles.Add(style);

  33.             //遍历所有段落
  34.             for (int i = 0; i < section.Paragraphs.Count; i++)
  35.             {
  36.                 //从第二段开始应用项目符号排列
  37.                 if (i != 0)
  38.                 {
  39.                     section.Paragraphs[i].ApplyStyle(BuiltinStyle.ListBullet2);
  40.                 }

  41.                 //应用字体格式到每一段
  42.                 section.Paragraphs[i].ApplyStyle("fontStyle");

  43.             }
  44.             //保存并打开文档
  45.             doc.SaveToFile("项目列表.docx", FileFormat.Docx2013);
  46.             System.Diagnostics.Process.Start("项目列表.docx");
  47.         }
  48.     }
  49. }
创建结果(截图):

VB.NET


  1. Imports Spire.Doc
  2. Imports Spire.Doc.Documents

  3. Namespace WordBullets

  4.     Class Program

  5.         Private Shared Sub Main(ByVal args As String())
  6.             Dim doc As Document = New Document()
  7.             Dim section As Section = doc.AddSection()
  8.             Dim para1 As Paragraph = section.AddParagraph()
  9.             para1.AppendText("国际政治类组织")
  10.             Dim para2 As Paragraph = section.AddParagraph()
  11.             para2.AppendText("欧洲联盟(欧盟)")
  12.             Dim para3 As Paragraph = section.AddParagraph()
  13.             para3.AppendText("独立国家联合体(独联体)")
  14.             Dim para4 As Paragraph = section.AddParagraph()
  15.             para4.AppendText("上海合作组织")
  16.             Dim para5 As Paragraph = section.AddParagraph()
  17.             para5.AppendText("阿拉伯会议联盟")
  18.             Dim para6 As Paragraph = section.AddParagraph()
  19.             para6.AppendText("国际生态安全合作组织")
  20.             Dim para7 As Paragraph = section.AddParagraph()
  21.             para7.AppendText("阿拉伯国家联盟")
  22.             Dim style As ParagraphStyle = New ParagraphStyle(doc)
  23.             style.Name = "fontStyle"
  24.             style.CharacterFormat.FontName = "宋体"
  25.             style.CharacterFormat.FontSize = 12F
  26.             doc.Styles.Add(style)
  27.             For i As Integer = 0 To section.Paragraphs.Count - 1
  28.                 If i <> 0 Then
  29.                     section.Paragraphs(i).ApplyStyle(BuiltinStyle.ListBullet2)
  30.                 End If

  31.                 section.Paragraphs(i).ApplyStyle("fontStyle")
  32.             Next

  33.             doc.SaveToFile("项目列表.docx", FileFormat.Docx2013)
  34.             System.Diagnostics.Process.Start("项目列表.docx")
  35.         End Sub
  36.     End Class
  37. End Namespace


二、 创建多级编号列表

C#

  1. using Spire.Doc;
  2. using Spire.Doc.Documents;
  3. using Spire.Doc.Fields;

  4. namespace Multi_levelList_Doc
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             //新建Word文档
  11.             Document doc = new Document();
  12.             Section section = doc.AddSection();

  13.             //初始化ListStyle对象,指定List类型为数字列表并命名
  14.             ListStyle listStyle = new ListStyle(doc, ListType.Numbered);
  15.             listStyle.Name = "levelstyle";

  16.             //设定一级列表模式为阿拉伯数字
  17.             listStyle.Levels[0].PatternType = ListPatternType.Arabic;

  18.             //设置二级列表数字前缀及模式
  19.             listStyle.Levels[1].NumberPrefix = "\x0000.";
  20.             listStyle.Levels[1].PatternType = ListPatternType.Arabic;

  21.             //设置三级列表数字前缀及模式
  22.             listStyle.Levels[2].NumberPrefix = "\x0000.\x0001.";
  23.             listStyle.Levels[2].PatternType = ListPatternType.Arabic;

  24.             //在ListStyles集合中添加新建的list style
  25.             doc.ListStyles.Add(listStyle);

  26.             //创建字体格式
  27.             Spire.Doc.Formatting.CharacterFormat format = new Spire.Doc.Formatting.CharacterFormat(doc);
  28.             format.FontName = "宋体";

  29.             //添加段落,设置一级序列
  30.             Paragraph paragraph = section.AddParagraph();
  31.             TextRange tr = paragraph.AppendText("主要组织机构");
  32.             tr.ApplyCharacterFormat(format); //应用字体格式
  33.             paragraph.ApplyStyle(BuiltinStyle.Heading1); //应用标题1样式
  34.             paragraph.ListFormat.ApplyStyle("levelstyle"); //应用列表样式

  35.             //添加段落,设置一级序列
  36.             paragraph = section.AddParagraph();
  37.             tr = paragraph.AppendText("主要职能");
  38.             tr.ApplyCharacterFormat(format);
  39.             paragraph.ApplyStyle(BuiltinStyle.Heading1);
  40.             paragraph.ListFormat.ApplyStyle("levelstyle");

  41.             //添加段落,设置二级序列
  42.             paragraph = section.AddParagraph();
  43.             tr = paragraph.AppendText("基本职能");
  44.             tr.ApplyCharacterFormat(format);
  45.             paragraph.ApplyStyle(BuiltinStyle.Heading2);
  46.             paragraph.ListFormat.ListLevelNumber = 1; //设置等级为第二等级
  47.             paragraph.ListFormat.ApplyStyle("levelstyle");

  48.             //添加段落,设置二级序列
  49.             paragraph = section.AddParagraph();
  50.             tr = paragraph.AppendText("5大职能");
  51.             tr.ApplyCharacterFormat(format);
  52.             paragraph.ApplyStyle(BuiltinStyle.Heading2);
  53.             paragraph.ListFormat.ContinueListNumbering();
  54.             paragraph.ListFormat.ApplyStyle("levelstyle");

  55.             //添加段落,设置三级序列
  56.             paragraph = section.AddParagraph();
  57.             tr = paragraph.AppendText("管理职能 \n 组织职能 \n 协调职能 \n 调节职能 \n 提供职能");
  58.             tr.ApplyCharacterFormat(format);
  59.             paragraph.ApplyStyle(BuiltinStyle.Heading5);
  60.             paragraph.ListFormat.ListLevelNumber = 2; //设置等级为第三等级
  61.             paragraph.ListFormat.ApplyStyle("levelstyle");

  62.             //添加段落,设置一级序列
  63.             paragraph = section.AddParagraph();
  64.             tr = paragraph.AppendText("基本原则");
  65.             tr.ApplyCharacterFormat(format);
  66.             paragraph.ApplyStyle(BuiltinStyle.Heading1);
  67.             paragraph.ListFormat.ApplyStyle("levelstyle");

  68.             //保存并打开文档
  69.             doc.SaveToFile("多级列表.docx", FileFormat.Docx);
  70.             System.Diagnostics.Process.Start("多级列表.docx");
  71.         }
  72.     }
  73. }

创建结果(截图):



VB.NET

  1. Imports Spire.Doc
  2. Imports Spire.Doc.Documents
  3. Imports Spire.Doc.Fields

  4. Namespace Multi_levelList_Doc

  5.     Class Program

  6.         Private Shared Sub Main(ByVal args As String())
  7.             Dim doc As Document = New Document()
  8.             Dim section As Section = doc.AddSection()
  9.             Dim listStyle As ListStyle = New ListStyle(doc, ListType.Numbered)
  10.             listStyle.Name = "levelstyle"
  11.             listStyle.Levels(0).PatternType = ListPatternType.Arabic
  12.             listStyle.Levels(1).NumberPrefix = vbNullChar & "."
  13.             listStyle.Levels(1).PatternType = ListPatternType.Arabic
  14.             listStyle.Levels(2).NumberPrefix = vbNullChar & "." & ChrW(1) & "."
  15.             listStyle.Levels(2).PatternType = ListPatternType.Arabic
  16.             doc.ListStyles.Add(listStyle)
  17.             Dim format As Spire.Doc.Formatting.CharacterFormat = New Spire.Doc.Formatting.CharacterFormat(doc)
  18.             format.FontName = "宋体"
  19.             Dim paragraph As Paragraph = section.AddParagraph()
  20.             Dim tr As TextRange = paragraph.AppendText("主要组织机构")
  21.             tr.ApplyCharacterFormat(format)
  22.             paragraph.ApplyStyle(BuiltinStyle.Heading1)
  23.             paragraph.ListFormat.ApplyStyle("levelstyle")
  24.             paragraph = section.AddParagraph()
  25.             tr = paragraph.AppendText("主要职能")
  26.             tr.ApplyCharacterFormat(format)
  27.             paragraph.ApplyStyle(BuiltinStyle.Heading1)
  28.             paragraph.ListFormat.ApplyStyle("levelstyle")
  29.             paragraph = section.AddParagraph()
  30.             tr = paragraph.AppendText("基本职能")
  31.             tr.ApplyCharacterFormat(format)
  32.             paragraph.ApplyStyle(BuiltinStyle.Heading2)
  33.             paragraph.ListFormat.ListLevelNumber = 1
  34.             paragraph.ListFormat.ApplyStyle("levelstyle")
  35.             paragraph = section.AddParagraph()
  36.             tr = paragraph.AppendText("5大职能")
  37.             tr.ApplyCharacterFormat(format)
  38.             paragraph.ApplyStyle(BuiltinStyle.Heading2)
  39.             paragraph.ListFormat.ContinueListNumbering()
  40.             paragraph.ListFormat.ApplyStyle("levelstyle")
  41.             paragraph = section.AddParagraph()
  42.             tr = paragraph.AppendText("管理职能 " & vbLf & " 组织职能 " & vbLf & " 协调职能 " & vbLf & " 调节职能 " & vbLf & " 提供职能")
  43.             tr.ApplyCharacterFormat(format)
  44.             paragraph.ApplyStyle(BuiltinStyle.Heading5)
  45.             paragraph.ListFormat.ListLevelNumber = 2
  46.             paragraph.ListFormat.ApplyStyle("levelstyle")
  47.             paragraph = section.AddParagraph()
  48.             tr = paragraph.AppendText("基本原则")
  49.             tr.ApplyCharacterFormat(format)
  50.             paragraph.ApplyStyle(BuiltinStyle.Heading1)
  51.             paragraph.ListFormat.ApplyStyle("levelstyle")
  52.             doc.SaveToFile("多级列表.docx", FileFormat.Docx)
  53.             System.Diagnostics.Process.Start("多级列表.docx")
  54.         End Sub
  55.     End Class
  56. End Namespace
以上代码供参考,欢迎转载(转载请注明出处)。
感谢阅读!



上一篇:C# 创建Excel透视表
下一篇:C# 如何创建、读取PDF文件