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

บทความนี้ไม่มีการอ้างอิงจากเอกสารอ้างอิงหรือแหล่งข้อมูลโปรดช่วยพัฒนาบทความนี้โด.

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

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

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document(getMyDir() + "Tables.docx");
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
table.getRange().replace("Carrots", "Eggs", new FindReplaceOptions(FindReplaceDirection.FORWARD));
table.getLastRow().getLastCell().getRange().replace("50", "20", new FindReplaceOptions(FindReplaceDirection.FORWARD));
doc.save(getArtifactsDir() + "FindAndReplace.ReplaceTextInTable.docx");

แยกข้อความธรรมดาจากตาราง

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document(getMyDir() + "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.
System.out.println("Contents of the table: ");
System.out.println(table.getRange().getText());

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
System.out.println("\nContents of the row: ");
System.out.println(table.getRows().get(1).getRange().getText());
System.out.println("\nContents of the cell: ");
System.out.println(table.getLastRow().getLastCell().getRange().getText());

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

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java.git.
Document doc = new Document(getMyDir() + "Tables.docx");
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
table.setTitle("Test title");
table.setDescription("Test description");
OoxmlSaveOptions options = new OoxmlSaveOptions(); { options.setCompliance(OoxmlCompliance.ISO_29500_2008_STRICT); }
doc.getCompatibilityOptions().optimizeFor(com.aspose.words.MsWordVersion.WORD_2016);
doc.save(getArtifactsDir() + "WorkingWithTableStylesAndFormatting.SetTableTitleAndDescription.docx", options);