使用表格中的文本
Contents
[
Hide
]
如前几篇文章所述,表格通常包含纯文本,尽管其他内容(如图像甚至其他表格)可以放置在表格单元格中。
向表中添加文本或其他内容是使用DocumentBuilder类的适当方法进行的,并在**“Create a Table”**文章中进行了描述。 在本文中,我们将讨论如何在已经存在的表中处理文本。
替换表中的文本
该表与Aspose.Words中的任何其他节点一样,具有对Range对象的访问权限。 使用表范围对象,可以替换表中的文本。
当前支持在替换时使用特殊字符的能力,因此可以用多段文本替换现有文本。 为此,您需要使用相应Replace方法中描述的特殊元字符。
通常,文本替换应在单元格级别(每个单元格)或段落级别完成。
下面的代码示例演示如何在整个表的单元格中替换文本字符串的所有实例:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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属性。
下面的代码示例演示如何打印表的文本范围:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); |
相同的技术仅用于从单个表格单元格中提取内容。
下面的代码示例演示如何打印行和表元素的文本范围:
下面的代码示例演示如何打印行和表元素的文本范围。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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属性添加表标题和描述。 这些属性对于符合ISO/IEC29500的DOCX文档有意义。 以早于ISO/IEC29500的格式保存时,将忽略这些属性。
下面的代码示例演示如何设置表的标题和描述属性:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |