The problem
One of the tasks that I often have to complete is manipulate some data in Excel, starting from the query results in SSMS.
Excel is a very convenient tool for one-off reports, quick data manipulation, simple charts.
Unfortunately, SSMS doesn’t ship with a tool to export grid results to Excel quickly.
Excel offers some ways to import data from SQL queries, but none of those offers the rich query tools available in SSMS. A representative example is Microsoft Query: how am I supposed to edit a query in a text editor like this?
Enough said.
Actually, there are many ways to export data from SQL Server to Excel, including SSIS packages and the Import/Export wizard. Again, all those methods require writing your queries in a separate tool, often with very limited editing capabilities.
PowerQuery offers great support for data exploration, but it is a totally different beast and I don’t see it as an alternative to running SQL queries directly.
The solution
How can I edit my queries taking advantage of the query editing features of SSMS, review the results and then format the data directly in Excel?
The answer is SSMS cannot do that, but, fortunately, the good guys at Solutions Crew brought you a great tool that can do that and much more.
SSMSBoost is a free add-in that empowers SSMS with many useful features, among which exporting to Excel is just one. I highly suggest that you check out the feature list, because it’s really impressive.
Once SSMSBoost is installed, every time you right click a results grid, a context menu appears that lets you export the grid data to several formats.
No surprises, one of those formats is indeed Excel.
The feature works great, even with relatively big result sets. However, it requires 5 clicks to create the Spreadsheet file and one more click to open it in Excel:
So, where is the single click I promised in the title of this post?
The good news is that SSMSBoost can be automated combining commands in macros to accomplish complex tasks.
Here’s how to create a one-click “open in Excel” command:
First, open the SSMSBoost settings window clicking the “Extras” button.
In the “Shortcuts & Macros” tab you can edit and add macros to the toolbar or the context menu and even assign a keyboard shortcut.
Clicking the “definitions” field opens the macro editor
Select “Add” and choose the following command: “SSMSBoost.Connect.GridDataCopyTemplateAllGridsDisk3”. This command corresponds to the “Script all grids as Excel to disk” command in SSMSBoost.
Now save everything with OK and close. You will notice a new button in your toolbar:
That button allows to export all grids to Excel in a single click.
You’re almost there: now you just need something to open the Excel file automatically, without the need for additional clicks.
To accomplish this task, you can use a Powershell script, bound to a custom External Tool.
Open the External Tools editor (Tools, External Tools), click “Add” and type these parameters:
Title: Open last XML in Excel
Command: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Arguments: -File %USERPROFILE%\openLastExcel.ps1 -ExecutionPolicy Bypass
Click OK to close the External Tools editor.
This command lets you open the last XML file created in the SSMSBoost output directory, using a small Powershell script that you have to create in your %USERPROFILE% directory.
The script looks like this:
## ============================================= ## Author: Gianluca Sartori - @spaghettidba ## Create date: 2014-01-15 ## Description: Open the last XML file in the SSMSBoost ## output dicrectory with Excel ## ============================================= sl $env:UserProfile # This is the SSMSBoost 2012 settings file # If you have the 2008 version, change this path # Sorry, I could not find a registry key to automate it. $settingsFile = "$env:UserProfile\AppData\Local\Solutions Crew\Ssms2012\SSMSBoostSettings.xml" # Open the settings file to look up the export directory $xmldata=[xml](get-content $settingsFile) $xlsTemplate = $xmldata.SSMSBoostSettings.GridDataCopyTemplates.ChildNodes | Where-Object { $_.Name -eq "Excel (MS XML Spreadsheet)" } $SSMSBoostPath = [System.IO.Path]::GetDirectoryName($xlsTemplate.SavePath) $SSMSBoostPath = [System.Environment]::ExpandEnvironmentVariables($SSMSBoostPath) # we filter out files created before (now -1 second) $startTime = (get-date).addSeconds(-1); $targetFile = $null; while($targetFile -eq $null){ $targetFile = Get-ChildItem -Path $SSMSBoostPath | Where-Object { $_.extension -eq '.xml' -and $_.lastWriteTime -gt $startTime } | Sort-Object -Property LastWriteTime | Select-Object -Last 1; # file not found? Wait SSMSBoost to finish exporting if($targetFile -eq $null) { Start-Sleep -Milliseconds 100 } }; $fileToOpen = $targetFile.FullName # enclose the output file path in quotes if needed if($fileToOpen -like "* *"){ $fileToOpen = "`"" + $fileToOpen + "`"" } # open the file in Excel # ShellExecute is much safer than messing with COM objects... $sh = new-object -com 'Shell.Application' $sh.ShellExecute('excel', "/r " + $fileToOpen, '', 'open', 1)
Now you just have to go back to the SSMSBoost settings window and edit the macro you created above.
In the definitions field click … to edit the macro and add a second step. The Command to select is “Tools.ExternalCommand1”.
Save and close everything and now your nice toolbar button will be able to open the export file in Excel automagically. Yay!
Troubleshooting
If nothing happens, you might need to change your Powershell Execution Policy. Remember that SSMS is a 32-bit application and you have to set the Execution Policy for the x86 version of Powershell.
Starting Powershell x86 is not easy in Windows 8/8.1, The documentation says to look up “Windows Powershell (x86)” in the start menu, but I could not find it.
The easiest way I have found is through another External Tool in SSMS. Start SSMS as an Administrator (otherwise the UAC will prevent you from changing the Execution Policy) and configure an external tool to run Powershell. Once you’re in, type “Set-ExecutionPolicy Remotesigned” and hit return. The external tool in your macro will now run without issues.
Bottom line
Nothing compares to SSMS when it comes down to writing queries, but Excel is the best place to format and manipulate data.
Now you have a method to take advantage of the best of both worlds. And it only takes one single click.
Enjoy.