工作表的查询表读取和写入

工作表的查询表读取和写入

以下示例代码读取第一个工作表的第一个查询表,并打印出两个查询表属性。然后将QueryTable.PreserveFormatting设置为true。

您可以从以下链接下载用于此代码的源Excel文件和代码生成的输出Excel文件。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create workbook from source excel file
Workbook workbook = new Workbook(dataDir + "Sample.xlsx");
// Access first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Access first Query Table
QueryTable qt = worksheet.QueryTables[0];
// Print Query Table Data
Console.WriteLine("Adjust Column Width: " + qt.AdjustColumnWidth);
Console.WriteLine("Preserve Formatting: " + qt.PreserveFormatting);
// Now set Preserve Formatting to true
qt.PreserveFormatting = true;
// Save the workbook
workbook.Save(dataDir + "Output_out.xlsx");

控制台输出

这是上述示例代码的控制台输出

Adjust Column Width: True

Preserve Formatting: False

检索查询表结果范围

Aspose.Cells提供了读取查询表结果范围的选项。以下代码演示了通过读取查询表的结果范围地址来实现此功能。示例文件可以从这里下载。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Create workbook from source excel file
Workbook wb = new Workbook("Query TXT.xlsx");
// Display the address(range) of result range of query table
Console.WriteLine(wb.Worksheets[0].QueryTables[0].ResultRange.Address);