When creating or editing Excel files, users may need to manipulate worksheets to make the files fit their specific needs. For instance, they might need to add new worksheets to record data for different categories, move or rearrange worksheets to ensure that the data is displayed in a logical order, or delete worksheets containing unnecessary information or errors. In this article, we will explain how to add, move or delete worksheets in Excel in C++ using Spire.XLS for C++.

Install Spire.XLS for C++

There are two ways to integrate Spire.XLS for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.

Integrate Spire.XLS for C++ in a C++ Application

Add a Worksheet to an Excel File in C++

You can use the Workbook->GetWorksheets()->Add(LPCWSTR_S name) method to add a new worksheet to an Excel file. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using the Workbook->LoadFromFile(LPCWSTR_S fileName) method.
  • Add a new worksheet with a specific name to the Excel file using the Workbook->GetWorksheets()->Add(LPCWSTR_S name) method.
  • Add text to a specific cell of the worksheet using the Worksheet->GetCellRange(LPCWSTR_S name)->SetText(LPCWSTR_S value) method.
  • Save the result file to a specific location using the Workbook->SaveToFile(LPCWSTR_S fileName, ExcelVersion version) method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main()
{
	//Initialize an instance of the Workbook class
	intrusive_ptr<Workbook> workbook = new Workbook();
	//Load an Excel file
	workbook->LoadFromFile(L"Sample.xlsx");

	//Add a new worksheet with a specific name to the file
	intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Add(L"New Sheet"));
	//Add text to the worksheet
	dynamic_pointer_cast<CellRange>(sheet->GetRange(L"B1"))->SetText(L"This is a new sheet.");
	//Autofit the width of the second column
	sheet->AutoFitColumn(2);

	//Save the result file to a specific location
	workbook->SaveToFile(L"AddWorksheet.xlsx", ExcelVersion::Version2013);
	workbook->Dispose();
}

C++: Add, Move or Delete Worksheets in Excel

Move a Worksheet to Another Location in an Excel File in C++

Spire.XLS for C++ provides the XlsWorksheet->MoveWorksheet(int destIndex) method which allows you to move a worksheet to from one location to another in an Excel file with ease. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using the Workbook->LoadFromFile(LPCWSTR_S fileName) method.
  • Get the worksheet that you want to move using the Workbook->GetWorksheets()->Get(int index) method.
  • Move the worksheet to a specific position in the Excel file using the XlsWorksheet->MoveWorksheet(int destIndex) method.
  • Save the result file to a specific location using the Workbook->SaveToFile(LPCWSTR_S fileName, ExcelVersion version) method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main()
{
	//Initialize an instance of the Workbook class
	intrusive_ptr<Workbook> workbook = new Workbook();
	//Load an Excel file
	workbook->LoadFromFile(L"AddWorksheet.xlsx");

	//Get the second worksheet
	intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(1));

	//Move the worksheet to the first position in the Excel file
	sheet->MoveWorksheet(0);

	//Save the result file to a specific location
	workbook->SaveToFile(L"MoveWorksheet.xlsx", ExcelVersion::Version2013);
	workbook->Dispose();
}

C++: Add, Move or Delete Worksheets in Excel

Delete a Worksheet from an Excel File in C++

Deleting a worksheet from an Excel file is also very simple, you just need to call the Workbook->GetWorksheets()->RemoveAt(int index) method. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load an Excel file using the Workbook->LoadFromFile(LPCWSTR_S fileName) method.
  • Delete a specific worksheet from the Excel file by its index using the Workbook->GetWorksheets()->RemoveAt(int index) method.
  • Save the result file to a specific location using the Workbook->SaveToFile(LPCWSTR_S fileName, ExcelVersion version) method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main()
{
	//Initialize an instance of the Workbook class
	intrusive_ptr<Workbook> workbook = new Workbook();
	//Load an Excel file
	workbook->LoadFromFile(L"AddWorksheet.xlsx");

	//Delete the second worksheet from the Excel file using its sheet index
	workbook->GetWorksheets()->RemoveAt(1);

	//Save the result file to another location
	workbook->SaveToFile(L"DeleteWorksheet.xlsx", ExcelVersion::Version2013);
	workbook->Dispose();
}

