串列分類是指在Word檔案中使用不同格式排序的串列,來幫助我們一目了然地表達出一段文字的主要內容,比如,當我們描述了某個主題的若干點,就可以用串列把它們一一表達出來,而不是寫成完整的段落形式,同時,串列也可以幫助我們做出精確的計算和比較,簡潔有效地表示出不同部分之間的關系,在Word檔案中創建串列可以便于人們去檢索資料方便定位,其中總共有四種不同型別的串列:編號串列、專案符號串列、多級編號串列和多級混合型別串列,本文就將詳細為您介紹如何使用C++在Word檔案中創建編號串列、專案符號串列和多級串列,
- 在Word中創建編號串列
- 在Word中創建專案符號串列
- 在Word中創建多級編號串列
- 在Word中創建多級混合型別串列
安裝 Spire.Doc for C++
有兩種方法可以將 Spire.Doc for C++ 集成到您的應用程式中,一種方法是通過 NuGet 安裝它,另一種方法是從我們的網站下載包并將庫復制到您的程式中,通過 NuGet 安裝更簡單,更推薦使用,您可以通過訪問以下鏈接找到更多詳細資訊,
如何將 Spire.Doc for C++ 集成到 C++ 程式中
在Word中創建編號串列
您可以使用ListStyle類創建編號串列樣式或專案符號樣式,然后,可以使用Paragraph->GetListFormat()->ApplyStyle() 方法將串列樣式應用于段落,創建編號串列的步驟如下,
- 創建一個Document物件,
- 使用Document->AddSection() 方法添加一個節,
- 創建ListStyle類的實體,將串列型別指定為Numbered,
- 使用ListStyle->GetLevels()->GetItem(index) 方法獲取串列的特定級別,并使用ListLevel->SetPatternType() 方法設定編號型別,
- 使用Document->GetListStyles()->Add() 方法將串列樣式添加到檔案中,
- 使用Section->AddParagraph() 方法將多個段落添加到檔案中,
- 使用Paragraph->GetListFormat()->ApplyStyle() 方法將串列樣式應用于特定段落,
- 使用Paragraph->GetListFormat()->GetListLevelNumber() 方法指定串列級別,
- 使用Document->SaveToFile() 方法將檔案保存到Word檔案中,
完整代碼
C++
#include "Spire.Doc.o.h"; using namespace Spire::Doc; using namespace std; int main() { //創建一個Document物件 intrusive_ptr<Document> document = new Document(); //添加一個節 intrusive_ptr<Section> section = document->AddSection(); //創建編號串列樣式 intrusive_ptr<ListStyle> listStyle = new ListStyle(document, ListType::Numbered); listStyle->SetName(L"numberedList"); listStyle->GetLevels()->GetItem(0)->SetPatternType(ListPatternType::DecimalEnclosedParen); listStyle->GetLevels()->GetItem(0)->SetTextPosition(20); document->GetListStyles()->Add(listStyle); //添加一個段落 intrusive_ptr<Paragraph> paragraph = section->AddParagraph(); paragraph->AppendText(L"完整的論證要素:"); paragraph->GetFormat()->SetAfterSpacing(5); //添加段落并對其應用編號串列樣式 paragraph = section->AddParagraph(); paragraph->AppendText(L"論題"); paragraph->GetListFormat()->ApplyStyle(L"numberedList"); paragraph->GetListFormat()->SetListLevelNumber(0); //再添加四個段落,并將編號串列樣式應用于特定段落 paragraph = section->AddParagraph(); paragraph->AppendText(L"論點"); paragraph->GetListFormat()->ApplyStyle(L"numberedList"); paragraph->GetListFormat()->SetListLevelNumber(0); paragraph = section->AddParagraph(); paragraph->AppendText(L"論據"); paragraph->GetListFormat()->ApplyStyle(L"numberedList"); paragraph->GetListFormat()->SetListLevelNumber(0); paragraph = section->AddParagraph(); paragraph->AppendText(L"論證方式"); paragraph->GetListFormat()->ApplyStyle(L"numberedList"); paragraph->GetListFormat()->SetListLevelNumber(0); //將檔案保存為Word檔案 document->SaveToFile(L"FE編號串列.docx", FileFormat::Docx2019); document->Dispose(); }
效果圖

