Shaik Vahid

Shaik Vahid

  • 1.6k
  • 124
  • 5.7k

import .xls file to mysql database

Jan 2 2018 12:08 AM
protected void btnExcel_Click(object sender, EventArgs e)
{
string connString = "";
string strFileType = Path.GetExtension(FileUpload1.FileName).ToLower();
string path =Server.MapPath(FileUpload1.PostedFile.FileName);
//Connection String to Excel Workbook
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
// FileUpload1.SaveAs(Server.MapPath(FileUpload1.FileName));
string query = "SELECT * FROM [UserDetails]";
OleDbConnection conn = new OleDbConnection(connString);
if (conn.State == ConnectionState.Closed)
conn.Open();
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
// string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
// dt=objBLHR.Read_Sheet(filename);
gd1.DataSource = ds.Tables[0];
gd1.DataBind();
}
}
 
 
 
How to avoid this error from the page. 

Answers (5)