Proceso para extraer datos de un componente de tipo Script: Extraer datos de Excel con un componente script
Ejemplo de como lograr extraer datos de multiples hojas de un archivo excel mediante SSIS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
#region Namespaces using System; using System.Data; using System.Data.OleDb; #endregion //********************************* www.SQL-Listo.com **************************************// [Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute] public class ScriptMain : UserComponent { // Declaramos los dataset y data table publico para poder utilizarlos en la salida del componente// DataSet ds = new DataSet(); DataTable dt; //********************************* www.SQL-Listo.com **************************************// public override void PreExecute() { //Conectamos y llenamos los DataTable// base.PreExecute(); string filename = Variables.FilePath; System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection( "Provider=Microsoft.ACE.OLEDB.12.0; data source='" + filename + "';" + "Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\" "); myConnection.Open(); DataTable mySheets = myConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); for (int i = 0; i < mySheets.Rows.Count; i++) { dt = MakeDataTableFromSheetName(filename, mySheets.Rows[i]["TABLE_NAME"].ToString()); ds.Tables.Add(dt); } } //********************************* Hacemos un DataTable por hoja**************************************// private static DataTable MakeDataTableFromSheetName(string filename, string sheetName) { System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection( "Provider=Microsoft.ACE.OLEDB.12.0; " + "data source='" + filename + "';" + "Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\" "); DataTable dtImport = new DataTable(); System.Data.OleDb.OleDbDataAdapter myImportCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [" + sheetName + "]", myConnection); myImportCommand.Fill(dtImport); //Llenamos el DataTable dtImport el cual se retorna dtImport.TableName = sheetName;// Aqui le damos el nombre de la hoja al DataTable return dtImport; } public override void PostExecute() { base.PostExecute(); } //Crearemos las salidas de cada DataTable (Cada datatable contiene una hoja del excel) public override void CreateNewOutputRows() { // Debemos llamar en cada foreEch cada hoja que necesitemos en la salida, importante colocar // colocar el signo dolar $ al final del nombre de la hoja // foreach por cada hoja que tenga el archivo excel foreach (DataRow row in ds.Tables["Hoja1$"].Rows) { //Aqui colocamos el nombre del objeto que creamos afuera this.Hoja1Buffer.AddRow();//Agregamos registro por cada item que tenga el data table this.Hoja1Buffer.A = Convert.ToInt32(row[0].ToString()); this.Hoja1Buffer.B = Convert.ToInt32(row[1].ToString()); } foreach (DataRow row in ds.Tables["Hoja2$"].Rows) { this.Hoja2Buffer.AddRow(); this.Hoja2Buffer.A = Convert.ToInt32(row[0].ToString()); this.Hoja2Buffer.B = Convert.ToInt32(row[1].ToString()); } } } |