C++: Add, Move or Delete Worksheets in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Data validation in Excel controls what types of information can be entered into a cell. Using it, you can restrict only specific data types such as numbers or dates to be in a cell, or limit numbers to a certain range and text to a certain length. In addition, it also allows you to present a list of predefined values in a drop-down menu for users to choose from. In this article, you will learn how to apply or remove data validation in Excel in C++ using Spire.XLS for C++.

Install Spire.XLS for C++

There are two ways to integrate Spire.XLS for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.

Integrate Spire.XLS for C++ in a C++ Application

Apply Data Validation to Excel Cells in C++

Spire.XLS for C++ allows you to create validation rules for numbers, dates, text values, lists, etc. The following are the steps to apply different data validation types to specified cells in Excel.

  • Create a Workbook object.
  • Get a specified worksheet using Workbook->GetWorksheets()->Get() method.
  • Get a specific cell using Worksheet->GetRange() method.
  • Set the data type allowed in the cell using CellRange->GetDataValidation()->SetAllowType() method. You can select different data type such as Decimal, Time, Date, TextLength and Integer.
  • Set the comparison operator using CellRange->GetDataValidation()->SetCompareOperator() method. The comparison operators include Between, NotBetween, Less, Greater, and Equal.
  • Set one or two formulas for the data validation using CellRange->GetDataValidation()->SetFormula1() and CellRange->GetDataValidation()->SetFormula2() methods.
  • Set the input prompt using CellRange->GetDataValidation()->SetInputMessage() method.
  • Set the error message using CellRange->GetDataValidation()->SetErrorMessage() method.
  • Set to show the error alert and set its alert style when invalid data is entered.
  • Save the result document using Workbook->SaveToFile() method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main() {

	//Specify the output file
	std::wstring outputFile = L"DataValidation.xlsx";

	//Create a Workbook object
	intrusive_ptr<Workbook> workbook = new Workbook();

	//Get the first worksheet
	intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0));

	//Insert text in specified cells
	dynamic_pointer_cast<CellRange>(sheet->GetRange(L"B2"))->SetText(L"Number Validation: ");
	dynamic_pointer_cast<CellRange>(sheet->GetRange(L"B4"))->SetText(L"Date Validation: ");
	dynamic_pointer_cast<CellRange>(sheet->GetRange(L"B6"))->SetText(L"Text Length Validation: ");
	dynamic_pointer_cast<CellRange>(sheet->GetRange(L"B8"))->SetText(L"List Validation: ");
	dynamic_pointer_cast<CellRange>(sheet->GetRange(L"B10"))->SetText(L"Time Validation: ");

	//Add a number validation to C2
	intrusive_ptr<CellRange> rangeNumber = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"C2"));
	rangeNumber->GetDataValidation()->SetAllowType(CellDataType::Decimal);
	rangeNumber->GetDataValidation()->SetCompareOperator(ValidationComparisonOperator::Between);
	rangeNumber->GetDataValidation()->SetFormula1(L"3");
	rangeNumber->GetDataValidation()->SetFormula2(L"6");
	rangeNumber->GetDataValidation()->SetInputMessage(L"Enter a number between 1 and 10");
	rangeNumber->GetDataValidation()->SetErrorMessage(L"Please input correct number!");
	rangeNumber->GetDataValidation()->SetShowError(true);
	rangeNumber->GetDataValidation()->SetAlertStyle(AlertStyleType::Warning);
	rangeNumber->GetStyle()->SetKnownColor(ExcelColors::Gray25Percent);

	//Add a date validation to C4
	intrusive_ptr<CellRange> rangeDate = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"C4"));
	rangeDate->GetDataValidation()->SetAllowType(CellDataType::Date);
	rangeDate->GetDataValidation()->SetCompareOperator(ValidationComparisonOperator::Between);
	rangeDate->GetDataValidation()->SetFormula1(L"1/1/2021");
	rangeDate->GetDataValidation()->SetFormula2(L"12/31/2021");
	rangeDate->GetDataValidation()->SetInputMessage(L"Enter a date between 1/1/2021 and 12/31/2021");
	rangeDate->GetStyle()->SetKnownColor(ExcelColors::Gray25Percent);

	//Add a text length validation to C6
	intrusive_ptr<CellRange> rangeTextLength = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"C6"));
	rangeTextLength->GetDataValidation()->SetAllowType(CellDataType::TextLength);
	rangeTextLength->GetDataValidation()->SetCompareOperator(ValidationComparisonOperator::LessOrEqual);
	rangeTextLength->GetDataValidation()->SetFormula1(L"5");
	rangeTextLength->GetDataValidation()->SetErrorMessage(L"Enter a Valid String!");
	rangeTextLength->GetDataValidation()->SetShowError(true);
	rangeTextLength->GetDataValidation()->SetAlertStyle(AlertStyleType::Stop);
	rangeTextLength->GetStyle()->SetKnownColor(ExcelColors::Gray25Percent);

	//Apply a list validation to C8
	intrusive_ptr<CellRange> rangeList = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"C8"));
	std::vector<LPCWSTR_S> files = { L"United States", L"Canada", L"United Kingdom" };
	rangeList->GetDataValidation()->SetValues(files);
	rangeList->GetDataValidation()->SetIsSuppressDropDownArrow(false);
	rangeList->GetDataValidation()->SetInputMessage(L"Choose an item from the list");
	rangeList->GetStyle()->SetKnownColor(ExcelColors::Gray25Percent);

	//Apply a time validation to C10
	intrusive_ptr<CellRange> rangeTime = dynamic_pointer_cast<CellRange>(sheet->GetRange(L"C10"));
	rangeTime->GetDataValidation()->SetAllowType(CellDataType::Time);
	rangeTime->GetDataValidation()->SetCompareOperator(ValidationComparisonOperator::Between);
	rangeTime->GetDataValidation()->SetFormula1(L"9:00");
	rangeTime->GetDataValidation()->SetFormula2(L"12:00");
	rangeTime->GetDataValidation()->SetInputMessage(L"Enter a time between 9:00 and 12:00");
	rangeTime->GetStyle()->SetKnownColor(ExcelColors::Gray25Percent);

	//Auto fit width of column 2
	sheet->AutoFitColumn(2);

	//Set the width of column 3
	sheet->GetColumns()->GetItem(2)->SetColumnWidth(20);

	//Save the result document
	workbook->SaveToFile(outputFile.c_str(), ExcelVersion::Version2016);
	workbook->Dispose();
}

