in my application im is exporting gridview data to an excel sheet, now I want to save this sheet in the folder that I created on my machine, how can I do this
I wrote this code
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { public override void VerifyRenderingInServerForm(Control control) { } private void ExportToExcel(string strFileName, GridView dg) { Response.Clear(); Response.Buffer = true; Response.ContentType = "application/vnd.ms-excel"; Response.Charset = ""; this.EnableViewState = false; System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); oHtmlTextWriter.WriteLine("<b><u><font size='5'><font color='blue'><center> REPORT </center></font></u></b>"); GridView1.RenderControl(oHtmlTextWriter); Response.End(); } protected void Page_Load(object sender, EventArgs e) { } protected void Button2_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(""); con.Open(); int var = DropDownList1.SelectedIndex; switch (var) { case 0: break; case 1: SqlCommand cmdd = new SqlCommand("update t1 set diff1=isnull(t1.date-t2.date,0)from reporttemp t1 left join reporttemp t2 on t1.rn=t2.rn+1", con); SqlCommand cmdd1 = new SqlCommand("update t1 set diff2=isnull(t1.date-t2.date,0)from reportpre t1 left join reportpre t2 on t1.rn=t2.rn+1", con); SqlCommand cmdd2 = new SqlCommand("update t1 set diff3=isnull(t1.date-t2.date,0)from reportph t1 left join reportph t2 on t1.rn=t2.rn+1", con); cmdd.ExecuteNonQuery(); cmdd1.ExecuteNonQuery(); cmdd2.ExecuteNonQuery(); GridView1.Visible = true; DataTable dt = new DataTable(); SqlCommand cmd = new SqlCommand("select Date,Temperature from reporttemp where datepart(minute,diff1)=5", con); SqlDataAdapter da = new SqlDataAdapter(cmd); SqlCommand cmd2 = new SqlCommand("select Date,Pressure from reportpre where datepart(minute,diff2)=5", con); SqlDataAdapter da2 = new SqlDataAdapter(cmd2); SqlCommand cmd3 = new SqlCommand("select Date,Ph from reportph where datepart(minute,diff3)=5", con); SqlDataAdapter da3 = new SqlDataAdapter(cmd3); da.Fill(dt); da2.Fill(dt); da3.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); break; case 2: GridView1.Visible = true; SqlCommand cmd4 = new SqlCommand("update t1 set diff1=isnull(t1.date-t2.date,0)from reporttemp t1 left join reporttemp t2 on t1.rn=t2.rn+1", con); SqlCommand cmd1 = new SqlCommand("update t1 set diff2=isnull(t1.date-t2.date,0)from reportpre t1 left join reportpre t2 on t1.rn=t2.rn+1", con); SqlCommand cmd5 = new SqlCommand("update t1 set diff3=isnull(t1.date-t2.date,0)from reportph t1 left join reportph t2 on t1.rn=t2.rn+1", con); cmd4.ExecuteNonQuery(); cmd1.ExecuteNonQuery(); cmd5.ExecuteNonQuery(); GridView1.Visible = true; DataTable dt1 = new DataTable(); SqlCommand cmd6 = new SqlCommand("select Date,Temperature from reporttemp where datepart(minute,diff1)=2 ", con); SqlDataAdapter daa = new SqlDataAdapter(cmd6); SqlCommand cmd7 = new SqlCommand("select Date,Pressure from reportpre where datepart(minute,diff2)=2", con); SqlDataAdapter daa2 = new SqlDataAdapter(cmd7); SqlCommand cmd8 = new SqlCommand("select Date,Ph from reportph where datepart(minute,diff3)=2", con); SqlDataAdapter daa3 = new SqlDataAdapter(cmd8); daa.Fill(dt1); daa2.Fill(dt1); daa3.Fill(dt1); GridView1.DataSource = dt1; GridView1.DataBind(); break; } } protected void Button3_Click1(object sender, EventArgs e) { ExportToExcel("Report.xls", GridView1); } }
can any1 help me with this
source share