在Word中創建專案符號串列
創建專案符號串列的程序與創建編號串列的程序類似,不同之處在于,創建串列樣式時,必須將串列型別指定為“專案符號”,并為其設定專案符號,以下是詳細步驟,
- 創建一個Document物件,
- 使用Document->AddSection() 方法添加一個節,
- 創建ListStyle類的實體,將串列型別指定為“Bulleted”,
- 使用ListStyle->GetLevels()->Get(index) 方法獲取串列的特定級別,并使用ListLevel->SetBulletCharacter() 方法設定專案符號,
- 使用Document->GetListStyles()->Add() 方法將串列樣式添加到檔案中,
- 使用Section->AddParagraph() 方法將多個段落添加到檔案中,
- 使用Paragraph->GetListFormat()->ApplyStyle() 方法將串列樣式應用于特定段落,
- 使用Paragraph->GetListFormat()->SetListLevelNumber() 方法指定串列級別,
- 使用Document->SaveToFile() 方法將檔案保存到Word檔案中,
完整代碼
C++
#include "Spire.Doc.o.h"; using namespace Spire::Doc; using namespace std; int main() { //創建一個Document物件 intrusive_ptr<Document> document = new Document(); //添加一個節 intrusive_ptr<Section> section = document->AddSection(); //創建專案符號串列樣式 intrusive_ptr<ListStyle> listStyle = new ListStyle(document, ListType::Bulleted); listStyle->SetName(L"bulletedList"); listStyle->GetLevels()->GetItem(0)->SetBulletCharacter(L"\u00B7"); listStyle->GetLevels()->GetItem(0)->GetCharacterFormat()->SetFontName(L"Symbol"); listStyle->GetLevels()->GetItem(0)->SetTextPosition(20); document->GetListStyles()->Add(listStyle); //添加一個段落 intrusive_ptr<Paragraph> paragraph = section->AddParagraph(); paragraph->AppendText(L"常用的六種論證方法:"); paragraph->GetFormat()->SetAfterSpacing(5); //添加段落并對其應用專案符號串列樣式 paragraph = section->AddParagraph(); paragraph->AppendText(L"舉例論證"); paragraph->GetListFormat()->ApplyStyle(L"bulletedList"); paragraph->GetListFormat()->SetListLevelNumber(0); //再添加五個段落,并將專案符號串列樣式應用于特定段落 paragraph = section->AddParagraph(); paragraph->AppendText(L"道理論證"); paragraph->GetListFormat()->ApplyStyle(L"bulletedList"); paragraph->GetListFormat()->SetListLevelNumber(0); paragraph = section->AddParagraph(); paragraph->AppendText(L"對比論證"); paragraph->GetListFormat()->ApplyStyle(L"bulletedList"); paragraph->GetListFormat()->SetListLevelNumber(0); paragraph = section->AddParagraph(); paragraph->AppendText(L"比喻論證"); paragraph->GetListFormat()->ApplyStyle(L"bulletedList"); paragraph->GetListFormat()->SetListLevelNumber(0); paragraph = section->AddParagraph(); paragraph->AppendText(L"參考論證"); paragraph->GetListFormat()->ApplyStyle(L"bulletedList"); paragraph->GetListFormat()->SetListLevelNumber(0); paragraph = section->AddParagraph(); paragraph->AppendText(L"因果論證"); paragraph->GetListFormat()->ApplyStyle(L"bulletedList"); paragraph->GetListFormat()->SetListLevelNumber(0); //保存結果檔案 document->SaveToFile(L"FE專案符號串列.docx", FileFormat::Docx2019); document->Dispose(); }
效果圖