C++: Apply or Remove Data Validation in Excel

Remove Data Validation from Excel Cells in C++

To remove data validation applied to the cells, Spire.XLS for C++ provides the Worksheet->GetDVTable()->Remove() method. The following are the detailed steps.

  • Create a Workbook object.
  • Load a sample Excel document containing data validation using Workbook->LoadFromFile() method.
  • Get a specified worksheet using Workbook->GetWorksheets()->Get() method.
  • Create an array of rectangles, which is used to locate the cells where the validation will be removed.
  • Remove the data validation from the selected cells using Worksheet->GetDVTable()->Remove() method.
  • Save the result document using Workbook->SaveToFile() method.
  • C++
#include "Spire.Xls.o.h";

using namespace Spire::Xls;

int main() {

	//Specify the input and output files
	std::wstring inputFile = L"DataValidation.xlsx";
	std::wstring outputFile = L"RemoveDataValidation.xlsx";

	//Create a Workbook object
	intrusive_ptr<Workbook> workbook = new Workbook();

	//Load a sample Excel document from disk
	workbook->LoadFromFile(inputFile.c_str());

	//Get the first worksheet
	intrusive_ptr<Worksheet> sheet = dynamic_pointer_cast<Worksheet>(workbook->GetWorksheets()->Get(0));

	//Create an array of rectangles, which is used to locate the ranges in worksheet
	std::vector<intrusive_ptr<Spire::Common::Rectangle>> rectangles(1);

	//Assign value to the first element of the array. A rectangle specifies a cell range
	rectangles[0] = Spire::Common::Rectangle::FromLTRB(0, 0, 2, 9);

	//Remove validations in the ranges represented by rectangles
	sheet->GetDVTable()->Remove(rectangles);

	//Save the result document
	workbook->SaveToFile(outputFile.c_str(), ExcelVersion::Version2016);
	workbook->Dispose();
}

