工作表读写查询表
Contents
[
Hide
]
Aspose.Cells提供工作表.getQueryTables()返回的集合查询表集合.得到一个特定的查询表, 使用查询表集合.get() 属性并传递 QueryTable 的索引。这查询表类具有以下两个用于调整QueryTable 的属性。
这些都是布尔值。您可以通过数据 > 连接 > 属性在 Microsoft Excel 中查看它们。
工作表读写查询表
下面的示例代码读取第一个查询表第一个工作表,然后打印两个查询表特性。然后它设置查询表.PreserveFormatting到真的.
以下屏幕截图显示了源文件在代码中使用,它的属性显示了查询表值。
以下屏幕截图显示了输出excel文件由代码生成,其属性显示了查询表值。如您所见,现在已选中保留格式复选框。
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 | |
String dataDir = Utils.getDataDir(ReadingAndWritingQueryTable.class); | |
// Create workbook from source excel file | |
Workbook workbook = new Workbook(dataDir + "Sample.xlsx"); | |
// Access first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Access first Query Table | |
QueryTable qt = worksheet.getQueryTables().get(0); | |
// Print Query Table Data | |
System.out.println("Adjust Column Width: " + qt.getAdjustColumnWidth()); | |
System.out.println("Preserve Formatting: " + qt.getPreserveFormatting()); | |
// Now set Preserve Formatting to true | |
qt.setPreserveFormatting(true); | |
// Save the workbook | |
workbook.save(dataDir + "Output.xlsx"); | |
控制台输出
这是上面示例代码的控制台输出
Adjust Column Width: true
Preserve Formatting: false
检索查询表结果范围
Aspose.Cells 提供了读取地址的选项,即查询表的单元格结果范围。以下代码通过读取查询表的结果范围地址来演示此功能。可以下载示例文件这里.
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 | |
// Create workbook from source excel file | |
Workbook wb = new Workbook("Query TXT.xlsx"); | |
// Display the address(range) of result range of query table | |
System.out.println(wb.getWorksheets().get(0).getQueryTables().get(0).getResultRange().getAddress()); |