在Word中創建多級編號串列
多級串列至少由兩個不同的級別組成,嵌套串列的每個級別都可以使用ListStyle->GetLevels()->GetItem(index) 方法進行訪問,通過ListLevel物件,您可以設定某個級別的編號型別和前綴,以下是在Word中創建多級編號串列的步驟,
- 創建一個Document物件,
- 使用Document->AddSection() 方法添加一個節,
- 創建ListStyle類的實體,將串列型別指定為Numbered,
- 使用ListStyle->GetLevels()->GetItem(index) 方法獲取串列的特定級別,并設定編號型別和前綴,
- 使用Document->GetListStyles()->Add() 方法將串列樣式添加到檔案中,
- 使用Section->AddParagraph() 方法將多個段落添加到檔案中,
- 使用Paragraph->GetListFormat()->ApplyStyle() 方法將串列樣式應用于特定段落,
- 使用Paragraph->GetListFormat()->SetListLevelNumber() 方法指定串列級別,
- 使用Document->SaveToFile() 方法將檔案保存到Word檔案中,
完整代碼
C++
#include "Spire.Doc.o.h"; using namespace Spire::Doc; using namespace std; int main() { //創建一個Document物件 intrusive_ptr<Document> document = new Document(); //添加一個節 intrusive_ptr<Section> section = document->AddSection(); //創建編號串列樣式,指定每個級別的編號前綴和圖案型別 intrusive_ptr<ListStyle> listStyle = new ListStyle(document, ListType::Numbered); listStyle->SetName(L"nestedStyle"); listStyle->GetLevels()->GetItem(0)->SetPatternType(ListPatternType::Arabic); listStyle->GetLevels()->GetItem(0)->SetTextPosition(20); listStyle->GetLevels()->GetItem(1)->SetNumberPrefix(L"%1."); listStyle->GetLevels()->GetItem(1)->SetPatternType(ListPatternType::Arabic); listStyle->GetLevels()->GetItem(2)->SetNumberPrefix(L"%1.%2."); listStyle->GetLevels()->GetItem(2)->SetPatternType(ListPatternType::Arabic); document->GetListStyles()->Add(listStyle); //添加一個段落 intrusive_ptr<Paragraph> paragraph = section->AddParagraph(); paragraph->AppendText(L"這是一個多級編號串列:"); paragraph->GetFormat()->SetAfterSpacing(5); //添加段落并對其應用編號串列樣式 paragraph = section->AddParagraph(); paragraph->AppendText(L"水果"); paragraph->GetListFormat()->ApplyStyle(L"nestedStyle"); paragraph->GetListFormat()->SetListLevelNumber(0); //再添加五個段落,并將編號串列樣式應用于特定段落 paragraph = section->AddParagraph(); paragraph->AppendText(L"蔬菜"); paragraph->GetListFormat()->ApplyStyle(L"nestedStyle"); paragraph->GetListFormat()->SetListLevelNumber(0); paragraph = section->AddParagraph(); paragraph->AppendText(L"根菜類"); paragraph->GetListFormat()->ApplyStyle(L"nestedStyle"); paragraph->GetListFormat()->SetListLevelNumber(1); paragraph = section->AddParagraph(); paragraph->AppendText(L"葉菜類"); paragraph->GetListFormat()->ContinueListNumbering(); paragraph->GetListFormat()->ApplyStyle(L"nestedStyle"); paragraph = section->AddParagraph(); paragraph->AppendText(L"小白菜"); paragraph->GetListFormat()->ApplyStyle(L"nestedStyle"); paragraph->GetListFormat()->SetListLevelNumber(2); paragraph = section->AddParagraph(); paragraph->AppendText(L"谷物"); paragraph->GetListFormat()->ApplyStyle(L"nestedStyle"); paragraph->GetListFormat()->SetListLevelNumber(0); //保存結果檔案 document->SaveToFile(L"FE多級編號串列.docx", FileFormat::Docx2019); document->Dispose(); }
效果圖