C++: Apply or Remove Data Validation in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

C++: Create Lists in a Word Document

2023-05-19 01:01:41 Written by Koohji

If you want to make your paragraphs easier to navigate and read, rearranging them into alphabetical, numerical, or even bullet order will allow your readers to quickly find what they are looking for and search through the list in an instant. In this article, you will learn how to create numbered lists, bulleted lists, and multilevel lists in a Word document in C++ using Spire.Doc for C++.

Install Spire.Doc for C++

There are two ways to integrate Spire.Doc for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.

Integrate Spire.Doc for C++ in a C++ Application

Create a Numbered List in Word in C++

Spire.Doc for C++ offers the ListStyle class that you can use to create a numbered list style or a bulleted style. Then, the list style can be applied to a paragraph using Paragraph->GetListFormat()->ApplyStyle() method. The steps to create a numbered list are as follows.

  • Create a Document object.
  • Add a section using Document->AddSection() method.
  • Create an instance of ListStyle class, specifying the list type to Numbered.
  • Get a specific level of the list using ListStyle->GetLevels()->GetItem(index) method, and set the numbering type using ListLevel->SetPatternType() method.
  • Add the list style to the document using Document->GetListStyles()->Add() method.
  • Add several paragraphs to the document using Section->AddParagraph() method.
  • Apply the list style to a specific paragraph using Paragraph->GetListFormat()->ApplyStyle() method.
  • Specify the list level using Paragraph->GetListFormat()->GetListLevelNumber() method.
  • Save the document to a Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

    //Create a Document object
    intrusive_ptr<Document> document = new Document();

    //Add a section
    intrusive_ptr<Section> section = document->AddSection();

    //Create a numbered list style
    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);

    //Add a paragraph
    intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
    paragraph->AppendText(L"Required Web Development Skills:");
    paragraph->GetFormat()->SetAfterSpacing(5);

    //Add a paragraph and apply the numbered list style to it
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"HTML");
    paragraph->GetListFormat()->ApplyStyle(L"numberedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //Add another four paragraphs and apply the numbered list style to them
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"CSS");
    paragraph->GetListFormat()->ApplyStyle(L"numberedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"C++Script");
    paragraph->GetListFormat()->ApplyStyle(L"numberedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"Python");
    paragraph->GetListFormat()->ApplyStyle(L"numberedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"MySQL");
    paragraph->GetListFormat()->ApplyStyle(L"numberedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //Save the document to file
document->SaveToFile(L"output/NumberedList.docx", FileFormat::Docx2019);
document->Dispose();
}

C++: Create Lists in a Word Document

Create a Bulleted List in Word in C++

The process of creating a bulleted list is similar to that of creating a numbered list. The difference is that when creating a list style, you must specify the list type as Bulleted and set a bullet symbol for it. The following are the detailed steps.

  • Create a Document object.
  • Add a section using Document->AddSection() method.
  • Create an instance of ListStyle class, specifying the list type to Bulleted.
  • Get a specific level of the list using ListStyle->GetLevels()->Get(index) method, and set the bullet symbol using ListLevel->SetBulletCharacter() method.
  • Add the list style to the document using Document->GetListStyles()->Add() method.
  • Add several paragraphs to the document using Section->AddParagraph() method.
  • Apply the list style to a specific paragraph using Paragraph->GetListFormat()->ApplyStyle() method.
  • Specify the list level using Paragraph->GetListFormat()->SetListLevelNumber() method.
  • Save the document to a Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

    //Create a Document object
    intrusive_ptr<Document> document = new Document();

    //Add a section
    intrusive_ptr<Section> section = document->AddSection();

    //Create a bulleted list style
    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);

    //Add a paragraph
    intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
    paragraph->AppendText(L"Computer Science Subjects:");
    paragraph->GetFormat()->SetAfterSpacing(5);

    //Add a paragraph and apply the bulleted list style to it
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"Data Structure");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //Add another five paragraphs and apply the bulleted list style to them
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"Algorithm");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"Computer Networks");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"Operating System");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"C Programming");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"Theory of Computations");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedList");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //Save the document to file
document->SaveToFile(L"output/BulletedList.docx", FileFormat::Docx2019);
document->Dispose();
}

