C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C++创建列表

详解C++如何实现在Word文档中创建列表

作者:E-iceblue

这篇文章主要为大家详细介绍了介绍如何使用C++在Word文档中创建编号列表、项目符号列表和多级列表,感兴趣的小伙伴可以跟随小编一起学习一下

列表分类是指在Word文档中使用不同格式排序的列表,来帮助我们一目了然地表达出一段文字的主要内容。比如,当我们描述了某个主题的若干点,就可以用列表把它们一一表达出来,而不是写成完整的段落形式。同时,列表也可以帮助我们做出精确的计算和比较,简洁有效地表示出不同部分之间的关系。在Word文档中创建列表可以便于人们去检索资料方便定位,其中总共有四种不同类型的列表:编号列表、项目符号列表、多级编号列表和多级混合类型列表。本文就将详细为您介绍如何使用C++在Word文档中创建编号列表、项目符号列表和多级列表

安装 Spire.Doc for C++

有两种方法可以将 Spire.Doc for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。

如何将 Spire.Doc for C++ 集成到 C++ 程序中

在Word中创建编号列表

您可以使用ListStyle类创建编号列表样式或项目符号样式。然后,可以使用Paragraph->GetListFormat()->ApplyStyle() 方法将列表样式应用于段落。创建编号列表的步骤如下。

完整代码

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中创建项目符号列表

创建项目符号列表的过程与创建编号列表的过程类似。不同之处在于,创建列表样式时,必须将列表类型指定为“项目符号”,并为其设置项目符号。以下是详细步骤。

完整代码

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中创建多级编号列表的步骤。

完整代码

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中创建多级混合类型列表

多级列表可以是编号列表和项目符号列表的组合。要创建混合类型列表,只需要创建编号列表样式和项目符号列表样式,并将它们应用于不同的段落。具体步骤如下。

完整代码

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();
}

效果图

以上就是详解C++如何实现在Word文档中创建列表的详细内容,更多关于C++创建列表的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文