Cara Menambahkan Baris ke Tabel di DOCX menggunakan C++

Dalam topik ini, kita akan mempelajari cara menambahkan Baris ke Tabel di DOCX menggunakan C++. Tabel memiliki utilitas yang luas di dalam dokumen Word dan Anda dapat dengan mudah menyisipkan baris di dalam tabel dokumen Word dengan menggunakan antarmuka C++ API yang sangat sederhana.

Langkah-langkah Menyisipkan Baris di dalam Tabel di DOCX menggunakan C++

  1. Instal Aspose.Words.Cpp menggunakan Pengelola paket NuGet
  2. Tambahkan referensi ke Aspose::Words dan Aspose::Words::Tables namespaces
  3. Buat instance objek Document Class untuk memuat dokumen untuk mengelola Baris Tabel
  4. Akses Tabel di bagian pertama dengan menggunakan objek Table Class
  5. Tambahkan baris di Tabel menggunakan beberapa opsi
  6. Simpan dokumen Word dengan baris tabel ke format DOCX menggunakan metode Simpan

Dalam contoh berikut yang telah kami tunjukkan, bagaimana Anda bisa menyisipkan baris dari awal di dalam tabel dokumen Word. Ini juga menjelaskan, bagaimana Anda dapat mengkloning baris yang ada di dalam tabel dan menambahkan yang baru menggunakan baris yang dikloning di dalam tabel DOCX.

Kode untuk Menyisipkan Baris di dalam Tabel di DOCX menggunakan C++

#pragma once
#include <cstdint>
#include <iostream>
#include <Aspose.Words.Cpp/Document.h>
#include <Aspose.Words.Cpp/Section.h>
#include <Aspose.Words.Cpp/Body.h>
#include <Aspose.Words.Cpp/Paragraph.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/Tables/Cell.h>
#include <Aspose.Words.Cpp/Tables/CellCollection.h>
#include <Aspose.Words.Cpp/Tables/CellFormat.h>
#include <Aspose.Words.Cpp/Tables/PreferredWidth.h>
#include <Aspose.Words.Cpp/Tables/Row.h>
#include <Aspose.Words.Cpp/Tables/RowCollection.h>
#include <Aspose.Words.Cpp/Tables/Table.h>
#include <Aspose.Words.Cpp/Tables/TableCollection.h>
//#include <Aspose.Words.Cpp/Tables/Tables.h>s
#include <Aspose.Words.Cpp/Run.h>
#include <Aspose.Words.Cpp/RunCollection.h>
#include <system/enumerator_adapter.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;
using namespace Aspose::Words::Tables;
class AddTableInWordDocumentUsingCpp
{
public:
void AddTableRows()
{
// Load and Set API License
System::String LicFilePath = u"Aspose.Total.CPP.lic";
SharedPtr<License> WordsCPPLicenseForTable = System::MakeObject<License>();
// Setting product license
WordsCPPLicenseForTable->SetLicense(LicFilePath);
// Instantiate Document class to load document for managing table
SharedPtr<Document> AddTableToDOCX = MakeObject<Document>(u"WordDocument.docx");
// Access the Table by index in first section
SharedPtr <Table> tableToAddRowsTo = AddTableToDOCX->get_FirstSection()->get_Body()
->get_Tables()->idx_get(0);
// Instantiate a new Row Object
SharedPtr <Row> row = MakeObject<Row>(AddTableToDOCX);
// Add 3 Cells for added Row by accessing it cells collection
for (int iRowIndex = 0; iRowIndex < 3; iRowIndex++)
{
SharedPtr < Cell> cell = MakeObject <Cell>(AddTableToDOCX);
cell->AppendChild(MakeObject <Paragraph>(AddTableToDOCX));
cell->get_FirstParagraph()->get_Runs()->Add(MakeObject <Run>(AddTableToDOCX, u"Text in Cell " + iRowIndex));
row->get_Cells()->Add(cell);
}
// Insert the Row after first row in table
tableToAddRowsTo->get_Rows()->Insert(1, row);
// Now, clone the existing Row in Table
SharedPtr <Row> cloneOfRow = System::DynamicCast<Row>(tableToAddRowsTo->get_FirstRow()->Clone(true));
// Clear all content from all Cells of the row
for(SharedPtr<Node> node : cloneOfRow->get_Cells())
{
SharedPtr<Cell> cell = System::DynamicCast<Cell>(node);
cell->RemoveAllChildren();
cell->EnsureMinimum();
}
// Now see, how to add multiple empty rows at the end of table
for (int iRowCounter = 0; iRowCounter < 10; iRowCounter++)
{
SharedPtr<Row> emptyRow = System::DynamicCast<Row>(cloneOfRow->Clone(true));
tableToAddRowsTo->get_Rows()->Add(emptyRow);
}
// Save the word document with table to DOCX format
AddTableToDOCX->Save(u"InsertTableinDocx.docx");
}
};

Pada contoh sebelumnya, kita melihat ke Cara Menyisipkan Header dan Footer di DOCX menggunakan C++. Sedangkan pada topik ini, kita telah mempelajari tentang menambahkan baris ke tabel dokumen Word menggunakan C++.

 Indonesian