Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to output properly formated select to an excel file

Status
Not open for further replies.

sonname

Programmer
May 18, 2001
115
US
I have the following snippet where I output to an excel file. The problem is that not each field fits into its own cell. The whole output goes into one cell. Is there a way that you can output a query to an excel file and it basically shows up the way that it does when you run it from query analyzer in grid view.

DECLARE @cmd VARCHAR(2048)

SET @cmd = 'OSQL -SLOCALHOST -dPubs '
+ ' -Ume -Ppw'
+ ' -Q"set nocount on SELECT * FROM Authors"'
+ ' -oc:\authors.xls'

EXEC master..xp_cmdshell @cmd, NO_OUTPUT
 
The best way to output to Excel is with a Data Transfer job.
Margaret
 
alternativley a work around would be selecting the defined columns eg column1 column2 column3 or whatever is in your table and appending a comma to the end of the result
then using a csv as your output file which excel will use nicely.

nb u may want to use isnull incase there is no output to keep column aligned eg

isnull(column1,'') + ',',isnull(column2,'') + ','
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top