SQL Server stored procedure export to Excel workbook with multiple sheets

calendar_today Asked Jan 5, 2012
thumb_up 6 upvotes
history Updated April 16, 2026

Question posted 2012 · +5 upvotes

How can I export to an Excel workbook from a stored procedure to multiple sheets with few sql statements?

I am currently using the following statement:

EXEC proc_generate__excel 'db', 'temp',@filename, @SeqNo, @ext, @sqlorder

It will create three Excel workbooks, if there are three sql statement.

How can I export data from three sql statement to three sheets in one Excel workbook?

Accepted answer +6 upvotes

  1. Create an empty Excel file with the sheets in it you need (my example sales.xls with sheets “sheet1″,”sheet2”)

  2. Copy empty file to desired location/name

  3. Using your select statement to get the desired information for sheet1; insert the data into the excel file:

    insert into OPENROWSET(
       'Microsoft.Jet.OLEDB.4.0', 
       'Excel 8.0;Database=d:exportsales.xls;;HDR=YES', 
       'SELECT * FROM [Sheet1$]')
    select * from sales_part1
    
  4. Using your select statement to get the desired information for sheet2; insert the data into the excel file:

    insert into OPENROWSET(
        'Microsoft.Jet.OLEDB.4.0', 
        'Excel 8.0;Database=d:exportsales.xls;;HDR=YES', 
        'SELECT * FROM [Sheet2$]')
    select * from sales_part2
    

Check these links for reference:
http://www.sqlservercentral.com/Forums/Topic487837-19-1.aspx
http://www.sqlservercentral.com/Forums/Topic660148-338-1.aspx
http://www.databasejournal.com/features/mssql/article.php/10894_3331881_1

Some SO threads:
SqlServer to Excel export with OPENROWSET
error on sql script with 'openrowset'

2 code variants in this answer

  • Variant 1 — 5 lines, starts with insert into OPENROWSET(
  • Variant 2 — 5 lines, starts with insert into OPENROWSET(

External references cited (3)

Top excel Q&A (6)

+6 upvotes ranks this answer #141 out of 167 excel solutions on this site .