excel sheet
-
The below code is for saving the database table data into excel sheet. private void Button1_Click(object sender, System.EventArgs e) { BindData(); Response.Clear(); Response.Buffer= true; Response.ContentType = "application/vnd.ms-excel"; Response.Charset = ""; this.EnableViewState = false; Response.AddHeader("content-disposition","attachment;filename=Emp.xls"); System.IO.StringWriter stringwrite=new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlwrite=new HtmlTextWriter(stringwrite); DataGrid1.RenderControl(htmlwrite); Response.Write(stringwrite.ToString()); Response.End(); } private void BindData() { oDA=new OleDbDataAdapter("select * from Emp",sqlconn); ds=new DataSet(); oDA.Fill(ds); DataGrid1.DataSource=ds; DataGrid1.DataBind(); } ----------------------------------------------------------------------------- The below code is for reading saved excel data private void Button2_Click(object sender, System.EventArgs e) { OleDbConnection con=new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:\\EmpSai.xls;Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\""); con.Open(); Response.Write("success"); con.Close(); } i am getting error at button2_click() event at con.open(); the error is: Exception Details: System.Data.OleDb.OleDbException: External table is not in the expected format.
-
The below code is for saving the database table data into excel sheet. private void Button1_Click(object sender, System.EventArgs e) { BindData(); Response.Clear(); Response.Buffer= true; Response.ContentType = "application/vnd.ms-excel"; Response.Charset = ""; this.EnableViewState = false; Response.AddHeader("content-disposition","attachment;filename=Emp.xls"); System.IO.StringWriter stringwrite=new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlwrite=new HtmlTextWriter(stringwrite); DataGrid1.RenderControl(htmlwrite); Response.Write(stringwrite.ToString()); Response.End(); } private void BindData() { oDA=new OleDbDataAdapter("select * from Emp",sqlconn); ds=new DataSet(); oDA.Fill(ds); DataGrid1.DataSource=ds; DataGrid1.DataBind(); } ----------------------------------------------------------------------------- The below code is for reading saved excel data private void Button2_Click(object sender, System.EventArgs e) { OleDbConnection con=new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:\\EmpSai.xls;Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\""); con.Open(); Response.Write("success"); con.Close(); } i am getting error at button2_click() event at con.open(); the error is: Exception Details: System.Data.OleDb.OleDbException: External table is not in the expected format.
Hi, In button_1 you are dealing with Emp.xls and in Button_2 you are trying to open EmpSai.xls! is this your simple mistake? Hope this helps. Difficult - > Challenging, this simple replacement made me take my life little easy;)