2016-05-03 3 views
-2

Verwendet unter Code zum Lesen Excel-Blatt Aber es gibt Ausnahme als Die 'Microsoft.ACE.OLEDB.12.0' Anbieter ist nicht auf dem lokalen Computer registriertSo binden Sie Excel-Blatt an kaskadierte Dropdown-Liste in ASP.NET C#

alle mögliche Lösung aus dem Internet ausprobiert, aber keine für mich

protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!IsPostBack) 
      { 
       DataSet temp_ds = new DataSet(); 
       temp_ds = ReadExcelFile(); 

       CheckBoxList1.DataSource = temp_ds.Tables[0]; 
       CheckBoxList1.DataBind(); 
      } 


     } 


     protected void Bindxml_To_chkboxlist(object sender, EventArgs e) 
     { 
      string filepath = Server.MapPath("Cust_input1.xml"); 
      using(DataSet DS = new DataSet()) 
      { 

       DS.ReadXml(filepath); 

       CheckBoxList1.DataSource = DS; 
       CheckBoxList1.DataTextField = "CustomerName"; 
       CheckBoxList1.DataBind(); 


      } 
     } 


     protected void Button1_Click(object sender, EventArgs e) 
     { 
      // Response.Redirect("Add New WOS Customer.aspx"); 
     } 




     protected void CheckBoxList1_SelectedIndexChanged1(object sender, EventArgs e) 
     { 

     } 

     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
     { 

     } 

     private string GetConnectionString() 
     { 
      Dictionary<string, string> props = new Dictionary<string, string>(); 

      // XLSX - Excel 2007, 2010, 2012, 2013 
      props["Provider"] = "Microsoft.ACE.OLEDB.14.0"; 
      props["Extended Properties"] = "Excel 14.0 XML"; 
      props["IMEX"] = "1"; 
      props["Data Source"] = @"C:\\Users\\amar.kate\\Documents\\Visual Studio 2015\\Projects\\WebApplication5\\WebApplication5\\input\\input.xlsx"; 

      // XLS - Excel 2003 and Older 
      //props["Provider"] = "Microsoft.Jet.OLEDB.4.0"; 
      //props["Extended Properties"] = "Excel 8.0"; 
      //props["Data Source"] = "C:\\Users\\amar.kate\\Documents\\Visual Studio 2015\\Projects\\WebApplication5\\WebApplication5\\input\\input.xls"; 

      StringBuilder sb = new StringBuilder(); 

      foreach (KeyValuePair<string, string> prop in props) 
      { 
       sb.Append(prop.Key); 
       sb.Append('='); 
       sb.Append(prop.Value); 
       sb.Append(';'); 
      } 

      return sb.ToString(); 
     } 

     private DataSet ReadExcelFile() 
     { 
      DataSet ds = new DataSet(); 

      string connectionString = GetConnectionString(); 

      using (OleDbConnection conn = new OleDbConnection(connectionString)) 
      { 
       conn.Open(); 
       OleDbCommand cmd = new OleDbCommand(); 
       cmd.Connection = conn; 
       DataTable dtSheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
       // loop through sheet to get col name 
       foreach (DataRow dr in dtSheet.Rows) 
       { 
        string sheetName = dr["Sheet1"].ToString(); 
        if (!sheetName.EndsWith("$")) 
         continue; 

        cmd.CommandText = "SELECT * FROM [" + sheetName + "]"; 
        Response.Write(cmd.CommandText); 
        DataTable dt = new DataTable(); 
        dt.TableName = sheetName; 

        OleDbDataAdapter da = new OleDbDataAdapter(cmd); 
        da.Fill(dt); 

        ds.Tables.Add(dt); 

       } 

       cmd = null; 
       //conn.Close(); 

      } 

      return ds; 
     } 
+0

Was haben Sie versucht, so weit zu arbeiten? Es gibt buchstäblich Tonnen von Beispielen da draußen, um einen Anfang zu bekommen. Einer wäre das: https://support.microsoft.com/en-us/kb/976156 – LocEngineer

+0

@LocEngineer Ich habe oben genannten Code aber nicht funktioniert –

+0

Werfen Sie einen guten Blick auf den Artikel, den ich verlinkt. Sie möchten nicht jedes Mal, wenn sich der Dropdown-Wert geändert hat, ein vollständiges Post-Back erhalten. Was Sie wollen, ist ein AJAX-Aufruf, um die Daten zu erhalten, ohne dass Sie die Seite neu laden müssen (und Ihren Combo-Index verlieren). Berücksichtigen Sie Ihren OLEDB-Fehler: https://social.msdn.microsoft.com/Forums/en-US/1d5c04c7-157f-4955-a14b-41d912d50a64/how-to-fix-error-the-microsoftaceoledb120-provider- is-not-registered-on-the-local-machine? Forum = vstsdb – LocEngineer

Antwort

Verwandte Themen