이 주제에서는 C++를 사용하여 DOCX에서 테이블을 생성하는 방법을 알아봅니다. 테이블은 일반적으로 워드 문서 내에서 사용되며 C++를 사용하여 DOCX에 테이블을 삽입할 수 있습니다. 간단한 API 호출을 사용하여 C++에서 Word 테이블을 삽입할 수 있습니다.
C++를 사용하여 DOCX에 테이블을 추가하는 단계
- NuGet에서 최신 버전의 Aspose.Words.Cpp 설치
- Aspose::Words 네임스페이스에 대한 참조 포함
- 테이블 추가를 위한 DOCX 로드를 위한 Document Class 객체 생성
- DocumentBuilder Class을(를) 인스턴스화하여 DOCX 내 콘텐츠 관리
- DocumentBuilder를 사용하여 셀 내부에 행, 셀 및 이미지 추가
- Save 메서드를 사용하여 C++에서 테이블과 함께 DOCX 저장
아래 예에서는 Microsoft Word에 의존하지 않고 C++를 사용하여 Word 테이블을 생성할 수 있습니다. 표 셀 안의 내용에 서식을 지정할 수 있으며 셀 안에 이미지를 추가하는 방법도 보여주었습니다.
C++를 사용하여 DOCX에 테이블을 추가하는 코드
#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"); | |
} | |
}; |
이전에 C++를 사용하여 DOCX에 이미지를 추가하는 방법을(를) 보았습니다. 그러나 이 항목에서는 C++를 사용하여 DOCX에 테이블을 삽입하는 방법을 배웠습니다.