วิธีเพิ่มแถวในตารางของเอกสาร MS Word ด้วย C#

วิธีเพิ่มแถวในตารางของเอกสาร MS Word ด้วย C#

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

ประโยชน์ของการเพิ่มแถวในตารางของเอกสาร Word

  1. การจัดการเนื้อหาแบบไดนามิก: - ปรับเปลี่ยนตารางที่มีอยู่ให้เข้ากับข้อมูลใหม่ได้อย่างง่ายดาย.
  2. ความยืดหยุ่น: - ปรับโครงสร้างตารางโดยโปรแกรมโดยไม่ต้องแก้ไขด้วยมือ.
  3. การทำงานอัตโนมัติ: - จัดการข้อมูลตารางอย่างมีประสิทธิภาพผ่านสคริปต์อัตโนมัติ.

ข้อกำหนดเบื้องต้น: การเตรียมสภาพแวดล้อม

  1. ให้ติดตั้ง Visual Studio หรือ IDE .NET ใด ๆ.
  2. ตรวจสอบให้แน่ใจว่าไลบรารี Aspose.Words มีให้ใช้งานผ่าน NuGet.

คู่มือขั้นตอนต่อขั้นเพื่อเพิ่มแถวในตารางใน Word

ขั้นตอนที่ 1: ติดตั้งไลบรารี Aspose.Words

ติดตั้งแพ็กเกจ Aspose.Words โดยใช้ NuGet package manager.

Install-Package Aspose.Words

ขั้นตอน 2: นำเข้า Namespaces ที่จำเป็น

รวม Aspose.Words และ Aspose.Words.Tables namespaces ในโครงการของคุณ.

using Aspose.Words;
using Aspose.Words.Tables;

ขั้นตอน 3: เปิดเอกสาร Word

โหลดเอกสาร MS Word ที่มีอยู่.

Document MSWordDocument = new Document(@"MS Word.docx");

ขั้นตอน 4: เข้าถึง Table

ดึง Table ตามดัชนีจากเอกสาร.

Table tableToAddRowsTo = MSWordDocument.FirstSection.Body.Tables[0];

ขั้นตอน 5: สร้างหรือคัดลอก Row

สร้าง Row ใหม่หรือคัดลอก Row ที่มีอยู่จาก Table.

Row row = new Row(MSWordDocument);

ขั้นตอนที่ 6: เพิ่มเซลล์ลงในแถว

เพิ่มเซลล์และข้อความลงในแถว.

using Aspose.Words;
using Aspose.Words.Tables;

// Open MS Word Document
Document MSWordDocument = new Document(@"BigDocument.docx");

// Get the Table by index
Table tableToAddRowsTo = MSWordDocument.FirstSection.Body.Tables[0];

// Create a new Row class object
Row row = new Row(MSWordDocument);

// Add three Cells to Row's cells collection
for (int i = 0; i < 3; i++)
{
    Cell cell = new Cell(MSWordDocument);
    cell.AppendChild(new Paragraph(MSWordDocument));
    cell.FirstParagraph.Runs.Add(new Run(MSWordDocument, "Text in Cell " + i));
    row.Cells.Add(cell);
}

// Insert new Row after the first Row
tableToAddRowsTo.Rows.Insert(1, row);

// Clone an existing Row from Table
Row cloneOfRow = (Row)tableToAddRowsTo.FirstRow.Clone(true);

// Remove all content from all Cells
foreach (Cell cell in cloneOfRow)
{
    cell.RemoveAllChildren();
    cell.EnsureMinimum();
}

// Add multiple empty rows to the end of table
for (int i = 0; i < 10; i++)
{
    Row emptyRow = (Row)cloneOfRow.Clone(true);
    tableToAddRowsTo.Rows.Add(emptyRow);
}

// Save updated document
MSWordDocument.Save(@"output.docx");

ขั้นตอนที่ 7: เพิ่มแถวลงท้าย

ใช้ RowCollection.Add เพื่อเพิ่ม Rows ลงใน Table.

tableToAddRowsTo.Rows.Add(row);

ขั้นตอนที่ 8: แทรก Rows ที่ตำแหน่งเฉพาะ

แทรก Rows ที่ตำแหน่งเฉพาะโดยใช้ RowCollection.Insert.

tableToAddRowsTo.Rows.Insert(1, row);

ขั้นตอนที่ 9: บันทึกเอกสารที่อัปเดต

บันทึกเอกสารใหม่พร้อมแถวที่เพิ่มเข้าไป.

MSWordDocument.Save(@"Added Rows to Table in MS Word.docx");

ตัวอย่างโค้ดเพื่อเพิ่ม Rows ลงใน Table ใน Word

ด้านล่างเป็นโค้ดเต็มสำหรับการเพิ่ม rows ลงใน table:

using Aspose.Words;
using Aspose.Words.Tables;

// Open MS Word Document
Document MSWordDocument = new Document(@"BigDocument.docx");

// Get the Table by index
Table tableToAddRowsTo = MSWordDocument.FirstSection.Body.Tables[0];

// Create a new Row class object
Row row = new Row(MSWordDocument);

// Add three Cells to Row's cells collection
for (int i = 0; i < 3; i++)
{
    Cell cell = new Cell(MSWordDocument);
    cell.AppendChild(new Paragraph(MSWordDocument));
    cell.FirstParagraph.Runs.Add(new Run(MSWordDocument, "Text in Cell " + i));
    row.Cells.Add(cell);
}

// Insert new Row after the first Row
tableToAddRowsTo.Rows.Insert(1, row);

// Clone an existing Row from Table
Row cloneOfRow = (Row)tableToAddRowsTo.FirstRow.Clone(true);

// Remove all content from all Cells
foreach (Cell cell in cloneOfRow)
{
    cell.RemoveAllChildren();
    cell.EnsureMinimum();
}

// Add multiple empty rows to the end of table
for (int i = 0; i < 10; i++)
{
    Row emptyRow = (Row)cloneOfRow.Clone(true);
    tableToAddRowsTo.Rows.Add(emptyRow);
}

// Save updated document
MSWordDocument.Save(@"output.docx");

สรุป

บทแนะนำนี้ได้สาธิตวิธีการเพิ่ม rows ลงใน table ในเอกสาร MS Word ด้วย C#. โดยการทำตามขั้นตอนเหล่านี้ คุณสามารถจัดการและแก้ไข tables ภายในเอกสาร Word ได้อย่างมีประสิทธิภาพโดยอัตโนมัติ.

 แบบไทย