W tym temacie zobaczymy jak stworzyć tabelę w DOCX używając C++. Tabele są powszechnie używane w dokumentach Word i można wstawiać tabele w DOCX za pomocą C++. Możesz wstawić tabelę Worda w C++ za pomocą prostych wywołań API.
Kroki, aby dodać tabelę w DOCX przy użyciu C++
- Zainstaluj najnowszą wersję Aspose.Words.Cpp z NuGet
- Dołącz odwołanie do przestrzeni nazw Aspose::Words
- Utwórz obiekt Document Class, aby załadować DOCX w celu dodania tabeli
- Utwórz instancję DocumentBuilder Class, aby zarządzać treścią w DOCX
- Użyj DocumentBuilder, aby dodać wiersze, komórki i obraz wewnątrz komórki
- Zapisz DOCX z tabelą w C++, używając metody Save
W poniższym przykładzie możesz utworzyć tabelę Worda przy użyciu C++ bez zależności od Microsoft Word. Możesz formatować zawartość w komórkach tabeli, a my pokazaliśmy również, jak dodać obraz w komórce.
Kod do dodania tabeli w DOCX przy użyciu C++
#pragma once | |
#include <cstdint> | |
#include <iostream> | |
#include <Aspose.Words.Cpp/Document.h> | |
#include <Aspose.Words.Cpp/DocumentBase.h> | |
#include <Aspose.Words.Cpp/DocumentBuilder.h> | |
#include <Aspose.Words.Cpp/Drawing/Shape.h> | |
#include <Aspose.Words.Cpp/HeaderFooterType.h> | |
#include <Aspose.Words.Cpp/Saving/SaveOutputParameters.h> | |
#include <system/array.h> | |
#include <system/exceptions.h> | |
#include <Aspose.Words.Cpp/License.h> | |
#include <system/io/path.h> | |
using System::ArrayPtr; | |
using System::MakeArray; | |
using System::MakeObject; | |
using System::SharedPtr; | |
using System::String; | |
using namespace Aspose::Words; | |
class CreateTableInWordDocumentUsingCpp | |
{ | |
public: | |
void AddTableinWordDocument() | |
{ | |
// Set License file name | |
System::String LicenseFile = u"Aspose.Total.NET.lic"; | |
// Setting the Aspose.Words before creating Word document | |
SharedPtr<License> wordsLicenseForTable = System::MakeObject<License>(); | |
// Setting product license | |
wordsLicenseForTable->SetLicense(LicenseFile); | |
// Instantiate Document class to load DOCX and add table | |
SharedPtr<Document> AddTableToWordDOC = MakeObject<Document>(u"WordDocument.docx"); | |
// Instantiate DocumentBuilder class to manage document content | |
SharedPtr<DocumentBuilder> TableWriter = MakeObject<DocumentBuilder>(AddTableToWordDOC); | |
// Mark the start of table | |
TableWriter->StartTable(); | |
// Insert Row and first Cell inside Word Table | |
TableWriter->InsertCell(); | |
// Add some text in Table Cell | |
TableWriter->Write(u"Table Row 1 and Cell 1"); | |
// Add a new Cell inside Row | |
TableWriter->InsertCell(); | |
// Insert an Image in Word Table Cell | |
TableWriter->InsertImage(u"image in table.jpg"); | |
// Mark end of Table Row | |
TableWriter->EndRow(); | |
// Mark end of Word Table creation | |
TableWriter->EndTable(); | |
// Save the word document with table to docx format | |
AddTableToWordDOC->Save(u"InsertTableinDocx.docx"); | |
} | |
}; |
Wcześniej widzieliśmy Jak dodać obraz w DOCX za pomocą C++. Jednak w tym temacie dowiedzieliśmy się, jak wstawić tabelę w DOCX przy użyciu C++.