Neste tópico, veremos como criar uma tabela em DOCX usando C++. As tabelas são comumente usadas dentro de documentos do Word e você pode inserir tabelas no DOCX usando C++. Você pode inserir a tabela do Word em C++ usando chamadas de API simples.
Etapas para adicionar tabela no DOCX usando C++
- Instale a versão mais recente do Aspose.Words.Cpp do NuGet
- Incluir referência ao namespace Aspose::Words
- Crie o objeto Document Class para carregar o DOCX para adicionar a tabela
- Instancie DocumentBuilder Class para gerenciar o conteúdo dentro do DOCX
- Use o DocumentBuilder para adicionar linhas, células e imagem dentro da célula
- Salve o DOCX com Tabela em C++ usando o método Save
No exemplo abaixo, você pode criar uma tabela do Word usando C++ sem dependência do Microsoft Word. Você pode formatar o conteúdo dentro das células da tabela e mostramos como adicionar uma imagem dentro da célula também.
Código para adicionar tabela no DOCX usando 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"); | |
} | |
}; |
Anteriormente, vimos Como adicionar imagem no DOCX usando C++. No entanto, neste tópico aprendemos como inserir tabela em DOCX usando C++.