使用C#代碼在Word文檔中插入列表的操作方法
列表在 Word 文檔中用于概述、排列和強(qiáng)調(diào)文本,使用戶能夠輕松掃描和理解一系列項(xiàng)目。Word 中有三種不同類型的列表,分別是編號(hào)列表、項(xiàng)目符號(hào)列表和多級(jí)列表。編號(hào)列表用于表示具有順序或優(yōu)先級(jí)的項(xiàng)目,例如一系列的指示。項(xiàng)目符號(hào)列表用于表示沒有特定優(yōu)先級(jí)的項(xiàng)目,例如功能或事實(shí)列表。多級(jí)列表用于存在順序且希望每個(gè)段落單獨(dú)編號(hào)的情況。
在本文中,您將學(xué)習(xí)如何使用 Spire.Doc for .NET 在 C# 和 VB.NET 中將這些類型的列表插入到 Word 文檔中。
安裝 Spire.Doc for .NET
首先,您需要將 Spire.Doc for .NET 包中包含的 DLL 文件添加為 .NET 項(xiàng)目的引用。這些 DLL 文件可以通過此鏈接下載,或者通過 NuGet 安裝。
PM> Install-Package Spire.Doc
在 C# 和 VB.NET 中插入編號(hào)列表
Spire.Doc for .NET 提供了 ListStyle 類,您可以使用它來創(chuàng)建編號(hào)列表樣式或項(xiàng)目符號(hào)樣式。然后,可以通過 Paragraph.ListFormat.ApplyStyle() 方法將列表樣式應(yīng)用到段落中。
創(chuàng)建編號(hào)列表的示例代碼如下:
using Spire.Doc;
using Spire.Doc.Documents;
namespace CreateOrderedList
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建一個(gè) Document 對(duì)象
Document document = new Document();
//添加一個(gè)節(jié)
Section section = document.AddSection();
//創(chuàng)建一個(gè)編號(hào)列表樣式
ListStyle listStyle = new ListStyle(document, ListType.Numbered);
listStyle.Name = "numberedList";
listStyle.Levels[0].PatternType = ListPatternType.DecimalEnclosedParen;
listStyle.Levels[0].TextPosition = 20;
document.ListStyles.Add(listStyle);
//添加一個(gè)段落
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Required Web Development Skills:");
paragraph.Format.AfterSpacing = 5f;
//添加一個(gè)段落并應(yīng)用編號(hào)列表樣式
paragraph = section.AddParagraph();
paragraph.AppendText("HTML");
paragraph.ListFormat.ApplyStyle("numberedList");
paragraph.ListFormat.ListLevelNumber = 0;
//添加另外四個(gè)段落并應(yīng)用編號(hào)列表樣式
paragraph = section.AddParagraph();
paragraph.AppendText("CSS");
paragraph.ListFormat.ApplyStyle("numberedList");
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("JavaScript");
paragraph.ListFormat.ApplyStyle("numberedList");
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("Python");
paragraph.ListFormat.ApplyStyle("numberedList");
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("MySQL");
paragraph.ListFormat.ApplyStyle("numberedList");
paragraph.ListFormat.ListLevelNumber = 0;
//保存結(jié)果文件
document.SaveToFile("NumberedList.docx", FileFormat.Docx);
}
}
}在 C# 和 VB.NET 中插入項(xiàng)目符號(hào)列表
創(chuàng)建項(xiàng)目符號(hào)列表的過程與創(chuàng)建編號(hào)列表類似。不同之處在于,在創(chuàng)建列表樣式時(shí),必須將列表類型指定為“項(xiàng)目符號(hào)”,并為其設(shè)置一個(gè)項(xiàng)目符號(hào)符號(hào)。以下是相關(guān)代碼示例:
using Spire.Doc;
using Spire.Doc.Documents;
namespace CreateUnorderedList
{
class Program
{
static void Main(string[] args)
{
//創(chuàng)建一個(gè) Document 對(duì)象
Document document = new Document();
//添加一個(gè)節(jié)
Section section = document.AddSection();
//創(chuàng)建一個(gè)項(xiàng)目符號(hào)列表樣式
ListStyle listStyle = new ListStyle(document, ListType.Bulleted);
listStyle.Name = "bulletedList";
listStyle.Levels[0].BulletCharacter = "\x00B7";
listStyle.Levels[0].CharacterFormat.FontName = "Symbol";
listStyle.Levels[0].TextPosition = 20;
document.ListStyles.Add(listStyle);
//添加一個(gè)段落
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Computer Science Subjects:");
paragraph.Format.AfterSpacing = 5f;
//添加一個(gè)段落并應(yīng)用項(xiàng)目符號(hào)列表樣式
paragraph = section.AddParagraph();
paragraph.AppendText("Data Structure");
paragraph.ListFormat.ApplyStyle("bulletedList");
paragraph.ListFormat.ListLevelNumber = 0;
//添加另外五個(gè)段落并應(yīng)用項(xiàng)目符號(hào)列表樣式
paragraph = section.AddParagraph();
paragraph.AppendText("Algorithm");
paragraph.ListFormat.ApplyStyle("bulletedList");
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("Computer Networks");
paragraph.ListFormat.ApplyStyle("bulletedList");
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("Operating System");
paragraph.ListFormat.ApplyStyle("bulletedList");
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("C Programming");
paragraph.ListFormat.ApplyStyle("bulletedList");
paragraph.ListFormat.ListLevelNumber = 0;
paragraph = section.AddParagraph();
paragraph.AppendText("Theory of Computations");
paragraph.ListFormat.ApplyStyle("bulletedList");
paragraph.ListFormat.ListLevelNumber = 0;
//保存結(jié)果文件
document.SaveToFile("BulletedList.docx", FileFormat.Docx);
}
}
}申請(qǐng)臨時(shí)許可證
如果您希望從生成的文檔中移除評(píng)估信息,或解除功能限制,請(qǐng)申請(qǐng)一份 30 天的試用許可證。
到此這篇關(guān)于使用C#代碼在Word文檔中插入列表的操作方法的文章就介紹到這了,更多相關(guān)C# Word插入列表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用Newtonsoft將json串轉(zhuǎn)為對(duì)象的方法(詳解)
下面小編就為大家?guī)硪黄肗ewtonsoft將json串轉(zhuǎn)為對(duì)象的方法(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Unity5.6大規(guī)模地形資源創(chuàng)建方法
word ppt excel文檔轉(zhuǎn)換成pdf的C#實(shí)現(xiàn)代碼

