Spire.Doc for .NET是一款專門對 Word 文檔進(jìn)行操作的 .NET 類庫。在于幫助開發(fā)人員無需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開發(fā)經(jīng)驗Spire系列辦公文檔開發(fā)工具,專注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue?功能類庫Spire 系列文檔處理組件均由中國本土團隊研發(fā),不依賴第三方軟件,不受其他國家的技術(shù)或法律法規(guī)限制,同時適配國產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Spire.Doc for.NET 最新下載(qun:767755948)https://www.evget.com/product/3368/download
Word 文檔中使用列表來勾勒、排列和強調(diào)文本,使用戶能夠輕松掃描和理解一系列項目。Word中有三種不同類型的列表,即編號列表,項目符號列表和多級列表。編號列表用于具有序列或優(yōu)先級的項目,例如一系列說明。項目符號列表用于沒有特定優(yōu)先級的項目,例如函數(shù)或事實列表。多級列表用于存在序列且您希望每個段落單獨編號的情況。
在 C# 中插入 Word 中的編號列表
Spire.Doc for .NET 提供了?ListStyle?類,可用于創(chuàng)建編號列表樣式或項目符號樣式。然后,可以使用?Paragraph.ListFormat.ApplyStyle()?方法將列表樣式應(yīng)用于段落。創(chuàng)建編號列表的步驟如下。
- 創(chuàng)建文檔對象。
- 使用?Document.AddSection()?方法添加一個節(jié)。
- 創(chuàng)建?ListStyle?類的實例,將列表類型指定為“已編號”。
- 通過?ListStyle.Levels[index]?屬性獲取列表的特定級別,并通過?ListLevel.PatternType?屬性設(shè)置編號類型。
- 使用?Document.ListStyles.Add()?方法將列表樣式添加到文檔中。
- 使用?Section.AddParagraph()?方法向文檔添加多個段落。
- 使用?Paragraph.ListFormat.ApplyStyle()?方法將列表樣式應(yīng)用于特定段落。
- 通過?Paragraph.ListFormat.ListLevelNumber?屬性指定列表級別。
- 使用?Document.SaveToFile()?方法將文檔保存到 Word 文件。
【C# 】
using Spire.Doc; using Spire.Doc.Documents; namespace CreateOrderedList { class Program { static void Main(string[] args) { //Create a Document object Document document = new Document(); //Add a section Section section = document.AddSection(); //Create a numbered list style 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); //Add a paragraph Paragraph paragraph = section.AddParagraph(); paragraph.AppendText("Required Web Development Skills:"); paragraph.Format.AfterSpacing = 5f; //Add a paragraph and apply the numbered list style to it paragraph = section.AddParagraph(); paragraph.AppendText("HTML"); paragraph.ListFormat.ApplyStyle("numberedList"); paragraph.ListFormat.ListLevelNumber = 0; //Add another four paragraphs and apply the numbered list style to them 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; //Save the document to file document.SaveToFile("NumberedList.docx", FileFormat.Docx); } } }
【VB.NET】
Imports Spire.Doc Imports Spire.Doc.Documents Namespace CreateOrderedList Class Program Shared Sub Main(ByVal args() As String) 'Create a Document object Dim document As Document = New Document() 'Add a section Dim section As Section = document.AddSection() 'Create a numbered list style Dim listStyle As ListStyle = New ListStyle(document,ListType.Numbered) listStyle.Name = "numberedList" listStyle.Levels(0).PatternType = ListPatternType.DecimalEnclosedParen listStyle.Levels(0).TextPosition = 20 document.ListStyles.Add(listStyle) 'Add a paragraph Dim paragraph As Paragraph = section.AddParagraph() paragraph.AppendText("Required Web Development Skills:") paragraph.Format.AfterSpacing = 5f 'Add a paragraph and apply the numbered list style to it paragraph = section.AddParagraph() paragraph.AppendText("HTML") paragraph.ListFormat.ApplyStyle("numberedList") paragraph.ListFormat.ListLevelNumber = 0 'Add another four paragraphs and apply the numbered list style to them 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 'Save the document to file document.SaveToFile("NumberedList.docx", FileFormat.Docx) End Sub End Class End Namespace
在 C# 中插入 Word 中的項目符號列表
創(chuàng)建項目符號列表的過程類似于創(chuàng)建編號列表的過程。不同之處在于,在創(chuàng)建列表樣式時,必須將列表類型指定為"項目符號",并為其設(shè)置項目符號。以下是詳細(xì)步驟。
- 創(chuàng)建文檔對象。
- 使用?Document.AddSection()?方法添加一個節(jié)。
- 創(chuàng)建?ListStyle?類的實例,將列表類型指定為項目符號。
- 通過?ListStyle.Levels[index]?屬性獲取列表的特定級別,并通過?ListLevel.BulletCharacter?屬性設(shè)置項目符號。
- 使用?Document.ListStyles.Add()?方法將列表樣式添加到文檔中。
- 使用?Section.AddParagraph()?方法向文檔添加多個段落。
- 使用?Paragraph.ListFormat.ApplyStyle()?方法將列表樣式應(yīng)用于特定段落。
- 通過?Paragraph.ListFormat.ListLevelNumber?屬性指定列表級別。
- 使用?Document.SaveToFile()?方法將文檔保存到 Word 文件。
【C# 】
using Spire.Doc; using Spire.Doc.Documents; namespace CreateUnorderedList { class Program { static void Main(string[] args) { //Create a Document object Document document = new Document(); //Add a section Section section = document.AddSection(); //Create a bulleted list style 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); //Add a paragraph Paragraph paragraph = section.AddParagraph(); paragraph.AppendText("Computer Science Subjects:"); paragraph.Format.AfterSpacing = 5f; //Add a paragraph and apply the bulleted list style to it paragraph = section.AddParagraph(); paragraph.AppendText("Data Structure"); paragraph.ListFormat.ApplyStyle("bulletedList"); paragraph.ListFormat.ListLevelNumber = 0; //Add another five paragraphs and apply the bulleted list style to them 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; //Save the document to file document.SaveToFile("BulletedList.docx", FileFormat.Docx); } } }
【VB.NET】
Imports Spire.Doc Imports Spire.Doc.Documents Namespace CreateUnorderedList Class Program Shared Sub Main(ByVal args() As String) 'Create a Document object Dim document As Document = New Document() 'Add a section Dim section As Section = document.AddSection() 'Create a bulleted list style Dim listStyle As 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) 'Add a paragraph Dim paragraph As Paragraph = section.AddParagraph() paragraph.AppendText("Computer Science Subjects:") paragraph.Format.AfterSpacing = 5f 'Add a paragraph and apply the bulleted list style to it paragraph = section.AddParagraph() paragraph.AppendText("Data Structure") paragraph.ListFormat.ApplyStyle("bulletedList") paragraph.ListFormat.ListLevelNumber = 0 'Add another five paragraphs and apply the bulleted list style to them 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 'Save the document to file document.SaveToFile("BulletedList.docx", FileFormat.Docx) End Sub End Class End Namespace
在 C# 的 Word 中插入多級編號列表
多級列表至少包含兩個不同的級別。嵌套列表的每個級別都由?ListStyle.Levels[index]?屬性表示,通過該屬性可以設(shè)置特定級別的編號類型和前綴。以下是在 Word 中創(chuàng)建多級編號列表的步驟。
- 創(chuàng)建文檔對象。
- 使用?Document.AddSection()?方法添加一個節(jié)。
- 創(chuàng)建?ListStyle?類的實例,將列表類型指定為“已編號”。
- 通過?ListStyle.Levels[index]?屬性獲取列表的特定級別,并設(shè)置編號類型和前綴。
- 使用?Document.ListStyles.Add()?方法將列表樣式添加到文檔中。
- 使用?Section.AddParagraph()?方法向文檔添加多個段落。
- 使用?Paragraph.ListFormat.ApplyStyle()?方法將列表樣式應(yīng)用于特定段落。
- 通過?Paragraph.ListFormat.ListLevelNumber?屬性指定列表級別。
- 使用?Document.SaveToFile()?方法將文檔保存到 Word 文件。
【C# 】
using Spire.Doc; using Spire.Doc.Documents; namespace CreateMultiLevelList { class Program { static void Main(string[] args) { //Create a Document object Document document = new Document(); //Add a section Section section = document.AddSection(); //Create a numbered list style, specifying number prefix and pattern type of each level ListStyle listStyle = new ListStyle(document, ListType.Numbered); listStyle.Name = "levelstyle"; listStyle.Levels[0].PatternType = ListPatternType.Arabic; listStyle.Levels[0].TextPosition = 20; listStyle.Levels[1].NumberPrefix = "\x0000."; listStyle.Levels[1].PatternType = ListPatternType.Arabic; listStyle.Levels[2].NumberPrefix = "\x0000.\x0001."; listStyle.Levels[2].PatternType = ListPatternType.Arabic; document.ListStyles.Add(listStyle); //Add a paragraph Paragraph paragraph = section.AddParagraph(); paragraph.AppendText("Here's a Multi-Level Numbered List:"); paragraph.Format.AfterSpacing = 5f; //Add a paragraph and apply the numbered list style to it paragraph = section.AddParagraph(); paragraph.AppendText("The first item"); paragraph.ListFormat.ApplyStyle("levelstyle"); paragraph.ListFormat.ListLevelNumber = 0; //Add another five paragraphs and apply the numbered list stype to them paragraph = section.AddParagraph(); paragraph.AppendText("The second item"); paragraph.ListFormat.ApplyStyle("levelstyle"); paragraph.ListFormat.ListLevelNumber = 0; paragraph = section.AddParagraph(); paragraph.AppendText("The first sub-item"); paragraph.ListFormat.ApplyStyle("levelstyle"); paragraph.ListFormat.ListLevelNumber = 1; paragraph = section.AddParagraph(); paragraph.AppendText("The second sub-item"); paragraph.ListFormat.ContinueListNumbering(); paragraph.ListFormat.ApplyStyle("levelstyle"); paragraph = section.AddParagraph(); paragraph.AppendText("A sub-sub-item"); paragraph.ListFormat.ApplyStyle("levelstyle"); paragraph.ListFormat.ListLevelNumber = 2; paragraph = section.AddParagraph(); paragraph.AppendText("The third item"); paragraph.ListFormat.ApplyStyle("levelstyle"); paragraph.ListFormat.ListLevelNumber = 0; //Save the document to file document.SaveToFile("MultilevelNumberedList.docx", FileFormat.Docx); } } }
【VB.NET】
Imports Spire.Doc Imports Spire.Doc.Documents Namespace CreateMultiLevelList Class Program Shared Sub Main(ByVal args() As String) 'Create a Document object Dim document As Document = New Document() 'Add a section Dim section As Section = document.AddSection() 'Create a numbered list style, specifying number prefix and pattern type of each level Dim listStyle As ListStyle = New ListStyle(document,ListType.Numbered) listStyle.Name = "levelstyle" listStyle.Levels(0).PatternType = ListPatternType.Arabic listStyle.Levels(0).TextPosition = 20 listStyle.Levels(1).NumberPrefix = "\x0000." listStyle.Levels(1).PatternType = ListPatternType.Arabic listStyle.Levels(2).NumberPrefix = "\x0000.\x0001." listStyle.Levels(2).PatternType = ListPatternType.Arabic document.ListStyles.Add(listStyle) 'Add a paragraph Dim paragraph As Paragraph = section.AddParagraph() paragraph.AppendText("Here's a Multi-Level Numbered List:") paragraph.Format.AfterSpacing = 5f 'Add a paragraph and apply the numbered list style to it paragraph = section.AddParagraph() paragraph.AppendText("The first item") paragraph.ListFormat.ApplyStyle("levelstyle") paragraph.ListFormat.ListLevelNumber = 0 'Add another five paragraphs and apply the numbered list stype to them paragraph = section.AddParagraph() paragraph.AppendText("The second item") paragraph.ListFormat.ApplyStyle("levelstyle") paragraph.ListFormat.ListLevelNumber = 0 paragraph = section.AddParagraph() paragraph.AppendText("The first sub-item") paragraph.ListFormat.ApplyStyle("levelstyle") paragraph.ListFormat.ListLevelNumber = 1 paragraph = section.AddParagraph() paragraph.AppendText("The second sub-item") paragraph.ListFormat.ContinueListNumbering() paragraph.ListFormat.ApplyStyle("levelstyle") paragraph = section.AddParagraph() paragraph.AppendText("A sub-sub-item") paragraph.ListFormat.ApplyStyle("levelstyle") paragraph.ListFormat.ListLevelNumber = 2 paragraph = section.AddParagraph() paragraph.AppendText("The third item") paragraph.ListFormat.ApplyStyle("levelstyle") paragraph.ListFormat.ListLevelNumber = 0 'Save the document to file document.SaveToFile("MultilevelNumberedList.docx", FileFormat.Docx) End Sub End Class End Namespace
在 C# 的 Word 中插入多級混合類型列表
在某些情況下,您可能希望在多級列表中混合使用數(shù)字和符號項目符號點。要創(chuàng)建混合類型列表,您只需創(chuàng)建編號列表樣式和項目符號列表樣式,并將它們應(yīng)用于不同的段落。具體步驟如下。
- 創(chuàng)建文檔對象。
- 使用?Document.AddSection()?方法添加一個節(jié)。
- 創(chuàng)建編號列表樣式和項目符號列表樣式。
- 使用?Section.AddParagraph()?方法向文檔添加多個段落。
- 使用?Paragraph.ListFormat.ApplyStyle()?方法將不同的列表樣式應(yīng)用于不同的段落。
- 使用?Document.SaveToFile()?方法將文檔保存到 Word 文件。
【C# 】
using Spire.Doc; using Spire.Doc.Documents; namespace CreateMultilevelMixedList { class Program { static void Main(string[] args) { //Create a Document object Document document = new Document(); //Add a section Section section = document.AddSection(); //Create a numbered list style ListStyle numberedListStyle = new ListStyle(document, ListType.Numbered); numberedListStyle.Name = "numberedStyle"; numberedListStyle.Levels[0].PatternType = ListPatternType.Arabic; numberedListStyle.Levels[0].TextPosition = 20; numberedListStyle.Levels[1].PatternType = ListPatternType.LowLetter; document.ListStyles.Add(numberedListStyle); //Create a bulleted list style ListStyle bulletedListStyle = new ListStyle(document, ListType.Bulleted); bulletedListStyle.Name = "bulltedStyle"; bulletedListStyle.Levels[2].BulletCharacter = "\x002A"; bulletedListStyle.Levels[2].CharacterFormat.FontName = "Symbol"; document.ListStyles.Add(bulletedListStyle); //Add a paragraph Paragraph paragraph = section.AddParagraph(); paragraph.AppendText("Here's a Multi-Level Mixed List:"); paragraph.Format.AfterSpacing = 5f; //Add a paragraph and apply the numbered list style to it paragraph = section.AddParagraph(); paragraph.AppendText("The first item"); paragraph.ListFormat.ApplyStyle("numberedStyle"); paragraph.ListFormat.ListLevelNumber = 0; //Add another five paragraphs and apply different list stype to them paragraph = section.AddParagraph(); paragraph.AppendText("The first sub-item"); paragraph.ListFormat.ApplyStyle("numberedStyle"); paragraph.ListFormat.ListLevelNumber = 1; paragraph = section.AddParagraph(); paragraph.AppendText("The second sub-item"); paragraph.ListFormat.ListLevelNumber = 1; paragraph.ListFormat.ApplyStyle("numberedStyle"); paragraph = section.AddParagraph(); paragraph.AppendText("The first sub-sub-item"); paragraph.ListFormat.ApplyStyle("bulltedStyle"); paragraph.ListFormat.ListLevelNumber = 2; paragraph = section.AddParagraph(); paragraph.AppendText("The second sub-sub-item"); paragraph.ListFormat.ApplyStyle("bulltedStyle"); paragraph.ListFormat.ListLevelNumber = 2; paragraph = section.AddParagraph(); paragraph.AppendText("The second item"); paragraph.ListFormat.ApplyStyle("numberedStyle"); paragraph.ListFormat.ListLevelNumber = 0; //Save the document to file document.SaveToFile("MultilevelMixedList.docx", FileFormat.Docx); } } }
【VB.NET】
Imports Spire.Doc Imports Spire.Doc.Documents Namespace CreateMultilevelMixedList Class Program Shared Sub Main(ByVal args() As String) 'Create a Document object Dim document As Document = New Document() 'Add a section Dim section As Section = document.AddSection() 'Create a numbered list style Dim numberedListStyle As ListStyle = New ListStyle(document,ListType.Numbered) numberedListStyle.Name = "numberedStyle" numberedListStyle.Levels(0).PatternType = ListPatternType.Arabic numberedListStyle.Levels(0).TextPosition = 20 numberedListStyle.Levels(1).PatternType = ListPatternType.LowLetter document.ListStyles.Add(numberedListStyle) 'Create a bulleted list style Dim bulletedListStyle As ListStyle = New ListStyle(document,ListType.Bulleted) bulletedListStyle.Name = "bulltedStyle" bulletedListStyle.Levels(2).BulletCharacter = "\x002A" bulletedListStyle.Levels(2).CharacterFormat.FontName = "Symbol" document.ListStyles.Add(bulletedListStyle) 'Add a paragraph Dim paragraph As Paragraph = section.AddParagraph() paragraph.AppendText("Here's a Multi-Level Mixed List:") paragraph.Format.AfterSpacing = 5f 'Add a paragraph and apply the numbered list style to it paragraph = section.AddParagraph() paragraph.AppendText("The first item") paragraph.ListFormat.ApplyStyle("numberedStyle") paragraph.ListFormat.ListLevelNumber = 0 'Add another five paragraphs and apply different list stype to them paragraph = section.AddParagraph() paragraph.AppendText("The first sub-item") paragraph.ListFormat.ApplyStyle("numberedStyle") paragraph.ListFormat.ListLevelNumber = 1 paragraph = section.AddParagraph() paragraph.AppendText("The second sub-item") paragraph.ListFormat.ListLevelNumber = 1 paragraph.ListFormat.ApplyStyle("numberedStyle") paragraph = section.AddParagraph() paragraph.AppendText("The first sub-sub-item") paragraph.ListFormat.ApplyStyle("bulltedStyle") paragraph.ListFormat.ListLevelNumber = 2 paragraph = section.AddParagraph() paragraph.AppendText("The second sub-sub-item") paragraph.ListFormat.ApplyStyle("bulltedStyle") paragraph.ListFormat.ListLevelNumber = 2 paragraph = section.AddParagraph() paragraph.AppendText("The second item") paragraph.ListFormat.ApplyStyle("numberedStyle") paragraph.ListFormat.ListLevelNumber = 0 'Save the document to file document.SaveToFile("MultilevelMixedList.docx", FileFormat.Docx) End Sub End Class End Namespace
文章來源:http://www.zghlxwxcb.cn/news/detail-459783.html
以上便是如何在 Word 文檔中插入列表的教程,如果您有其他問題也可以繼續(xù)瀏覽本系列文章。文章來源地址http://www.zghlxwxcb.cn/news/detail-459783.html
到了這里,關(guān)于Word控件Spire.Doc 【列表】教程:在 Word 文檔中插入列表的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!