How to automatically filling excel sheets with SQL query results
Tags: sql, excel, reportautomation
Published: 2024-07-10 09:31:00

I need to fill the query results of some SQL statements into a table like the following every day.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zgbjcyxjrw49nmwwea05.png)

Here's a simple way to do it.

Note: SQLMessenger2.0 installation is required before proceeding with the following steps.

First, modify the table template to look like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gxyyngi44bqwkpgumgko.png)

Mark the cells that need to be filled with SQL query results as "Data Cell". The format for a data cell marker is **<%DataCellName%>**. 

Here, we can use formulas to generate data cell markers. For example, in the "State" (A3) cell in the figure above, we can use the formula **="<%"&A2&"%>"** to generate the data cell marker. Then, copy the A3 cell to the B3-E3 cells to quickly generate the data cell markers for the B3 to E3 cells.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mgztcqe6wytajn9r8l03.png)

After modifying the Excel template, create a task in SQLMessenger, and add an attachment template of type "Dynamic Attachment File" to the task.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/az8u5wfi4cjk2coj36qp.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4j8ufai08ptacfqoflyu.png)
Select "Customize Spreadsheet Template" for the Template Type, then click the "Select File" button to import the designed Excel template sheet.

After importing the template file, click the "New Query" button to add an SQL query to the template.

In the "Create SQL Query" wizard, select the data source and enter the SQL statement, following the wizard's prompts to proceed.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f8nr8se0jpo7khvxwkpu.png)

Set corresponding Data Cells for each SQL field that we want to display in the Excel table.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0sb4kobap9i6gft56s5a.png)

Add another query to fill in the Total row in the same way.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2jhweeyde6cj9e1o953q.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wkf9you9l2igrl4np4rj.png)

After configuring the SQL query statements, click the "Preview" button to preview the template execution results.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0863zfvd753g5ltglsqz.png)

The following image shows the Excel sheet filled out after executing the template:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5y40vln7jjossdojmdfu.png)

After completing the task configuration, click the "Deploy" button for the new task configuration to take effect.

I have also compiled some Q&A about this feature.

_Q_: Can this system automatically send the filled-out table via email to colleagues?

_A_: Yes, SQLMessenger can automatically send the table as an email attachment or in the email body to specified recipients. It depends on your configuration.  [Setting Recipients for Tasks](https://www.sqlmessenger.com/manual/index.htm?page=31.1.1.htm)

_Q_: Can this task be scheduled to run automatically at specific times I request, such as every day at 8 AM or 2 PM?

_A_: Yes. You can configure "Task Schedules" for the task to enable it to run automatically at scheduled times. [Using Task Schedules](https://www.sqlmessenger.com/manual/index.htm?page=31.1.14.htm)


_Q_: I would like to individually query personal reports (such as sales performance reports) for multiple colleagues and then send them via email to each. Can this be done?

_A_: Yes. You can use the "Information Distribute" feature to achieve point-to-point distribution of reports.  [Using Information Distribution Task](https://www.sqlmessenger.com/manual/index.htm?page=31.1.12.htm)

_Q_: Is it possible to convert SQL query results directly into an Excel spreadsheet without using a template?

_A_: Yes. You can use the "Simple Table" to do this.  [Using Simple Tables](https://www.sqlmessenger.com/manual/index.htm?page=31.1.7.htm)


Original Link:[https://www.sqlmessenger.com/docreader.html?id=506](https://www.sqlmessenger.com/docreader.html?id=506)