-2
以下代碼用於讀取excel表但它提供了異常作爲'Microsoft.ACE.OLEDB.12.0'提供程序未在本地計算機上註冊如何將excel表綁定到ASP.NET C#中的級聯下拉列表中#
嘗試從互聯網上所有可能的解決方案,但沒有工作
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;
}
你嘗試過這麼遠嗎?從字面上看,有很多例子可以開始。一個會是這樣的:https://support.microsoft.com/en-us/kb/976156 – LocEngineer
@LocEngineer我已經使用上面的代碼,但不工作 –
看看我鏈接的文章。每當下拉值發生變化時,您都不希望完整的回傳。你想要的是一個AJAX調用來獲取數據,而不必重新加載頁面(並失去你的組合索引)。考慮到你的OLEDB錯誤,試試這個: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-the-local-machine?forum = vstsdb – LocEngineer