在下面的代码中使用以读取excel表但是它将异常作为' Microsoft.ACE.OLEDB.12.0'提供商未在本地计算机上注册
从互联网上尝试了所有可能的解决方案,但没有一个正在运作
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;
}