Click here to Skip to main content
12,072,067 members (61,336 online)
Click here to Skip to main content
Add your own
alternative version


41 bookmarked

Dynamically creating Excel File, ExcelSheets and Exporting Data from SQL Server using SSIS 2005

, 15 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Export SQL Server Tables to Excel Files Dynamically


This article is intended primarily for intermediate to advanced SSIS (SQL Server Integration Services) users who are familiar with SQL Server Business Intelligence. Basic understanding of script task, Execute Sql Task, parameters and variables are required, as it is not exactly a tutorial, but a description of the implementation of exporting the database table objects to Excel Objects through SSIS without the need of any templates and much of programming.This package will help to migrate data in a simple and efficient way. This package could also be useful to the users of SQL Server 2008 database, which could help in replacing the deprecated sp_makewebtask feature for exporting the data to excel.

The Goal of this article is to help the users by providing the concept to create a Excel file with unlimited sheets with all the data from the SQL Sever database eliminating the need of SSIS data conversion tasks, mapping and predefined column names, data types and etc. The same concept of developing generic packages can be applied similarly when migrating SQL Server data to different databases like Oracle, Teradata, MS-Access and etc using the connection manager with related data source providers can be achieved through the SSIS ETL Tool.

This Generic DTSX package is basically to demonstrate a method of data migration to export SQL Server database tables to Excel file with multiple sheets. After reading many blogs about people having problems in generating excel files and sheets dynamically from SQL Server 2000/2005 through SSIS (SQL Server Integration Services). Here is the simple DTSX package which creates a excel file on the fly and dumps the data on the sheets.

Currently Most of the Microsoft Dynamics ERP software products like (Navision, Axapta, Great Plains, Solomon, Concorde, Point of Sale, Retail Management System, Customer Relationship Management and etc) are also using the SQL Server and Excel for data migration and reporting purpose.

Little bit of tweaks in the below code in the tasks could help many users to achieve their goals in a easiest way.

Below is the demonstration of the steps to achieve this.


The dtsx package should look like the above Image. The package is developed using Visual Studio 2005.


Change the database connection to SQL Server 2000/2005 and point to the database which you want to export (currently it is pointed to master database). Change the Variable DestExcelFilePath Value from 'C:\SSIS' for creating a file at the desired location.

Note: This package should work properly if all the Table names and Column names in the database are followed as per the microsoft naming standards.


Excel file treats each sheet as a table.Excel-95/2003 generates only 65335 rows for sheet and 255 sheets maximum if its Excel-95, There is no rows or sheets limitation if you are using Excel 2007 and above. There are certain limitations to Excel previous versions. Read online documentation for more information.

Following are few links on Excel differences and comparisons.

The above package uses the SQL Server OPENROWSET Object for data migration. For more information on the OPENROWSET functionality and its options refer online documentation at the following link

Explanation and use of OPENROWSET, SP_SERVEROPTION, SP_MAKEWEBTASK and server options like 'DATA ACCESS','SHOW ADVANCED OPTIONS', 'AD HOC DISTRIBUTED QUERIES' is out of scope of this article.

Using the Code

These options need to be configured 'Show Advanced Options' and 'Ad Hoc Distributed Queries' on the SQL Server before running the package. The package will fail without these Options. And importantly the database login should have admin privileges to execute the package tasks successfully. If you want to run this code, then better check with your DBA's regarding permissions you require.

/*-- This has to executed on the server before running the package.
  -- These Options are required to run the OPENROWSET Function.                                                      This has to be just executed once.*/

SP_CONFIGURE 'Show Advanced Options', 1
SP_CONFIGURE 'Ad Hoc Distributed Queries', 1

[Get List of Tables to Process] Execute SQL Task: The query is to retrieve all the table names which are less then 65335 rows. ( Customize this query as per your requirement.)

 CREATED ON      : This is created and tested in SQL SERVER 2000 and SQL SERVER 2005.
 PURPOSE      : This Object is get the List of Tables from the Database.
 COMMENTS     : So that it can be used for creating objects in xls file via SSIS.
                Tested on SQL Server 8.00.194 - RTM (Desktop Engine) ,SQL Server 9.00.4035.00 - SP3 (Developer Edition)
 SSIS TASK    : [Get List of Tables to Process] SSIS Execute SQL Task   

