amit shukla

amit shukla

  • NA
  • 78
  • 64.9k

Insert data in excel from c#.

Nov 27 2017 4:25 AM
Hi Friends,
 
I want insert some records in excel sheet using c# but it gives error like this
 
"The 'Microsoft.ACE.OLEDB.16.0' provider is not registered on the local machine"
 
i am using office 2016 and windos 10 with 64 bit.
and my code is below
 
 
 
string ConStr = "";
//getting the path of the file
string path = Server.MapPath("InsertDataExcel.xlsx");
//connection string for that file which extantion is .xlsx
ConStr = "Provider=Microsoft.ACE.OLEDB.16.0;Data Source=" + path + ";Extended Properties=\"Excel 16.0;ReadOnly=False;HDR=Yes;\"";
//making query
string query = "INSERT INTO [Sheet1$] ([Name], [Email], [MobileNo], [Location], [Qualification]) VALUES('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "')";
//Providing connection
OleDbConnection conn = new OleDbConnection(ConStr);
//checking that connection state is closed or not if closed the
//open the connection
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
//create command object
OleDbCommand cmd = new OleDbCommand(query, conn);
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
Response.Write("<script>alert('Sucessfully Data Inserted Into Excel')</script>");
}
else
{
Response.Write("<script>alert('Sorry!\n Insertion Failed')</script>");
}
conn.Close();
 
So please suggest what am i doing mistake.
 
Thanks 

Answers (3)