converting gridview to excel in asp.net
-
hi i am using following code to export gridview to excel..but the generated excel file does not contain rows and columns as normal excel file ... can any on e suggest a solution for that here is my code StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); String savefile = heading; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + savefile + ".xls"); HttpContext.Current.Response.Charset = ""; gv.RenderControl(htw); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End();
-
hi i am using following code to export gridview to excel..but the generated excel file does not contain rows and columns as normal excel file ... can any on e suggest a solution for that here is my code StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); String savefile = heading; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + savefile + ".xls"); HttpContext.Current.Response.Charset = ""; gv.RenderControl(htw); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End();
use the below code in you ExporttoExcel function
using conn as new SqlConnection(Constring)
conn.open()
da=new sqlDataAdapter(query,conn)
da.fill(ds)
End UsingDim Gridview1 as New Gridview
Gridview1.datasource=ds
Gridview1.databind()
Response.clear()
Response.Buffer=true
Response.AddHeader("content-disposition","attachment;filename=testfile.xls")
Response.charset=""
Response.ContentType="application/vdn.ms-excel"
Dim sw as new StringWriter()Dim hw as new HtmlTextWriter(sw)
For i as integer=0 To Gridview1.Rows.Count-1
Gridview1.Row(i).Attribute.Add("class","textmode")
Next
Gridview1.RenderControl(hw)dim style as String="<style> .textmode(mso-number-format:\@;}</style>"
Response.write(style)
Response.output.write(sw.ToString())
Response.Flush()
Response.End()Imtiaz A.K
-
use the below code in you ExporttoExcel function
using conn as new SqlConnection(Constring)
conn.open()
da=new sqlDataAdapter(query,conn)
da.fill(ds)
End UsingDim Gridview1 as New Gridview
Gridview1.datasource=ds
Gridview1.databind()
Response.clear()
Response.Buffer=true
Response.AddHeader("content-disposition","attachment;filename=testfile.xls")
Response.charset=""
Response.ContentType="application/vdn.ms-excel"
Dim sw as new StringWriter()Dim hw as new HtmlTextWriter(sw)
For i as integer=0 To Gridview1.Rows.Count-1
Gridview1.Row(i).Attribute.Add("class","textmode")
Next
Gridview1.RenderControl(hw)dim style as String="<style> .textmode(mso-number-format:\@;}</style>"
Response.write(style)
Response.output.write(sw.ToString())
Response.Flush()
Response.End()Imtiaz A.K
-
It is in Asp.Net only... When u write code in ASP.net, it means you have to choose the language you prefer to code either vb.net or C# etc.. the code which i have send is used in asp.net application only... and if u are using the C# as code behind then.. convert this piece of code into c# as we have many online converters... paste the code they will give converted format in C#... Regards Imtiaz
Imtiaz A.K
-
thanx it works as it is I previously return code for this........
-
hi i am using following code to export gridview to excel..but the generated excel file does not contain rows and columns as normal excel file ... can any on e suggest a solution for that here is my code StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); String savefile = heading; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + savefile + ".xls"); HttpContext.Current.Response.Charset = ""; gv.RenderControl(htw); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End();
Have you tried earlier solutions?
-
Have you tried earlier solutions?
yes, I tried this one.