按名称访问文本框
Contents
[
Hide
]
以前,文本框是通过从 Workheet.getTextBoxes() 集合中的索引进行访问的,但现在还可以通过该集合的名称访问文本框。如果您已知其名称,这是一种方便快捷的访问文本框的方式。
通过名称访问文本框
以下示例代码首先创建了一个文本框并分配了一些文本和名称。然后在接下来的行中,我们通过其名称访问相同的文本框并打印其文本。
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-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(AccessTextBoxName.class); | |
Workbook workbook = new Workbook(); | |
Worksheet sheet = workbook.getWorksheets().get(0); | |
int idx = sheet.getTextBoxes().add(10, 10, 10, 10); | |
// Create a texbox with some text and assign it some name | |
TextBox tb1 = sheet.getTextBoxes().get(idx); | |
tb1.setName("MyTextBox"); | |
tb1.setText("This is MyTextBox"); | |
// Access the same textbox via its name | |
TextBox tb2 = sheet.getTextBoxes().get("MyTextBox"); | |
// Displaying the text of the textbox accessed by its name | |
System.out.println(tb2.getText()); |
控制台输出
这是上面示例代码的控制台输出。
This is MyTextBox