詳解C++如何實(shí)現(xiàn)在Word文檔中創(chuàng)建列表
列表分類是指在Word文檔中使用不同格式排序的列表,來幫助我們一目了然地表達(dá)出一段文字的主要內(nèi)容。比如,當(dāng)我們描述了某個(gè)主題的若干點(diǎn),就可以用列表把它們一一表達(dá)出來,而不是寫成完整的段落形式。同時(shí),列表也可以幫助我們做出精確的計(jì)算和比較,簡潔有效地表示出不同部分之間的關(guān)系。在Word文檔中創(chuàng)建列表可以便于人們?nèi)z索資料方便定位,其中總共有四種不同類型的列表:編號(hào)列表、項(xiàng)目符號(hào)列表、多級(jí)編號(hào)列表和多級(jí)混合類型列表。本文就將詳細(xì)為您介紹如何使用C++在Word文檔中創(chuàng)建編號(hào)列表、項(xiàng)目符號(hào)列表和多級(jí)列表。
- 在Word中創(chuàng)建編號(hào)列表
- 在Word中創(chuàng)建項(xiàng)目符號(hào)列表
- 在Word中創(chuàng)建多級(jí)編號(hào)列表
- 在Word中創(chuàng)建多級(jí)混合類型列表
安裝 Spire.Doc for C++
有兩種方法可以將 Spire.Doc for C++ 集成到您的應(yīng)用程序中。一種方法是通過 NuGet 安裝它,另一種方法是從我們的網(wǎng)站下載包并將庫復(fù)制到您的程序中。通過 NuGet 安裝更簡單,更推薦使用。您可以通過訪問以下鏈接找到更多詳細(xì)信息。
如何將 Spire.Doc for C++ 集成到 C++ 程序中
在Word中創(chuàng)建編號(hào)列表
您可以使用ListStyle類創(chuàng)建編號(hào)列表樣式或項(xiàng)目符號(hào)樣式。然后,可以使用Paragraph->GetListFormat()->ApplyStyle() 方法將列表樣式應(yīng)用于段落。創(chuàng)建編號(hào)列表的步驟如下。
- 創(chuàng)建一個(gè)Document對(duì)象。
- 使用Document->AddSection() 方法添加一個(gè)節(jié)。
- 創(chuàng)建ListStyle類的實(shí)例,將列表類型指定為Numbered。
- 使用ListStyle->GetLevels()->GetItem(index) 方法獲取列表的特定級(jí)別,并使用ListLevel->SetPatternType() 方法設(shè)置編號(hào)類型。
- 使用Document->GetListStyles()->Add() 方法將列表樣式添加到文檔中。
- 使用Section->AddParagraph() 方法將多個(gè)段落添加到文檔中。
- 使用Paragraph->GetListFormat()->ApplyStyle() 方法將列表樣式應(yīng)用于特定段落。
- 使用Paragraph->GetListFormat()->GetListLevelNumber() 方法指定列表級(jí)別。
- 使用Document->SaveToFile() 方法將文檔保存到Word文件中。
完整代碼
C++
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//創(chuàng)建一個(gè)Document對(duì)象
intrusive_ptr<Document> document = new Document();
//添加一個(gè)節(jié)
intrusive_ptr<Section> section = document->AddSection();
//創(chuàng)建編號(hào)列表樣式
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);
//添加一個(gè)段落
intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
paragraph->AppendText(L"完整的論證要素:");
paragraph->GetFormat()->SetAfterSpacing(5);
//添加段落并對(duì)其應(yīng)用編號(hào)列表樣式
paragraph = section->AddParagraph();
paragraph->AppendText(L"論題");
paragraph->GetListFormat()->ApplyStyle(L"numberedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
//再添加四個(gè)段落,并將編號(hào)列表樣式應(yīng)用于特定段落
paragraph = section->AddParagraph();
paragraph->AppendText(L"論點(diǎn)");
paragraph->GetListFormat()->ApplyStyle(L"numberedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
paragraph = section->AddParagraph();
paragraph->AppendText(L"論據(jù)");
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編號(hào)列表.docx", FileFormat::Docx2019);
document->Dispose();
}效果圖

在Word中創(chuàng)建項(xiàng)目符號(hào)列表
創(chuàng)建項(xiàng)目符號(hào)列表的過程與創(chuàng)建編號(hào)列表的過程類似。不同之處在于,創(chuàng)建列表樣式時(shí),必須將列表類型指定為“項(xiàng)目符號(hào)”,并為其設(shè)置項(xiàng)目符號(hào)。以下是詳細(xì)步驟。
- 創(chuàng)建一個(gè)Document對(duì)象。
- 使用Document->AddSection() 方法添加一個(gè)節(jié)。
- 創(chuàng)建ListStyle類的實(shí)例,將列表類型指定為“Bulleted”。
- 使用ListStyle->GetLevels()->Get(index) 方法獲取列表的特定級(jí)別,并使用ListLevel->SetBulletCharacter() 方法設(shè)置項(xiàng)目符號(hào)。
- 使用Document->GetListStyles()->Add() 方法將列表樣式添加到文檔中。
- 使用Section->AddParagraph() 方法將多個(gè)段落添加到文檔中。
- 使用Paragraph->GetListFormat()->ApplyStyle() 方法將列表樣式應(yīng)用于特定段落。
- 使用Paragraph->GetListFormat()->SetListLevelNumber() 方法指定列表級(jí)別。
- 使用Document->SaveToFile() 方法將文檔保存到Word文件中。
完整代碼
C++
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//創(chuàng)建一個(gè)Document對(duì)象
intrusive_ptr<Document> document = new Document();
//添加一個(gè)節(jié)
intrusive_ptr<Section> section = document->AddSection();
//創(chuàng)建項(xiàng)目符號(hào)列表樣式
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);
//添加一個(gè)段落
intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
paragraph->AppendText(L"常用的六種論證方法:");
paragraph->GetFormat()->SetAfterSpacing(5);
//添加段落并對(duì)其應(yīng)用項(xiàng)目符號(hào)列表樣式
paragraph = section->AddParagraph();
paragraph->AppendText(L"舉例論證");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
//再添加五個(gè)段落,并將項(xiàng)目符號(hào)列表樣式應(yīng)用于特定段落
paragraph = section->AddParagraph();
paragraph->AppendText(L"道理論證");
paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
paragraph->GetListFormat()->SetListLevelNumber(0);
paragraph = section->AddParagraph();
paragraph->AppendText(L"對(duì)比論證");
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);
//保存結(jié)果文檔
document->SaveToFile(L"FE項(xiàng)目符號(hào)列表.docx", FileFormat::Docx2019);
document->Dispose();
}效果圖

在Word中創(chuàng)建多級(jí)編號(hào)列表
多級(jí)列表至少由兩個(gè)不同的級(jí)別組成。嵌套列表的每個(gè)級(jí)別都可以使用ListStyle->GetLevels()->GetItem(index) 方法進(jìn)行訪問。通過ListLevel對(duì)象,您可以設(shè)置某個(gè)級(jí)別的編號(hào)類型和前綴。以下是在Word中創(chuàng)建多級(jí)編號(hào)列表的步驟。
- 創(chuàng)建一個(gè)Document對(duì)象。
- 使用Document->AddSection() 方法添加一個(gè)節(jié)。
- 創(chuàng)建ListStyle類的實(shí)例,將列表類型指定為Numbered。
- 使用ListStyle->GetLevels()->GetItem(index) 方法獲取列表的特定級(jí)別,并設(shè)置編號(hào)類型和前綴。
- 使用Document->GetListStyles()->Add() 方法將列表樣式添加到文檔中。
- 使用Section->AddParagraph() 方法將多個(gè)段落添加到文檔中。
- 使用Paragraph->GetListFormat()->ApplyStyle() 方法將列表樣式應(yīng)用于特定段落。
- 使用Paragraph->GetListFormat()->SetListLevelNumber() 方法指定列表級(jí)別。
- 使用Document->SaveToFile() 方法將文檔保存到Word文件中。
完整代碼
C++
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//創(chuàng)建一個(gè)Document對(duì)象
intrusive_ptr<Document> document = new Document();
//添加一個(gè)節(jié)
intrusive_ptr<Section> section = document->AddSection();
//創(chuàng)建編號(hào)列表樣式,指定每個(gè)級(jí)別的編號(hào)前綴和圖案類型
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);
//添加一個(gè)段落
intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
paragraph->AppendText(L"這是一個(gè)多級(jí)編號(hào)列表:");
paragraph->GetFormat()->SetAfterSpacing(5);
//添加段落并對(duì)其應(yīng)用編號(hào)列表樣式
paragraph = section->AddParagraph();
paragraph->AppendText(L"水果");
paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
paragraph->GetListFormat()->SetListLevelNumber(0);
//再添加五個(gè)段落,并將編號(hào)列表樣式應(yīng)用于特定段落
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);
//保存結(jié)果文檔
document->SaveToFile(L"FE多級(jí)編號(hào)列表.docx", FileFormat::Docx2019);
document->Dispose();
}效果圖