在Word中創建多級混合型別串列
多級串列可以是編號串列和專案符號串列的組合,要創建混合型別串列,只需要創建編號串列樣式和專案符號串列樣式,并將它們應用于不同的段落,具體步驟如下,
- 創建一個Document物件,
- 使用Document->AddSection() 方法添加一個節,
- 創建編號串列樣式和專案符號串列樣式,
- 使用Section->AddParagraph() 方法將多個段落添加到檔案中,
- 使用Paragraph->GgetListFormat()->ApplyStyle() 方法將不同的串列樣式應用于不同的段落,
- 使用Document->SaveToFile() 方法將檔案保存到Word檔案中,
完整代碼
C++
#include "Spire.Doc.o.h"; using namespace Spire::Doc; using namespace std; int main() { //創建一個Document物件 intrusive_ptr<Document> document = new Document(); //添加一個節 intrusive_ptr<Section> section = document->AddSection(); //創建編號串列樣式 intrusive_ptr<ListStyle> numberedListStyle = new ListStyle(document, ListType::Numbered); numberedListStyle->SetName(L"numberedStyle"); numberedListStyle->GetLevels()->GetItem(0)->SetPatternType(ListPatternType::Arabic); numberedListStyle->GetLevels()->GetItem(0)->SetTextPosition(20); numberedListStyle->GetLevels()->GetItem(1)->SetPatternType(ListPatternType::LowLetter); document->GetListStyles()->Add(numberedListStyle); //創建專案符號串列樣式 intrusive_ptr<ListStyle> bulletedListStyle = new ListStyle(document, ListType::Bulleted); bulletedListStyle->SetName(L"bulletedStyle"); bulletedListStyle->GetLevels()->GetItem(2)->SetBulletCharacter(L"\u002A"); bulletedListStyle->GetLevels()->GetItem(2)->GetCharacterFormat()->SetFontName(L"Symbol"); document->GetListStyles()->Add(bulletedListStyle); //添加段落 intrusive_ptr<Paragraph> paragraph = section->AddParagraph(); paragraph->AppendText(L"這是一個多級混合串列:"); paragraph->GetFormat()->SetAfterSpacing(5); //添加段落并對其應用編號串列樣式 paragraph = section->AddParagraph(); paragraph->AppendText(L"水果"); paragraph->GetListFormat()->ApplyStyle(L"numberedStyle"); paragraph->GetListFormat()->SetListLevelNumber(0); //再添加五個段落,并對其應用不同的串列樣式 paragraph = section->AddParagraph(); paragraph->AppendText(L"瓜果類"); paragraph->GetListFormat()->ApplyStyle(L"numberedStyle"); paragraph->GetListFormat()->SetListLevelNumber(1); paragraph = section->AddParagraph(); paragraph->AppendText(L"漿果類"); paragraph->GetListFormat()->SetListLevelNumber(1); paragraph->GetListFormat()->ApplyStyle(L"numberedStyle"); paragraph = section->AddParagraph(); paragraph->AppendText(L"蔓越莓"); paragraph->GetListFormat()->ApplyStyle(L"bulletedStyle"); paragraph->GetListFormat()->SetListLevelNumber(2); paragraph = section->AddParagraph(); paragraph->AppendText(L"覆盆子"); paragraph->GetListFormat()->ApplyStyle(L"bulletedStyle"); paragraph->GetListFormat()->SetListLevelNumber(2); paragraph = section->AddParagraph(); paragraph->AppendText(L"蔬菜"); paragraph->GetListFormat()->ApplyStyle(L"numberedStyle"); paragraph->GetListFormat()->SetListLevelNumber(0); //保存結果檔案 document->SaveToFile(L"FE多級混合型別串列.docx", FileFormat::Docx); document->Dispose(); }
效果圖

—本文完—
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/553519.html
標籤:其他
上一篇:Java的Atomic原子類
下一篇:返回列表
