Shaggy
Shaggy

Reputation: 5800

Export data to Excel file using SSIS

I have SSIS Package which Exports Data from table to Excel file

Control Flow :-

Control Flow

Data Flow :-

enter image description here

This is My Step :-

  1. Drop Excel Table
  2. Create Excel Table with format as of my Select Query which i used to retrieve data from database
  3. Insert Data from Database to Excel file

I Used Query Like Select * From Table Where --Some Condition

I retrieve 3000 rows out of 10000 rows and put that 3000 rows in my excel sheet. But when open my excel sheet i saw scrollbar which goes till 10000th row and ends hence my excel sheet size also increses . how can i reduce my excel sheet size ? my excel sheet contains only 3000 rows then why blank cells which goes till 10000th row ?

SQL Server 2008 & Visual Studio 2008 with BIDS

Upvotes: 10

Views: 63427

Answers (2)

praveen
praveen

Reputation: 12281

Drop table SheetName doesn't delete the sheet instead it just deletes the row . If for the 1st time you have loaded 10K rows and then again executed the package by restricting the number of rows to 3K ,the excel file will still contain those 10K empty rows as it retains the sheet along with the empty spaces .

You can use script task to delete the sheet using COM obects .But for that you need to place the Excel PIA(Primary Interop Assemply) to make it visible for VSA or else create a new excel file every time the package runs

Else as suggested by Nicarus use File System Task to delete the existing file and create a new Excel file on every execution .

Diagram :

enter image description here

File System Task :

enter image description here

Use the same components and the query for Create Table using Execute SQL task and your DFT

Upvotes: 2

user985189
user985189

Reputation:

I believe your issue is around the method in which you are using to create the file. You have two alternatives and both should fix your issue:

Solution #1:

You can create an Excel file with those predefined columns, essentially your empty output file - this would act as your 'Template File'. Your flow would then be this:

  1. File System Task - Copy template file to output or working directory (rename if necessary)
  2. OLEDB Source Task - Query your source for the data (3000)
  3. Data Conversion Task
  4. Excel Destination Task - Put data into new Excel file

Note: You already have steps 2 thru 3 complete, you just need to make sure you are connecting to the new Excel file. Also, to clarify, step 1 is outside the Control Flow Task.

This way is helpful because you always have a blank and consistently formatted Excel file to copy and work with.

Solution #2:

The other option is to use a Script Task and create the Excel file - you could also load the data into the file in this task. This requires some basic understanding of VB.NET or C#. Basically you would need to get a XLS library (like NPOI). This is more complicated, but gives you the best functionality.

I recommend you try solution #1 and see how that works for you.

Upvotes: 24

Related Questions