# วิธีการเพิ่มแถวในตารางในเอกสาร MS Word โดยใช้ C#
ในบทเรียนนี้คุณจะเรียนรู้วิธีการเพิ่มแถวในตารางในเอกสาร MS Word โดยใช้ C# คุณจะเพิ่มแถวในดัชนีที่ระบุและใส่แถวว่างหลายในตอนท้ายของตาราง
ประโยชน์ของการเพิ่มเส้นไปยังตารางในเอกสาร Word
การจัดการเนื้อหาแบบไดนามิก:- เปลี่ยนแท็บที่มีอยู่ได้อย่างง่ายดายเพื่อปรับตัวข้อมูลใหม่
ความยืดหยุ่น:- การปรับโครงสร้างตารางโดยการเขียนโปรแกรมโดยไม่มีการแก้ไขด้วยตนเอง
อัตโนมัต:- การจัดการข้อมูลตารางได้อย่างมีประสิทธิภาพผ่าน scripts อัตโนมัติ
ข้อกําหนด: การเตรียมสิ่งแวดล้อม
- ติดตั้ง Visual Studio หรือ .NET IDE ใด ๆ
- ตรวจสอบให้แน่ใจว่าห้องสมุด Aspose.Words สามารถใช้ได้ผ่าน NuGet
คู่มือขั้นตอนเพื่อเพิ่มเส้นไปยังโต๊ะใน Word
ขั้นตอน 1: ติดตั้งห้องสมุด Aspose.Words
ติดตั้งแพคเกจ Aspose.Words โดยใช้ผู้จัดการแพคเกจ NuGet
Install-Package Aspose.Words
ขั้นตอนที่ 2: นําเข้าพื้นที่ชื่อที่จําเป็น
รวมพื้นที่ชื่อ Aspose.Words และ Aspose.Words.Tables ในโครงการของคุณ
using Aspose.Words;
using Aspose.Words.Tables;
ขั้นตอนที่ 3: เปิดเอกสาร Word
ดาวน์โหลดเอกสาร MS Word ที่มีอยู่
Document MSWordDocument = new Document(@"MS Word.docx");
ขั้นตอนที่ 4: เข้าถึงโต๊ะ
รับตารางโดย index ของมันจากเอกสาร
Table tableToAddRowsTo = MSWordDocument.FirstSection.Body.Tables[0];
ขั้นตอน 5: สร้างหรือ Clone a Row
สร้างร่องใหม่หรือคลอนหนึ่งที่มีอยู่จากโต๊ะ
Row row = new Row(MSWordDocument);
ขั้นตอน 6: เพิ่มเซลล์ไปยังเส้น
เพิ่มเซลล์และข้อความไปยังแถว
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);
}
ขั้นตอน 7: เพิ่มเส้นทางไปยังจุดสิ้นสุด
ใช้ RowCollection. Add เพื่อเพิ่ม Rows ไปยังโต๊ะ
tableToAddRowsTo.Rows.Add(row);
ขั้นตอน 8: ใส่เส้นด้ายในดัชนีเฉพาะ
ใส่เส้นผ่านศูนย์กลางในดัชนีเฉพาะโดยใช้ RowCollection.Insert
tableToAddRowsTo.Rows.Insert(1, row);
ขั้นตอนที่ 9: เก็บเอกสารที่อัปเดต
กลับคืนเอกสารด้วยแถวที่เพิ่ม
MSWordDocument.Save(@"Added Rows to Table in MS Word.docx");
ตัวอย่างรหัสเพื่อเพิ่มเส้นไปยังโต๊ะใน Word
ต่อไปนี้เป็นรหัสสมบูรณ์สําหรับการเพิ่มเส้นไปยังตาราง:
// Open MS Word Document
Document MSWordDocument = new Document(@"input.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");
ข้อสรุป
การสอนนี้ได้แสดงให้เห็นว่าคุณสามารถเพิ่มเส้นไปยังตารางในเอกสาร MS Word โดยใช้ C# โดยการปฏิบัติตามขั้นตอนเหล่านี้คุณสามารถจัดการและแก้ไขตารางภายในเอกสาร Word ได้อย่างมีประสิทธิภาพ