Sometimes you find these really old files floating around on your
harddrive and you forget that you ever downloaded them. Here is one
such example. I have no idea where I got this or who to credit for its
creation, but I've had it for awhile and came across it and thought it
would be something nice to share with you, as I’m sure it is something
of great help to many of you, especially if you are limited in your
experience on creating DTS packages, which is another way, and
preferred way under most circumstances, to get data from Sql to
Excel. This
is a T-SQL script that uses the system stored procedures sp_OA* for
creating and handling OLE objects, ADO, Jet and a linked server to
create and
populate an XLS file from a select statement. By default, if the
XLS file already exists, the result of the query will get appended to
the worksheet. You'll have to add some code to check for and
delete the file before creating if that is your desired behavior.
Oh, and I used this a long time ago
with some minor code changes and it worked fine, but this is the
original script using the pubs database, so there are changes you’ll
have to make, and they should be fairly obvious to you.