问题
I'm trying to load data from an excel file with a sheetname which is not static (sheetname contains yyyymmdd which would change with each file) into SQL database table. I followed the solution provided on How to loop through Excel files and load them into a database using SSIS package? but could only manage to get the first for loop working. When I'm trying to assign the user variable 'Sheetname' to Excel Source under the Data Flow task, I'm getting the error -
Error at CSSN_Invoice [Connection manager "TEST MKBS CONNECTION"]: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80004005. An OLE DB record is available. Source: "Microsoft Access Database Engine" Hresult: 0x80004005 Description: "Invalid argument.".
Error at Data Flow Task [MKBS Sheetname [1]]: SSIS Error Code DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER. The AcquireConnection method call to the connection manager "TEST MKBS CONNECTION" failed with error code 0xC0202009. There may be error messages posted before this with more information on why the AcquireConnection method call failed
The data flow task is working fine when a sheetname is picked as 'Table or View' and not as 'Table Name or View Name Variable'
Please help !
回答1:
Create a data flow task to read sheet names into ADO object.
First item is a script component as a source. I have a variable for connection string to the Excel Spreadsheet
Created an Output of SheetName
Here's the code to read tab names:
You are basically opening the spreadsheet with oleDB. Putting the table names into a data table
Looping through the data table and writing out the rows to output.
Make sure to close the Connection!!! This may cause errors later if you don't.
The next step is a conditional split as for some reason the result has duplicates of tab names and they all end in an '_'.
Next step is deriving a column to clean the sheet name of exta "'"
Create a Variable of type Object: I named mine ADO_Sheets
Insert a recordset destination object: 1. Set the variable to the variable you just created 2. Map the columns for clean Sheet
Now back to the Control Flow and set up a foreach loop control:
Configure the foreach... Enumerator: Foreach ADO Enumerator Source: ADO_Sheets Variable Mapping: Set to a variable called SheetName
I have a Function Task inside the loop but it is more for ease of understanding, it could have been down in the variables:
This variable is now your select for extracting the data off that page.
Last is the data flow task you want to run.
Lot's of work, but I use this so often I thought I would share!!!
来源:https://stackoverflow.com/questions/45097309/how-to-loop-through-excel-file-and-get-sheetname-using-ssis-2008