Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7185315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:23:23+00:00 2026-05-28T18:23:23+00:00

I have a script that does various things and the end result is one

  • 0

I have a script that does various things and the end result is one large table. I was wondering how I could export this final table to a new Excel file (with column headers as well).

I would need to do this within the script.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:23:24+00:00Added an answer on May 28, 2026 at 6:23 pm

    This is by far the best post for exporting to excel from SQL:

    http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=49926

    To quote from user madhivanan,

    Apart from using DTS and Export wizard, we can also use this query to export data from SQL Server2000 to Excel

    Create an Excel file named testing having the headers same as that of table columns and use these queries

    1 Export data to existing EXCEL file from SQL Server table

    insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
        'Excel 8.0;Database=D:\testing.xls;', 
        'SELECT * FROM [SheetName$]') select * from SQLServerTable
    

    2 Export data from Excel to new SQL Server table

    select * 
    into SQLServerTable FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
        'Excel 8.0;Database=D:\testing.xls;HDR=YES', 
        'SELECT * FROM [Sheet1$]')
    

    3 Export data from Excel to existing SQL Server table (edited)

    Insert into SQLServerTable Select * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
        'Excel 8.0;Database=D:\testing.xls;HDR=YES', 
        'SELECT * FROM [SheetName$]')
    

    4 If you dont want to create an EXCEL file in advance and want to export data to it, use

    EXEC sp_makewebtask 
        @outputfile = 'd:\testing.xls', 
        @query = 'Select * from Database_name..SQLServerTable', 
        @colheaders =1, 
        @FixedFont=0,@lastupdated=0,@resultstitle='Testing details'
    

    (Now you can find the file with data in tabular format)

    5 To export data to new EXCEL file with heading(column names), create the following procedure

    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)
    

    After creating the procedure, execute it by supplying database name, table name and file path:

    EXEC proc_generate_excel_with_columns 'your dbname', 'your table name','your file path'
    

    Its a whomping 29 pages but that is because others show various other ways as well as people asking questions just like this one on how to do it.

    Follow that thread entirely and look at the various questions people have asked and how they are solved. I picked up quite a bit of knowledge just skimming it and have used portions of it to get expected results.

    To update single cells

    A member also there Peter Larson posts the following:
    I think one thing is missing here. It is great to be able to Export and Import to Excel files, but how about updating single cells? Or a range of cells?

    This is the principle of how you do manage that

    update OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
    'Excel 8.0;Database=c:\test.xls;hdr=no', 
    'SELECT * FROM [Sheet1$b7:b7]') set f1 = -99
    

    You can also add formulas to Excel using this:

    update OPENROWSET('Microsoft.Jet.OLEDB.4.0', 
    'Excel 8.0;Database=c:\test.xls;hdr=no', 
    'SELECT * FROM [Sheet1$b7:b7]') set f1 = '=a7+c7'
    

    Exporting with column names using T-SQL

    Member Mladen Prajdic also has a blog entry on how to do this here

    References: http://www.sqlteam.com (btw this is an excellent blog / forum for anyone looking to get more out of SQL Server). For error referencing I used this

    Errors that may occur

    If you get the following error:

    OLE DB provider ‘Microsoft.Jet.OLEDB.4.0’ cannot be used for
    distributed queries

    Then run this:

    sp_configure 'show advanced options', 1;
    GO
    RECONFIGURE;
    GO
    sp_configure 'Ad Hoc Distributed Queries', 1;
    GO
    RECONFIGURE;
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that does various things and access parameters using sys.argv but
I have a simple script that does some search and replace. This is basically
I have to write a Matlab script that does this: The input is 2
I have the below code that does various things and checks hotfix info. I
I have made a script that run various loops and does some SQL inserts.
I have a Perl script that does various installation steps to set up a
So, I have a chrome extension that does various things to a site. What
I have a bash script that does ssh to a remote machine and executes
I have a PHP script that does the following: Uses file_get_contents() to get content
I have a ColdFusion script that does: <cfset content = replace(content,&##147;,,all)> Which replaces &147;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.