Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 SSIS and Import/Export (2008)
 Data format while exporting from SQL to Excel

Author  Topic 

umertahir
Posting Yak Master

154 Posts

Posted - 2010-03-09 : 10:56:12
Hi All,

I am exporting data from SQL to Excel files.
My main objective is to
  • Using column names from SQL table as column headers
  • Remove existing data from the old excel file
  • Expanding the column width so that all the data is visible when the file is opened. The reason why this is important is because the same excel file is used in a MS Word document as a linked object and if the columns are not wide enough in excel then data stays hidden in the Word file thus not coming out in print outs


I have managed to do above two tasks from the list using the following code:


create procedure proc_generate_excel_with_columns
(
@db_name varchar(100),
@table_name varchar(100),
@file_name varchar(100)
)
as

--Generate column names as a recordset
declare @columns varchar(8000), @sql varchar(8000), @data_file varchar(100)
select
@columns=coalesce(@columns+',','')+column_name+' as '+column_name
from
information_schema.columns
where
table_name=@table_name
select @columns=''''''+replace(replace(@columns,' as ',''''' as '),',',',''''')

--Create a dummy file to have actual data
select @data_file=substring(@file_name,1,len(@file_name)-charindex('\',reverse(@file_name)))+'\data_file.xls'

--Generate column names in the passed EXCEL file
set @sql='exec master..xp_cmdshell ''bcp " select * from (select '+@columns+') as t" queryout "'+@file_name+'" -c'''
exec(@sql)

--Generate data in the dummy file
set @sql='exec master..xp_cmdshell ''bcp "select * from '+@db_name+'..'+@table_name+'" queryout "'+@data_file+'" -c'''
exec(@sql)

--Copy dummy file to passed EXCEL file
set @sql= 'exec master..xp_cmdshell ''type '+@data_file+' >> "'+@file_name+'"'''
exec(@sql)

--Delete dummy file
set @sql= 'exec master..xp_cmdshell ''del '+@data_file+''''
exec(@sql)
Reference: [url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926&whichpage=1[/url]

Can anyone suggest me any more options I can use with BCP command or anything else to achieve the last step?

Thanks

umertahir
Posting Yak Master

154 Posts

Posted - 2010-03-09 : 11:07:33
The simple table import is fine but when it comes to big strings then that's where the problem occurs. The example problematic data is as follows:


Description Prev_Count Curr_Count Diff
------------------------------------------------------------------------------------------ ----------- ----------- -----------
--------------------------------------------------------------------- NULL NULL NULL
DataFileformatForMIX NULL NULL NULL
AllBreeds current::102_Live; previous:101_Live NULL NULL NULL
DataFileformatForMix: number of records: 124272743 126012029 1739286
Number of animals: 7778431 7855380 76949
Number of dropped animals (just group of 4) NULL 3703 NULL
Number of gained animals (just group of 4) NULL 80652 NULL
Number of DROPPED <group-of-4, lactation and DIM> values NULL 94446 NULL
Number of GAINED <group-of-4, lactation and DIM> values NULL 1833706 NULL


when it comes out on MS Word its like this:
Go to Top of Page
   

- Advertisement -