2017-09-07 9 views
0

Ce code fonctionne parfaitement sur ma machine (j'ai excel 2010) mais quand mon superviseur a essayé de courir mais ne fonctionne pas sur sa machine (il a excel 2016) donc pour excel 2016 dois-je changer de connexion ConStr = "Provider = Microsoft.ACE.OLEDB.12.0; Source de données =" + fileFullPath + "; Propriétés étendues = \" Excel 12.0; HDR = "+ HDR +"; IMEX = 0 \ ""; ?? Est-ce qu'il a reçu l'erreur, dit le fournisseur ACE n'est pas enregistré?Comment charger plusieurs feuilles de fichier excel (2016) dans ssis

string FolderPath = Dts.Variables["User::FolderPath"].Value.ToString(); 
     string TableName = Dts.Variables["User::TableName"].Value.ToString(); 
     string SchemaName = Dts.Variables["User::SchemaName"].Value.ToString(); 
     string SheetNameToLoad = Dts.Variables["User::SheetNameLike"].Value.ToString(); 

     var directory = new DirectoryInfo(FolderPath); 
     FileInfo[] files = directory.GetFiles(); 

     //Declare and initilize variables 
     string fileFullPath = ""; 


     SqlConnection myADONETConnection = new SqlConnection(); 
     myADONETConnection = (SqlConnection)(Dts.Connections["DBconnection"].AcquireConnection(Dts.Transaction) as SqlConnection); 

     ////Get one Book(Excel file at a time) 
     foreach (FileInfo file in files) 
     { 
      fileFullPath = FolderPath + "\\" + file.Name; 
      MessageBox.Show(fileFullPath); 

      // //Create Excel Connection 
      string ConStr; 
      string HDR; 
      HDR = "YES"; 
      ConStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileFullPath + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\""; 
      OleDbConnection cnn = new OleDbConnection(ConStr); 

     // //Get Sheet Name 
      cnn.Open(); 
      DataTable dtSheet = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 

      string sheetname; 
      sheetname = ""; 
      //Only read data from provided SheetNumber 

      foreach (DataRow drSheet in dtSheet.Rows) 
      { 



       sheetname = drSheet["TABLE_NAME"].ToString(); 
       MessageBox.Show(sheetname); 

       //Load the Data if Sheet Name contains value of SheetNameLike 
       if (sheetname.Contains(SheetNameToLoad) == true) 
       { 



        //Load the DataTable with Sheet Data so we can get the column header 
        OleDbCommand oconn = new OleDbCommand("select * from [" + sheetname + "] where CityName ='ARLINGTON'", cnn); 
        OleDbDataAdapter adp = new OleDbDataAdapter(oconn); 
        DataTable dt = new DataTable(); 
        adp.Fill(dt); 
        cnn.Close(); 



        //Load Data from DataTable to SQL Server Table. 
        using (SqlBulkCopy BC = new SqlBulkCopy(myADONETConnection)) 
        { 
         BC.DestinationTableName = SchemaName + "." + TableName; 

         BC.WriteToServer(dt); 
        } 

       } 
      } 

Répondre

1

Dans ce cas, votre superviseur a besoin de télécharger et installer sur sa machine:

https://www.microsoft.com/en-us/download/details.aspx?id=13255

+0

yup i déjà télécharger Microsoft Access Database Engine 2010 redistribuable sur sa machine en obtenant toujours la même erreur. Est-ce que ace.oledb.12 fonctionne encore pour Excel 2016? @ LONG – Rab

+0

pouvez-vous coller le message d'erreur complet? S'il est dit ACE.OLEDB.12.0, alors oui, je n'ai pas 2016 installé, mais si cela est dit, vous devez télécharger et installer la version correcte (32 bits ou 64 bits), puis redémarrer – LONG

+0

en obtenant que Exception a été lancé par la cible d'une invocation. mais quand j'ai vérifié sur sa machine il n'a pas eu ace.oledb.12.0 alors j'ai installé mais obtenant toujours la même erreur @LONG – Rab