แยกโต๊ะ
ตารางที่แสดงใน Aspose.Words Document Object Model ประกอบด้วยแถวและเซลล์ที่เป็นอิสระ ทำให้แยกตารางได้ง่าย
หากต้องการจัดการตารางให้แยกออกเป็นสองตาราง เราเพียงแค่ต้องย้ายแถวบางแถวจากตารางเดิมไปยังตารางใหม่ เมื่อต้องการทำเช่นนี้ เราจำเป็นต้องเลือกแถวที่เราต้องการแยกตาราง
เราสามารถสร้างตารางสองตารางจากตารางต้นฉบับได้โดยทำตามขั้นตอนง่ายๆ เหล่านี้:
- สร้างโคลนของตารางโดยไม่ต้องโคลนเด็ก ๆ เพื่อเก็บแถวที่ถูกย้ายและแทรกไว้หลังตารางต้นฉบับ
- เริ่มต้นที่แถวที่ระบุ ย้ายแถวถัดไปทั้งหมดไปยังตารางที่สองนี้
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแบ่งตารางออกเป็นสองตารางในแถวที่ระบุ:
// 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 firstTable = (Table) doc.GetChild(NodeType.Table, 0, true); | |
// We will split the table at the third row (inclusive). | |
Row row = firstTable.Rows[2]; | |
// Create a new container for the split table. | |
Table table = (Table) firstTable.Clone(false); | |
// Insert the container after the original. | |
firstTable.ParentNode.InsertAfter(table, firstTable); | |
// Add a buffer paragraph to ensure the tables stay apart. | |
firstTable.ParentNode.InsertAfter(new Paragraph(doc), firstTable); | |
Row currentRow; | |
do | |
{ | |
currentRow = firstTable.LastRow; | |
table.PrependChild(currentRow); | |
} while (currentRow != row); | |
doc.Save(ArtifactsDir + "WorkingWithTables.SplitTable.docx"); |