Monday, January 7, 2013

Excel retrieve sheet by sheet

Excel connection:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Source\Product\20081202_Product.xls;Extended Properties="EXCEL 8.0;HDR=NO";

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+@[User::filenameProduct]+";Extended Properties=\"Excel 8.0;HDR=NO\";"

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\assignment related\Source\Product\20081202_Product.xls;Extended Properties="Excel 8.0;HDR=YES";



  public void Main()
        {
            ////Microsoft.SqlServer.Dts.Runtime.ConnectionManager cm = Dts.Connections["HS_Incidents"];
            ////IDTSConnectionManagerDatabaseParameters100 cmParams = cm.InnerObject as IDTSConnectionManagerDatabaseParameters100;
            ////OleDbConnection conn = cmParams.GetConnectionForSchema() as OleDbConnection;
            ////conn.Close();
         
            string excelFile;
            string connectionString;
            OleDbConnection excelConnection;
            DataTable tablesInFile;
            int tableCount = 0;
            string currentTable;
            int tableIndex = 0;

            excelFile = Dts.Variables["FileSource"].Value.ToString();
            connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                "Data Source=" + excelFile + ";Extended Properties=Excel 8.0";
            excelConnection = new OleDbConnection(connectionString);
            excelConnection.Open();
            tablesInFile = excelConnection.GetSchema("Tables");
            tableCount = tablesInFile.Rows.Count;

            string[] excelTables = new string[tableCount];
         
            foreach (DataRow tableInFile in tablesInFile.Rows)
            {
                currentTable = tableInFile["TABLE_NAME"].ToString();
                //GetRowsOfCurrentTable(currentTable, excelConnection.GetOleDbSchemaTable(
                excelTables[tableIndex] = "Select [" + currentTable + "].* From " + "["+ currentTable + "]";
                tableIndex += 1;
            }
            excelConnection.Close();
            //System.IO.FileInfo x = new System.IO.FileInfo(Dts.Variables["FileSource"].Value.ToString() );
            //x.Delete();
            //System.IO.File.Move(excelFile, Dts.Variables["ValidSourceFile"].Value.ToString());
            Dts.Variables["ExcelTables"].Value = excelTables;

            Dts.TaskResult = (int)ScriptResults.Success;

        }
    }

No comments:

Post a Comment