DECLARE @Server varchar(50) 
-- This ServerOption Should be Turned on 
EXEC sp_serveroption  @Server,'DATA ACCESS','TRUE'

SELECT [TableName] = 
                FROM sysobjects so, sysindexes si 
                WHERE so.xtype = 'U' AND = OBJECT_ID(
/*Comment the following line if you are using Excel 2007.*/
                AND si.rows < 65335 
/*Uncomment the following code if you want to avoid the table names where you dont have data in it.*/
                --AND si.rows > 0
                GROUP BY 
                ORDER BY 1 DESC

[Excel Script Generator] Execute SQLTask:The query in this task builds the table structure with the data types required for the Excel.(Most of the SQL data types have been handled here. If you want add any other specific data type or a different database then you need to modify this.)

 CREATED ON      : This is created and tested in SQL SERVER 2000 and SQL SERVER 2005.
 PURPOSE      : This Object is Created for Generating a Script Table for Excel.
 COMMENTS     : So that it can be used for creating objects in xls file via SSIS.
                Tested on SQL Server 8.00.194 - RTM (Desktop Engine) ,SQL Server 9.00.4035.00 - SP3 (Developer Edition) 
 SSIS TASK    : [Excel Script Generator] Execute SQL Task 
DECLARE @vsTableName VARCHAR(100)  
SET @vsTableName =?
     Handled Most of the regular SQL data types below. */
  SELECT @vsSQL = 'CREATE TABLE ' + '`' + @vsTableName + '`' + CHAR(10) + '(' + CHAR(10)  
  --SELECT @vsSQL = @vsSQL + '`' + RTRIM(sc.Name) + '` ' +  
  SELECT @vsSQL = @vsSQL + '[' + sc.Name + '] ' +  
  CASE WHEN st.Name IN ('nvarchar','ntext','text','varchar','varchar','char','nchar','xml','uniqueidentifier') THEN  'LongText'  
       WHEN st.Name IN ('tinyint','int','smallint','bigint','float','numeric','decimal','money','smallmoney','bit') THEN  'Long'
       WHEN st.Name IN ('date','datetime','timestamp') THEN  'datetime'
  ELSE ' ' END + ',' + CHAR(10) 
  FROM sysobjects so  
  JOIN syscolumns sc ON =  
  JOIN systypes st ON st.xusertype = sc.xusertype  
  WHERE = @vsTableName 
  /* Avoiding the following data types here... */
  AND st.Name not in ( 'image','sysname','binary','varbinary','xml','uniqueidentifier') 
  SELECT SUBSTRING(@vsSQL,1,LEN(@vsSQL) - 2) + CHAR(10) + ')'  AS ExcelTableName   

[Create Tables in Excel] Execute SQLTask: The create table script query from the above task is passed to this task, which is executed in the Excel file with the Excel Connection Manager for creating sheets with the table structure and data types required for the Excel. The destination file could be found with the Name (DynamicExcelFileDDMMYYYY.xls). Basically this task is the key to create all the sheets in the excel destination file.

[Get Column Names] Execute SQLTask: The task builds the column names and passes it to the below [Insert Script Generation] Script Task which is used in the query with OPENROWSET.

 CREATED ON      : This is created and tested in SQL SERVER 2000 and SQL SERVER 2005.
 PURPOSE      : This Object is Created for Generating Column names.
 COMMENTS     : So that it can be used for creating objects in xls file via SSIS.
                Tested on SQL Server 8.00.194 - RTM (Desktop Engine) ,SQL Server 9.00.4035.00 - SP3 (Developer Edition)

DECLARE @SRCOBJECT varchar(8000)
DECLARE @SRCOBJECT1 varchar(8000)

SET @SRCOBJECT1 = Replace(@SRCOBJECT1,'LongText','')

SET @SRCOBJECT1 = Replace(@SRCOBJECT1 ,'Long','')

SET @SRCOBJECT1 = Replace(@SRCOBJECT1,'DateTime','')

SET @SRCOBJECT1= Replace(@SRCOBJECT1,')','')

SET @SRCOBJECT1 = Replace(@SRCOBJECT1,'`','')


[Insert Script Generation] ScriptTask: This script task code in is to build a Query using SQL Server OPENROWSET function which finally inserts the data into the ExcelSheets. The query is passed to the Execute SQL Task through a variable and is executed on the Excel file with the Excel Connection Manager.

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime

Public Class ScriptMain

    Public Sub Main()
        ' Insert Script Generation

        Dim excelFilePath As String = CStr(Dts.Variables("User::DestExcelFilePath").Value) + CStr(Dts.Variables("DestExcelFileName").Value)
        Dim TableName, ExcelTable As String
        TableName = CStr(Dts.Variables("User::ExcelTable").Value)

        If TableName.Length > 30 Then
            ExcelTable = Strings.Left(TableName, 31)
            'Excel sheet Name can only be 30 characters length.
            ExcelTable = TableName
        End If
       'Retrieve the ExcelColumnNames from the Variable and build the String Here.
        Dim ColumnNames As String = CStr(Dts.Variables("User::ExcelColumns").Value)
        Dim strCn As String = " Insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0'," & _
                "'Excel 8.0;Database=" + excelFilePath + "','SELECT " + ColumnNames + " FROM [" + ExcelTable + "$]') SELECT " + ColumnNames + " FROM [" + TableName + "]"

        'Uncomment the following message to check the query.
        Dts.Variables("User::InsertScripter").Value = strCn
        Dts.TaskResult = Dts.Results.Success
    End Sub

End Class 


The above mentioned demonstration of steps determine that excel file with multiple sheets along with data migration from SQL Server can be dynamically created using SSIS.


Hence it can be concluded that exporting the database table objects to a Excel file with multiple sheets is achieved through SSIS without the need of any templates, DLL's and much of programming.

This Article is basically intended to demonstrate the concept of developing a generic package to create sheets and table structures in a excel file with SSIS and to understand the excel functionalities. The same concept can be applied similarly when migrating SQL Server data to different databases. Its not a good Idea to export a huge database with the above package where it might end up using the system memory and other resources.

Since the Web Assistant features like SP_MAKEWEBTASK which help in exporting the data objects to html, excel, word, etc are no longer available in SQL Server 2008. This package could also be useful to the users of SQL Server 2008 database, which could help in replacing the deprecated sp_makewebtask for exporting the data to excel file.

There are many articles on creating & handling excel files including formatting the cells, inserting formulas and manipulating the excel objects which can be achieved through various programming languages. But this article is intended for the SQL Server Integration Services users who are aware of its benefits and limitations.


The above image is provided as a sample to show the excel file created through this package.

Hope this article will serve the purpose. Any suggestions or corrections are most welcome.


Points of Interest

Reverse Engineering, Up-gradations, Conversions, Integrations and Data migration.

Have loads of real time experience on million dollar Banking and Financial data migration projects.


Last Updated on 16 Th Jan 2012.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Venkat Chaitanya Kanumukula
Technical Lead
India India
He is a lead software developer, and has been in the industry since Oct 1997. Currently working on Dealership Management System using Microsoft Dynamics AX 2012( Inc - Visual Studio, SharePoint, SQL Server, BizTalk, MS Windows Server and etc).

You may also be interested in...

Comments and Discussions

QuestionNeed Help Pin
Ratnesh N Bharos17-Apr-13 1:07
memberRatnesh N Bharos17-Apr-13 1:07 
Hi Venkat,

Thanks for such nice article. I implement an ssis package same as you mention in article . I got error at last stage of Execute sql task while insertScript insert data from sql table to excel table. the error is

[Execute SQL Task] Error: Executing the query " Insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0',..." failed with the following error: "OLE DB provider 'Microsoft.Jet.OLEDB.4.0' cannot be used for distributed queries because the provider is configured to run in single-threaded apartment mode.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.

Please give me the suggestion and workarround.

Ratnesh Nirmal Bharos

(Dot Net Professional)
QuestionNeed help!! Pin
Vikky2107310-Feb-13 22:25
memberVikky2107310-Feb-13 22:25 
AnswerRe: Need help!! Pin
Venkat Chaitanya Kanumukula11-Feb-13 20:15
memberVenkat Chaitanya Kanumukula11-Feb-13 20:15 
GeneralRe: Need help!! Pin
Vikky2107311-Feb-13 20:20
memberVikky2107311-Feb-13 20:20 
QuestionQuestion Pin
andrey.koshkarov231-Jan-13 4:37
memberandrey.koshkarov231-Jan-13 4:37 
AnswerRe: Question Pin
Venkat Chaitanya Kanumukula8-Feb-13 20:09
memberVenkat Chaitanya Kanumukula8-Feb-13 20:09 
GeneralMy vote of 5 Pin
vkushwaha30-Jan-13 11:02
membervkushwaha30-Jan-13 11:02 
QuestionExcel SSIS Pin
Rio Khn2-Nov-12 2:37
memberRio Khn2-Nov-12 2:37 
QuestionIssue in solving a requirement in SSIS Pin
prabusuccess11-Oct-12 22:52
memberprabusuccess11-Oct-12 22:52 
AnswerRe: Issue in solving a requirement in SSIS Pin
Venkat Chaitanya Kanumukula16-Oct-12 7:26
memberVenkat Chaitanya Kanumukula16-Oct-12 7:26 
GeneralMy vote of 5 Pin
dineshvishe20-Aug-12 9:27
groupdineshvishe20-Aug-12 9:27 
GeneralRe: My vote of 5 Pin
Venkat Chaitanya Kanumukula8-Feb-13 20:27
memberVenkat Chaitanya Kanumukula8-Feb-13 20:27 
AnswerRe: My vote of 5 Pin
Venkat Chaitanya Kanumukula11-Feb-13 20:41
memberVenkat Chaitanya Kanumukula11-Feb-13 20:41 
QuestionStep3:Create Tables in Excel (Code Missing) Pin
Member 90962218-Jun-12 23:31
memberMember 90962218-Jun-12 23:31 
GeneralExcel Sheet Names Pin
LeMike23-May-12 7:30
memberLeMike23-May-12 7:30 
GeneralRe: Excel Sheet Names Pin
Venkat Chaitanya Kanumukula27-Jun-12 19:31
memberVenkat Chaitanya Kanumukula27-Jun-12 19:31 
GeneralRe: Excel Sheet Names Pin
LeMike27-Jun-12 20:18
memberLeMike27-Jun-12 20:18 
QuestionRetaining template in excel and adding records to those columns from sql server 2008 DB using C# code Pin
Kiran S.V23-May-12 4:25
memberKiran S.V23-May-12 4:25 
GeneralMy vote of 5 Pin
Member 432084412-Feb-12 7:33
memberMember 432084412-Feb-12 7:33 
QuestionMessage Automatically Removed Pin
7-Jan-12 2:13
groupsghjyuk7-Jan-12 2:13 
AnswerMessage Removed Pin
Venkat Chaitanya Kanumukula11-Jan-12 6:23
memberVenkat Chaitanya Kanumukula11-Jan-12 6:23 
GeneralRe: marketing Pin
AspDotNetDev19-Apr-12 7:17
protectorAspDotNetDev19-Apr-12 7:17 
GeneralMy vote of 5 Pin
Member 43208442-Jan-12 9:28
memberMember 43208442-Jan-12 9:28 
QuestionRe: My vote of 5 Pin
Member 47047866-Aug-13 9:25
memberMember 47047866-Aug-13 9:25 
AnswerRe: My vote of 5 Pin
Venkat Chaitanya Kanumukula5-Mar-15 6:57
memberVenkat Chaitanya Kanumukula5-Mar-15 6:57 
GeneralGood one on excel! Pin
bantu211-Jan-12 7:36
memberbantu211-Jan-12 7:36 
Question.NET alternative Pin
JamesHoward97226-Dec-11 23:34
memberJamesHoward97226-Dec-11 23:34 
AnswerRe: .NET alternative Pin
Venkat Chaitanya Kanumukula11-Jan-12 5:50
memberVenkat Chaitanya Kanumukula11-Jan-12 5:50 
GeneralMy vote of 4 Pin
Suresh Suthar21-Dec-11 20:37
memberSuresh Suthar21-Dec-11 20:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.160208.1 | Last Updated 16 Jan 2012
Article Copyright 2011 by Venkat Chaitanya Kanumukula
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid