C#/VB.NET 創建Word項目符號列表、多級項目編號列表

(本文爲轉載文章,可在此處查看原文: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

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章