01.08.08

Export data from SQL Server to Excel without using SSIS or DTS

Posted in SQL Server, SSIS, Utlities tagged , , , , , , at 5:19 am by dayananthan

Hi,

Normally for exporting data from SQL Server to Excel one would use DTS (SQL 2k) or SSIS (SQL Server 2005).

For some reason if at all you want to do it via query read on:

Step 1: Execute the code snippet

–Step 1: Execute the code snippet

EXEC sp_configure ’show advanced options’, 1;

GO

RECONFIGURE;

GO

EXEC sp_configure ‘Ad Hoc Distributed Queries’, 1;

GO

RECONFIGURE;

GO

Step 2: Create the excel file and then add the headings in the .xls file.

– Create one Excel File in c:\Test.xls and rename the Sheet1 to Emp. The Sheet should contain 2 columns EmployeeID,Title

Step 3: Run the following query to export data from sql server to excel

USE [AdventureWorks]

INSERT INTO OPENROWSET (‘Microsoft.Jet.OLEDB.4.0′, ‘Excel 8.0;Database=c:\Test.xls;’,‘Select * from [Emp$]‘)

SELECT EmployeeID, Title FROM HumanResources.Employee

Points which might interest you:

1. As long as the file is within your C: drive this sample would work. If at all your database is in a different machine from that .xls file you need to change Database=c:\Test.xls; to UNC path. For example, Database=\\Servername\shareName (And need to provide appropriate permission).

2. Instead of “Emp” replace it with your excel worksheet name.

20 Comments »

  1. priya said,

    Thanks daya!! Its really helped me a lot….

  2. Ankit said,

    This is exporting data directly from sql server to excel, is there any way to do so using java???
    Please let me know…..

  3. brad77 said,

    I tried to get this working on the SQL Express instance on my laptop running Vista and had a few problems. I kept receiving the following message:

    OLE DB provider “Microsoft.Jet.OLEDB.4.0″ for linked server “(null)” returned message “Unspecified error”.
    Msg 7303, Level 16, State 1, Line 1
    Cannot initialize the data source object of OLE DB provider “Microsoft.Jet.OLEDB.4.0″ for linked server “(null)”.

    Not all that helpful. It ends up that it was a permissions problem. I needed to be sure to run the SQL Management Studio as Administrator (right-click on the shortcut and choose “Run as administrator”) to get it to work.

  4. Guruprasath B said,

    Thanks Daya. Wonderful Article. Its helped me a lot.

  5. Bill Obst said,

    Daya… is there a parm that can be specified to direct that the data should be exported to a specific worksheet in the Excel file?

  6. dayananthan said,

    Hi Bill,

    You can give the worksheet name as parameter
    Select * from [Emp$]

    In the above statement [Emp] is the worksheet name. You can place a variable as a parameter there and data will be directed to that specified sheet

  7. Srinivas said,

    It helped me a lot and solved my problem.
    But i have some queries…
    How to delete existing data in the excel file through SQL Query?
    So that i can remove repeated data.

  8. Keith Robinson said,

    Just as a heads up to everyone. This won’t work in SQL 2008 64bit. There is not a Microsoft.Jet.OLEDB.4.0 driver for SQL 2008.

    • dayananthan said,

      Oh is it i didn’t try that in SQL 2008 64 bit… Thanks for your information

      Dayaa

  9. Naveen kumar said,

    hi

    I tried running the query in sql 2000. I got the following error. My sheet name is sheet1 only. But still I am getting the error. Can u help me out?

    Server: Msg 7399, Level 16, State 1, Line 1
    OLE DB provider ‘Microsoft.Jet.OLEDB.4.0′ reported an error.
    [OLE/DB provider returned message: The Microsoft Jet database engine could not find the object 'Sheet1'. Make sure the object exists and that you spell its name and the path name correctly.]

  10. Marin said,

    Ah, I was so happy to find this. but it doesnt work on 64bit :(

  11. Dave said,

    I am wondering is there a way to Export from EXCEL to MS SQL Server 2005 Express without using SSIS or DTS ?

    Very much appreciate for your kind suggestion.

  12. John Esraelo said,

    Very useful information and it worked for me right away.. I wonder if the same type of openrowset would work with almost any external links..
    Of course with the proper selection of the OLEDB and the path and all. that..
    very good.. and thank you again.
    JohnE

  13. John Esraelo said,

    Thank you!! It worked for me right away.. excellent method..
    This way, I don’t have to enable my SSIS engine and process and I can put my script right onto the Job/Task sql panel.
    awesome

    JohnE

  14. tms said,

    Configuration option ’show advanced options’ changed from 1 to 1. Run the RECONFIGURE statement to install.
    Configuration option ‘Ad Hoc Distributed Queries’ changed from 1 to 1. Run the RECONFIGURE statement to install.
    Msg 213, Level 16, State 1, Line 2
    Insert Error: Column name or number of supplied values does not match table definition.

    help-me! :)

    • Matt Thompson said,

      Great idea. I tried BCP to get the desired output but it wouldn’t readily re-import correctly back to SQL.
      Now i am going to incorporate this with just adding the column names via the information schema. and trying to specify the worksheet name the same as well.

  15. tester said,

    ERROR:
    Ad hoc access to OLE DB provider ‘Microsoft.Jet.OLEDB.4.0′ has been denied. You must access this provider through a linked server.

  16. murale said,

    hey.. i need to export tables in my sql 2008 into multiple excel files,( not in multiple excel pages in one excel file…)

  17. murale said,

    hey.. i need to export tables from my sql 2008 into multiple excel files,( not in multiple excel pages in one excel file…)


Leave a Comment