Data Import from SQL server to Excel sheet SSIS - sql

I'm working on a data export package where I move few data from sql to a excel sheet.
This package is automated to execute every weekend .
Unless i delete the previous excel file , I'm not able to run the package again as it throws the destination table is already present error.
Suggest me a way to retain the old excel file and create a new one everytime.
For example , if the earlier excel sheet was named export_1 , the next sheet on the subsequent week should be named export_2

Create 2 parameteres
File_Path
Connection_String
2.Use a Script task that creates an Excel File and Assign the values to
parameters "File_Path" and "Connection_String" based on the newly
created Excel file
Parameterize the 'Excel Connection'
ConnectionString --> Connection_String
ExcelFilePath --> File_Path

You can dynamically pass the file name through the expressions based on the Datetime.
Ex: Filepath+Date1_mmddyy_hhmmss
Every time the file generates, It will create a file with new file name and Excel file should be passed as an expression.

Related

Dynamic Excel Sheets load into SQL using SSIS

I'm trying to load multiple excel format files (.xlsx) into sql. I have set up my package as followed. My excel files name and sheet name will change daily
File name: PROD File Tracking 02-10.xlsx - Month and Day change daily
Sheet name: 2-10$ -- month and day change daily
Package Structure
For each Loop Container -> Data Flow Task -> Excel Source -> OLE DB Destination
Variables Values
FileName: Z:\Users\darsftp\BDS\GBRTest\PROD File Tracking 02-10.xlsx
FolderPath: Z:\Users\darsftp\BDS\GBRTest
ExtProperties: "Excel 12.0;HDR=Yes"
Need Help
To dynamically pick up file daily with a dynamic sheet name.
I know how to pick up files with a dynamic file name but not with a dynamic sheet name. That's where I'm having the issue.
Create a new variable Sheetname and Set it's property Evaluate as expression to True and use the following expression:
REPLACE(SUBSTRING(#[User::Filename],FINDSTRING(#[User::Filename],"PROD File Tracking", 1 ) + LEN("PROD File Tracking"),100),".xlsx","") + "$"
So if your Filename variable value its Z:\Users\darsftp\BDS\GBRTest\PROD File Tracking 02-10.xlsx so Sheetname variable value will be 02-10$ wich is the sheetname.
And in your Excel Source read the sheetname from variable Sheetname
Side note: Excel sheetname always end with $ sign (it doesn't appear in excel)
Read more about variable and expressions in this article
EDIT 1
You have to add an expression the the Excel connection manager connection string property:
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+ #[User::Filename] + ";Extended Properties=\"Excel 12.0;HDR=YES\";"
Set DelayValidation property of Data Flow task to True.
Usefull Links
SSIS with variable excel connection manager
Dynamically assign filename to excel connection string
https://www.google.com.lb/amp/s/sqlserverrider.wordpress.com/2013/01/07/dynamic-file-name-for-excel-connection-manager-ssis/amp/

DataTable.ImportSheet operation failed.Invalid file

I'm running Excel tests on UFT and sometimes I get the error number 20012 which is "DataTable.ImportSheet operation failed.Invalid file".
This is my way of importing the script:
DataTable.ImportSheet filepath,scriptname,"Action2"
filepath is the path of my workbook which conatins many excel sheets (scripts)
scriptname: the name of the script that I want to run
Action2: contains all the call of all possible keywords that may script can contains.
Any help please, why I'm getting this error.
The problem is that this is working well for some scripts and for others not after 3 or 4 run times.
I think the problem is on Excel itself and not on the code, are there any problems when working with Excel 2016 and UFT 12 ?
UFT syntax for importing a worksheet is:
DataTable.ImportSheet FileName, vtSrcSheet, vtDstSheet
This means you need to pass as parameters the filename (and path) to the excel file, the name (or index) of the source sheet you want to import, and then the destination you want this sheet to be (for example "Global" or "Action1" etc)
Unless scriptname happens to be the exact name of the worksheet you are trying to import you will get this error.
If you want to import the whole file use Datatable.Import instead of Datatable.ImportSheet

SSIS: Export to Dynamic Excel File

I am trying to create a project that will produce a separate excel file for every account in a specific table. The first steps to pull the data into a temp file and load an array variable with the accounts for the loop, this is working fine.
I am getting the 2 errors below. And when I execute the package it fails on the export to excel data flow task.
Error 1 Error loading RAW_DATA_EXPORT.dtsx: The connection string
format is not valid. It must consist of one or more components of the
form X=Y, separated by semicolons. This error occurs when a connection
string with zero components is set on database connection manager.
Error 2 Error loading RAW_DATA_EXPORT.dtsx: The result of the
expression ""\\server\DATA\Status\Testing\Filename" +
#[User::Accts] + ".xls"" on property "ConnectionString" cannot be
written to the property. The expression was evaluated, but cannot be
set on the property.
This is what my dynamic connection string looks like evaluated:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\server\DATA\Status\Testing\Filename03500.xls;Extended Properties="EXCEL 8.0;HDR=YES";
I think the issue is since the excel file are non-existent, the validation fails when it gets to the step. From what I found online I need to either create the sheets through a a script task (not fond of) or use a variable value to create the sheet in the file. I have not been successful on either.
(...) since the excel file are non-existent, the validation fails when it gets to the step.
Have you tried delaying validation in the Data Flow task that exports to Excel? Check Data Flow's properties window.

Delete top row of excel file using SSIS

I have an excel file that has a header row which is a row that I want to delete. The header row in thsi file are the cells of A1 to W1 merged into one. This causes a problem when I try to read the file because I am expecting column names. Proper column names exist in the second row of the file, which is why I want to delete the first.
To accomplish this I thought I'd be able to use the 'Excel Source' item in SSIS since it supports a SQL option to write a query. What I want to do is something like this:
SELECT * from ExcelFile WHERE Row > 1
My file only has data in columns A thru W.
I don't know what syntax I can use in the query to do this. The query builder that is in the Excel Source item will allow me to do many things with columns but I don't see an option for doing anything with rows. Searching online and using the help didn't get me anywhere.
None of these solutions will work because the Excel driver will be confused by the merged first line. You won't be able to use any driver features such as skip first row to do this. You need to run some script to open the Excel file and delete the row manually.
There is some basic sample script at this site:
http://www.sqlservercentral.com/Forums/Topic1327014-1292-1.aspx
The code below is adapted from the code written by snsingh at that site.
You would obviously want to use connnection manager properties, not hard coded paths
Excel needs to be installed on the SSIS Server for it to work - this is the only way to use Excel automation.
Dim filename As String
Dim appExcel As Object
Dim newBook As Object
Dim oSheet1 As Object
appExcel = CreateObject("Excel.Application")
filename = "C:\test.xls"
appExcel.DisplayAlerts = False
newBook = appExcel.Workbooks.Open(filename)
oSheet1 = newBook.worksheets("Sheet1")
oSheet1.Range("A1").Entirerow.Delete()
newBook.SaveAs(filename, FileFormat:=56)
appExcel.Workbooks.Close()
appExcel.Quit()
You don't need to use a syntax.
Go to control flow..
Pull in a data flow task.
Add a excel file source...add a conection manager
With excel sheet.
Open your connection manager and then check the box which says.
Column names In first row. That's it and add ur destination.

SSIS Dynamic Excel Destination File Name

How can I configure a dataflow task that takes data from a MS SQL Server 2008 datasource and puts it in an Excel file where the filename looks like 'date filename'.xls?
Excel is the biggest pain to deal with in SSIS. Usually I store a template file that just has the column headers and nothing else. I start with a task to copy the template file to the processing directory. You can use variables to create the file name in an expression at this point. Alternatively, you can create the file in the dataflow and then rename the file in a step after the data flow. With text files, I have dynamically created the connection in an expression, but Excel seems to be funny about that.
Provided that your column definition don't change.... you can go to
Right Click on Excel Connection Manager
Expression
Select connectionstring
bulid expression (for Example : (DT_WSTR, 50) GETDATE() + #[user::FileName] +".xlsx")
Select the properties for Excel Connection Manager instance, Click on the ellipsis for 'Expressions 'property and set an expression for 'ExcelFilePath' to a variable with a valid path to an excel file, this takes cares of the connection string.
You do need a variable valid excel file at the design time, otherwise connection manager does not work, you can overwrite it at run time using a script task to point to the excel file that does not exist at design time.
HLGEM's answer certainly helps. As #sql-rookie requested, I'll provide a sample about how to copy the excel file to a new file named with current date. Hope this will help others with the same question in the future.
Basically you just need to add an additional File System Task after the Data Flow Task. And use a variable for the Destination File Path: "\\\\file\\"+SUBSTRING((DT_STR,30, 1252) GETDATE(), 1, 10) +".xlsx"