ทำงานกับข้อความในตาราง

ตามที่กล่าวไว้ในบทความก่อนหน้านี้ ตารางมักจะมีข้อความธรรมดา แม้ว่าเนื้อหาอื่นๆ เช่น รูปภาพ หรือแม้แต่ตารางอื่นๆ ก็สามารถวางในเซลล์ตารางได้

การเพิ่มข้อความหรือเนื้อหาอื่นๆ ลงในตารางทำได้โดยใช้วิธีการที่เหมาะสมของคลาส DocumentBuilder และอธิบายไว้ในบทความ “สร้างตาราง” ในบทความนี้ เราจะพูดถึงวิธีทำงานกับข้อความในตารางที่มีอยู่แล้ว

แทนที่ข้อความในตาราง

ตารางก็เหมือนกับโหนดอื่นๆ ใน Aspose.Words ที่สามารถเข้าถึงออบเจ็กต์ Range เมื่อใช้วัตถุช่วงตาราง คุณสามารถแทนที่ข้อความในตารางได้

ขณะนี้รองรับความสามารถในการใช้อักขระพิเศษเมื่อแทนที่ ดังนั้นจึงเป็นไปได้ที่จะแทนที่ข้อความที่มีอยู่ด้วยข้อความหลายย่อหน้า ในการดำเนินการนี้ คุณจะต้องใช้อักขระเมตาพิเศษที่อธิบายไว้ในวิธี Replace ที่เกี่ยวข้อง

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทนที่อินสแตนซ์ทั้งหมดของสตริงข้อความในเซลล์ของทั้งตาราง:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Tables.docx");
Table table = (Table)doc.GetChild(NodeType.Table, 0, true);
table.Range.Replace("Carrots", "Eggs", new FindReplaceOptions(FindReplaceDirection.Forward));
table.LastRow.LastCell.Range.Replace("50", "20", new FindReplaceOptions(FindReplaceDirection.Forward));
doc.Save(ArtifactsDir + "FindAndReplace.ReplaceTextInTable.docx");
view raw replace-text.cs hosted with ❤ by GitHub

แยกข้อความธรรมดาจากตารางหรือเซลล์

เมื่อใช้ออบเจ็กต์ Range คุณยังสามารถเรียกใช้เมธอดบนช่วงตารางทั้งหมดและแยกตารางเป็นข้อความธรรมดาได้ เมื่อต้องการทำเช่นนี้ ให้ใช้คุณสมบัติ Text

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการพิมพ์ช่วงข้อความของตาราง:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Tables.docx");
Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
// The range text will include control characters such as "\a" for a cell.
// You can call ToString and pass SaveFormat.Text on the desired node to find the plain text content.
Console.WriteLine("Contents of the table: ");
Console.WriteLine(table.Range.Text);
view raw extract-text.cs hosted with ❤ by GitHub

เทคนิคเดียวกันนี้ใช้เพื่อแยกเนื้อหาจากเซลล์ตารางแต่ละเซลล์เท่านั้น

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการพิมพ์ช่วงข้อความขององค์ประกอบแถวและตาราง:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Console.WriteLine("\nContents of the row: ");
Console.WriteLine(table.Rows[1].Range.Text);
Console.WriteLine("\nContents of the cell: ");
Console.WriteLine(table.LastRow.LastCell.Range.Text);

การทำงานกับข้อความตารางทางเลือก

ตาราง Microsoft Word มี table title และ table description ที่ให้ทางเลือกในการแสดงข้อความที่มีอยู่ในตาราง

ใน Aspose.Words คุณยังเพิ่มชื่อตารางและคำอธิบายโดยใช้คุณสมบัติ Title และ Description ได้ด้วย คุณสมบัติเหล่านี้มีความหมายสำหรับเอกสาร DOCX ที่สอดคล้องกับ ISO/IEC 29500 เมื่อบันทึกในรูปแบบที่เก่ากว่า ISO/IEC 29500 คุณสมบัติเหล่านี้จะถูกละเว้น

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่าชื่อเรื่องและคำอธิบายคุณสมบัติของตาราง:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Tables.docx");
Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
table.Title = "Test title";
table.Description = "Test description";
OoxmlSaveOptions options = new OoxmlSaveOptions { Compliance = OoxmlCompliance.Iso29500_2008_Strict };
doc.CompatibilityOptions.OptimizeFor(Aspose.Words.Settings.MsWordVersion.Word2016);
doc.Save(ArtifactsDir + "WorkingWithTableStylesAndFormatting.TableTitleAndDescription.docx", options);