C++: Create Lists in a Word Document

Create a Multilevel Numbered List in Word in C++

A multilevel list consists of at least two different levels. Each level of a nested list can be accessed using ListStyle->GetLevels()->GetItem(index) method. Through ListLevel object, you can set the numbering type and prefix for a certain level. The following are the steps to create a multilevel numbered list in Word.

  • Create a Document object.
  • Add a section using Document->AddSection() method.
  • Create an instance of ListStyle class, specifying the list type to Numbered.
  • Get a specific level of the list using ListStyle->GetLevels()->GetItem(index) method, and set the numbering type and prefix.
  • Add the list style to the document using Document->GetListStyles()->Add() method.
  • Add several paragraphs to the document using Section->AddParagraph() method.
  • Apply the list style to a specific paragraph using Paragraph->GetListFormat()->ApplyStyle() method.
  • Specify the list level using Paragraph->GetListFormat()->SetListLevelNumber() method.
  • Save the document to a Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

    //Create a Document object
    intrusive_ptr<Document> document = new Document();

    //Add a section
    intrusive_ptr<Section> section = document->AddSection();

    //Create a numbered list style, specifying number prefix and pattern type of each level
    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);

    //Add a paragraph
    intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
    paragraph->AppendText(L"Here's a Multi-Level Numbered List:");
    paragraph->GetFormat()->SetAfterSpacing(5);

    //Add a paragraph and apply the numbered list style to it
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The first item");
    paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //Add another five paragraphs and apply the numbered list stype to them
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The second item");
    paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The first sub-item");
    paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(1);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The second sub-item");
    paragraph->GetListFormat()->ContinueListNumbering();
    paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"A sub-sub-item");
    paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(2);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The third item");
    paragraph->GetListFormat()->ApplyStyle(L"nestedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //Save the document to file
    document->SaveToFile(L"output/MultilevelNumberedList.docx", FileFormat::Docx2019);
    document->Dispose();
}

C++: Create Lists in a Word Document

Create a Multilevel Mixed-Type List in Word in C++

A multilevel list can be a combination of numbered lists and bulleted lists. To create a mixed-type list, you just need to create a numbered list style and a bulleted list style and apply them to different paragraphs. The detailed steps are as follows.

  • Create a Document object.
  • Add a section using Document->AddSection() method.
  • Create a numbered list style and a bulleted list style.
  • Add several paragraphs to the document using Section->AddParagraph() method.
  • Apply different list style to different paragraphs using Paragraph->GgetListFormat()->ApplyStyle() method.
  • Save the document to a Word file using Document->SaveToFile() method.
  • C++
#include "Spire.Doc.o.h";

using namespace Spire::Doc;
using namespace std;

int main() {

    //Create a Document object
    intrusive_ptr<Document> document = new Document();

    //Add a section
    intrusive_ptr<Section> section = document->AddSection();

    //Create a numbered list style
    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);

    //Create a bulleted list style
    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);

    //Add a paragraph
    intrusive_ptr<Paragraph> paragraph = section->AddParagraph();
    paragraph->AppendText(L"Here's a Multi-Level Mixed List:");
    paragraph->GetFormat()->SetAfterSpacing(5);

    //Add a paragraph and apply the numbered list style to it
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The first item");
    paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //Add another five paragraphs and apply different list stype to them
    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The first sub-item");
    paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(1);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The second sub-item");
    paragraph->GetListFormat()->SetListLevelNumber(1);
    paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The first sub-sub-item");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(2);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The second sub-sub-item");
    paragraph->GetListFormat()->ApplyStyle(L"bulletedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(2);

    paragraph = section->AddParagraph();
    paragraph->AppendText(L"The second item");
    paragraph->GetListFormat()->ApplyStyle(L"numberedStyle");
    paragraph->GetListFormat()->SetListLevelNumber(0);

    //Save the document to file
document->SaveToFile(L"output/MultilevelMixedList.docx", FileFormat::Docx);
document->Dispose();
}

C++: Create Lists in a Word Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

page 82

Coupon Code Copied!

Christmas Sale

Celebrate the season with exclusive savings

Save 10% Sitewide

Use Code:

View Campaign Details