Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 123»»»

Build Great Looking Excel Spreadsheets Expand / Collapse
Author
Message
Posted Tuesday, November 4, 2008 10:57 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, November 6, 2013 2:24 PM
Points: 69, Visits: 182
Comments posted to this topic are about the item Build Great Looking Excel Spreadsheets
Post #597103
Posted Tuesday, November 4, 2008 11:34 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, March 28, 2012 8:33 PM
Points: 2, Visits: 54


SSMS > Tools > Options > Query Results > SQL Server > Results to Grid > 'Include column headers when copying or saving results'

... the macros good though :)



Post #597112
Posted Wednesday, November 5, 2008 12:54 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 2:26 AM
Points: 204, Visits: 1,336
"Excel is a great tool for formatting data that you've extracted from tables or views. You can copy such data into an Excel spreadsheet straight from SQL Management Studio's output grid. Unfortunately there's no way to also grab the column names, so unless you have some way of generating them, you're stuck either omitting them or filling them in by hand."

David I like your articel but one question:
As you incoporate VBA macros why not use ADO as alternative to get out the data from
SQL Server. This is easier and you can get the column the names by a method like:
Just part of the script
I use this one to print TAB sep files with column headers
Dim rs As New ADODB.Recordset
Dim utskriftrad as string
Dim K as long
cmd.CommandText = sqltext 'An SQL statement
rs.open cmd

For K = 0 To rs.Fields.Count - 1
utskriftrad = utskriftrad & rs.Fields(K).Name & Chr(9)
Next K
print utskriftrad etc

I yesterday wrote an article to SQL ServerCentral maybe that one will be
accepted. And there is a lot more to write how to interact with Excel.

//Gosta

Post #597129
Posted Wednesday, November 5, 2008 2:00 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 5:28 AM
Points: 587, Visits: 2,532
I realise I'm a bit off-topic, as what you are aiming to do here is a semi-manual process. but all the things you do here, and more, can be done via OLE automation. Robyn and I wrote something a while back which will work in SQL Server 2000 as well
http://www.simple-talk.com/sql/t-sql-programming/sql-server-excel-workbench/

The advantage in using the metadata from ADODB or odbc is that it will work for any result to give you your column titles, not just a table or view.



Best wishes,

Phil Factor
Simple Talk
Post #597145
Posted Wednesday, November 5, 2008 2:39 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 2:26 AM
Points: 204, Visits: 1,336
Phil
Thank you for pointing at your article. It was new for me.
But still why this labor in T-SQL when you can do all in VBA coding?

Best regards
Gosta
Post #597154
Posted Wednesday, November 5, 2008 3:40 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 5:28 AM
Points: 587, Visits: 2,532
You can do a lot of this by simply using VBA from Excel. I've done it both ways. If the structure and formatting of the spreadsheet requires knowledge of the data, or maybe if it is a timed report, or requested report, to be sent out via email using an Excel file attachment, then I do it from TSQL. Where it is a much simpler report than VBA is the obvious strategy. There are a lot of ways of producing Excel spreadsheets from database data, and the more techniques you have in your armory, the more likely you are to find one that is a good fit for a particular requirement.





Best wishes,

Phil Factor
Simple Talk
Post #597189
Posted Wednesday, November 5, 2008 4:48 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Saturday, July 12, 2014 6:44 AM
Points: 441, Visits: 1,799
Nice Macro. :)

The only thing I'd add to the Sub ColorAlternateDataRows is this line at the start
Application.ScreenUpdating = False

And at the end
Application.ScreenUpdating = True

Then it runs much quicker

HTH

Dave J



http://glossopian.co.uk/
"I don't know what I don't know."
Post #597220
Posted Wednesday, November 5, 2008 5:01 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 30, 2013 8:30 PM
Points: 2, Visits: 41
The technique from this article doesn't seem to retain the correct data types from the database.

For example a number (data type varchar in SQL) from the SQL Output grid with a leading zero, once copied into Excel with loose it's leading Zero as it will be converted to Number.
Even if you format the whole spreadsheet to text before you copy the results, its will then mean real numbers are converted as text and you can't do any more sums in Excel.

This means this technique is not really usefull as a fully automated technique. You will have to spend some time setting the Excel column cell data types acordinly. Which is a requirement from most of us i guess.

However using the SQL Server data export functionality (right click on a table, "export") will retain column names and data types. And is really as automated as running the code from the article. I would say even more !

Excel formatting bit is usefull though and the stored procedure / use of schema an eye opener.

thanks
Post #597228
Posted Wednesday, November 5, 2008 5:36 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, September 18, 2009 9:21 AM
Points: 6, Visits: 7
The article states that unfortunately there's no way to also grab the column names from SQL Management Studio's output grid, but there is a checkbox option on the Query Options dialogue box that reads "Include column headers when copying or saving the results" that works great to bring along the column headers.

Additionally, Excel 2007 now has built-in table formatting that allows you to automatically apply different colors based on conditional values from any column.

Respectfully,

LGW



Post #597248
Posted Wednesday, November 5, 2008 5:42 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Tuesday, April 6, 2010 7:00 AM
Points: 214, Visits: 44
I concur with:

SSMS > Tools > Options > Query Results > SQL Server > Results to Grid > 'Include column headers when copying or saving results'

Otherwise, good effort and nice article!!!
Post #597251
« Prev Topic | Next Topic »

Add to briefcase 123»»»

Permissions Expand / Collapse