public static string OledbConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
string location = Server.MapPath("~/Excel_file/") + ConfigurationManager.AppSettings["filename"].ToString().Trim();
GridView1.DataSource = GetExcelData(location);
GridView1.DataBind();
}
public DataTable GetExcelData(string location)
{
OledbConnectionString = string.Empty;
string ext = Path.GetExtension(ConfigurationManager.AppSettings["filename"].ToString().Trim());
if(ext==".xls")
{
OledbConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+location+";Extended Properties=Excel 8.0;";
}
else if(ext==".xlsx")
{
OledbConnectionString = "Microsoft.Jet.OLEDB.12.0;Data Source=" + location + ";Extended Properties=Excel 12.0;";
}
else
{
}
OleDbConnection objConn = null;
objConn = new OleDbConnection(OledbConnectionString);
if (objConn.State == ConnectionState.Closed)
{
objConn.Open();
}
OleDbCommand objCmdSelect = new OleDbCommand("Select * from [Sheet1$] where Orderid=1", objConn);
OleDbDataAdapter objAdapter = new OleDbDataAdapter();
objAdapter.SelectCommand = objCmdSelect;
DataSet objDataset = new DataSet();
objAdapter.Fill(objDataset, "excel");
objConn.Close();
return objDataset.Tables[0];
}
ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/