Follow this article to access and update an Excel QueryTable using C#. It has details to set the IDE, a list of steps, and a sample code for accessing and editing an Excel query table using C#. You will learn to check and access the query tables collection, fetch it, and update its properties if required.
Steps to Update Excel External Data Connection using C#
- Set the IDE to use Aspose.Cells for .NET to work with the query table
- Load the Excel file into the Workbook object containing the QueryTable
- Access the first sheet, get the collection of the QueryTable objects, and get the first QueryTable
- Print the basic information of the selected QueryTable
- Check the existence of the external connection and display its properties
- Update the connection settings in the QueryTable object
- Write the updated properties and save the workbook
These steps summarize how to work with an external data connection using C# in an Excel file. Load the Excel file into the Workbook object, access the first sheet, fetch the collection of query tables, print query table data, and update its properties. Access the external connection in the query table, update its properties, and save it on the disk.
Code to Update QueryTable Connection using C#
This code demonstrates how to perform different operations on the Excel QueryTable. Ensure that you set the AdjustColumnWidth and PreserveFormatting together, as these are commonly paired in query table output. Also, check if the QueryTable still exists after making changes, as inappropriate changes may not save the QueryTable in the output Excel file.
This article has taught us the process to work with the query table in an Excel file. To create subtotals in an Excel file, refer to the article Creating subtotals in Excel using C#.