Export to Excel (.xlsx) File with OPENROWSET




--Create an Excel file \myBook2.xlsx with default Sheet1
--Past column names from the select statement to the first row in Excel

INSERT INTO 
OPENROWSET (      
  'Microsoft.ACE.OLEDB.12.0',
  'Excel 12.0;Database=c:\temp\myBook2.xlsx;HDR=YES', 
  'SELECT accountnumber, firstname, lastname, street1, street2, city, state FROM [Sheet1$]')
  SELECT  accountnumber, firstname, lastname, street1, street2, city, state    FROM [dbo].[bcpView]



---One more sample

 
if object_id('[aNewTable20150729]') IS nULL

Begin
CREATE TABLE [dbo].[aNewTable20150729](
	[accountnumber] [nvarchar](255) NULL,
	[COl1] [nvarchar](2550) NULL
) 
End

Insert into aNewTable20150729

SELECT * FROM OPENROWSET('MSDASQL','DRIVER=Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb);
DBQ=c:\temp\test8.xlsx','SELECT * FROM [Sheet1$]') 

select *, len(COL1)  from aNewTable20150729

truncate table aNewTable20150729


 
Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s