在Word中創(chuàng)建多級(jí)混合類型列表
多級(jí)列表可以是編號(hào)列表和項(xiàng)目符號(hào)列表的組合。要?jiǎng)?chuàng)建混合類型列表,只需要?jiǎng)?chuàng)建編號(hào)列表樣式和項(xiàng)目符號(hào)列表樣式,并將它們應(yīng)用于不同的段落。具體步驟如下。
- 創(chuàng)建一個(gè)Document對(duì)象。
- 使用Document->AddSection() 方法添加一個(gè)節(jié)。
- 創(chuàng)建編號(hào)列表樣式和項(xiàng)目符號(hào)列表樣式。
- 使用Section->AddParagraph() 方法將多個(gè)段落添加到文檔中。
- 使用Paragraph->GgetListFormat()->ApplyStyle() 方法將不同的列表樣式應(yīng)用于不同的段落。
- 使用Document->SaveToFile() 方法將文檔保存到Word文件中。
完整代碼
C++
#include "Spire.Doc.o.h";
using namespace Spire::Doc;
using namespace std;
int main() {
//創(chuàng)建一個(gè)Document對(duì)象
intrusive_ptr<Document> document = new Document();
//添加一個(gè)節(jié)
intrusive_ptr<Section> section = document->AddSection();
//創(chuàng)建編號(hào)列表樣式
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);
//創(chuàng)建項(xiàng)目符號(hào)列表樣式
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"這是一個(gè)多級(jí)混合列表:");
paragraph->GetFormat()->SetAfterSpacing(5);
//添加段落并對(duì)其應(yīng)用編號(hào)列表樣式
paragraph = section->AddParagraph();
paragraph->AppendText(L"水果");
paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
paragraph->GetListFormat()->SetListLevelNumber(0);
//再添加五個(gè)段落,并對(duì)其應(yīng)用不同的列表樣式
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);
//保存結(jié)果文檔
document->SaveToFile(L"FE多級(jí)混合類型列表.docx", FileFormat::Docx);
document->Dispose();
}效果圖

以上就是詳解C++如何實(shí)現(xiàn)在Word文檔中創(chuàng)建列表的詳細(xì)內(nèi)容,更多關(guān)于C++創(chuàng)建列表的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++構(gòu)造函數(shù)+復(fù)制構(gòu)造函數(shù)+重載等號(hào)運(yùn)算符調(diào)用
這篇文章主要介紹了C++構(gòu)造函數(shù)+復(fù)制構(gòu)造函數(shù)+重載等號(hào)運(yùn)算符調(diào)用,文章敘述詳細(xì),具有一定的的參考價(jià)值,需要的小伙伴可以參考一下2022-03-03
MFC實(shí)現(xiàn)學(xué)生選課系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了MFC實(shí)現(xiàn)學(xué)生選課系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
json格式解析和libjson的用法介紹(關(guān)于cjson的使用方法)
下面小編就為大家?guī)硪黄猨son格式解析和libjson的用法介紹(關(guān)于cjson的使用方法)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12
基于C語言實(shí)現(xiàn)學(xué)生選課系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了基于C語言實(shí)現(xiàn)學(xué)生選課系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
C++11中std::function基礎(chǔ)用法詳解
std::function是C++11標(biāo)準(zhǔn)庫中提供的一種可調(diào)用對(duì)象的通用類型,它可以存儲(chǔ)任意可調(diào)用對(duì)象,本文就來和大家講講它的基礎(chǔ)用法,希望對(duì)大家有所幫助2023-04-04

