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/
Related
How to fetch the Excel file path connection dynamically through a variable in the Excel Source?
Inside my Foreach Loop Container, I have Excel Source which has Excel Connection String.
I am using the variable to map the Incoming folder path. I set DelayValidation to True.
Folder Path : c:\IncomingPath\
However, I am getting an error as
The connection is not found.
We put the excel files by extracting the zip file. Steps are done through SSIS Package.
In the Foreach Loop make sure it's using the Foreach File Enumerator type if it's not already. Then on the Variable Mappings page, set a string variable to Index 0, this will hold the file name for each iteration of the loop. Then go to the Excel Connection Manager, click on the ellipsis next to the Expressions property (highlight the connection manager and press F4 to view the Properties window) and set the same string variable that was set at Index 0 in the loop as the expression for the ExcelFilePath, not ConnectionString, property. This will set the Excel Source component to use the current file from each iteration of the Foreach Loop.
How to dynamically set the DataFlowTask : Excel Source in SSIS Package?
I am loading over 100 excel files which has to be passed through Excel Source.
In the Property of Excel Source / OpenRowset , I have the set condition as
" Sheet2$A5:K "
I am able to set the Excel Source for one specific file. However, I am not able to set the source in a dynamic approach of loading 100 files in a folder.
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.
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.
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"