วิธีเพิ่มแถวในตารางใน DOCX โดยใช้ C++

ในหัวข้อนี้ เราจะเรียนรู้วิธี เพิ่มแถวในตารางใน DOCX โดยใช้ C+* ตารางมียูทิลิตี้มากมายภายในเอกสาร Word และคุณสามารถแทรกแถวภายในตารางของเอกสาร Word ได้อย่างง่ายดายโดยใช้อินเทอร์เฟซ C++ API ที่ง่ายมาก

ขั้นตอนการแทรกแถวภายในตารางใน DOCX โดยใช้ C++

  1. ติดตั้ง Aspose.Words.Cpp โดยใช้ NuGet package Manager
  2. เพิ่มการอ้างอิงถึงเนมสเปซ Aspose::Words และ Aspose::Words::Tables
  3. สร้างอินสแตนซ์ Document Class วัตถุเพื่อโหลดเอกสารสำหรับจัดการแถวของตาราง
  4. เข้าถึงตารางในส่วนแรกโดยใช้วัตถุ Table Class
  5. เพิ่มแถวในตารางโดยใช้หลายตัวเลือก
  6. บันทึกเอกสาร Word พร้อมแถวตารางเป็นรูปแบบ DOCX โดยใช้วิธีบันทึก

ในตัวอย่างต่อไปนี้ เราได้แสดงวิธี แทรกแถวตั้งแต่เริ่มต้นภายในตารางของเอกสาร Word นอกจากนี้ยังอธิบายวิธีที่คุณสามารถโคลนแถวที่มีอยู่ภายในตารางและเพิ่มแถวใหม่โดยใช้แถวที่โคลนภายในตาราง DOCX

รหัสเพื่อแทรกแถวภายในตารางใน DOCX โดยใช้ 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");
}
};

ในตัวอย่างก่อนหน้านี้ เราดูที่ วิธีแทรกส่วนหัวและส่วนท้ายใน DOCX โดยใช้ C ++ ในหัวข้อนี้ เราได้เรียนรู้เกี่ยวกับ *การเพิ่มแถวในตารางของเอกสาร Word โดยใช้ C++

 ไทย