Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
gvMaster.AllowPaging = false;
gvMaster.RenderControl(hw);
//write the output
Response.Output.Write(header.ToString());
Response.Output.Write("Some title\n");
Response.Output.Write("\n");
//to write current date and time
System.DateTime tDate1 = System.DateTime.Now;
Response.Output.Write(tDate1);
Response.Output.Write("\n");
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
The problem is when I add header next Line is not working and the Title and system time are all in one row,
My question is how can I write the gridview to specific row in output excel or how can I add some line on top of gridview and also how can I add some style to title?
try to write the title and date in <table> tag. it will display in separate line.
Response.Output.Write(header.ToString()); Response.Output.Write(<table border=\"0\"><tbody><tr><td>");
Response.Output.Write("Some title"); Response.Output.Write("</td></tr><tr><td>");
//to write current date and time
System.DateTime tDate1 = System.DateTime.Now;
Response.Output.Write(tDate1); Response.Output.Write("</td></tr></tbody></table>");
arny1
Member
27 Points
133 Posts
Export Gridview to Excel Problem
Jul 24, 2009 08:05 AM|LINK
Hi There,
I had this post before,but I have some problems when I add a header for specific charcter set,and also add some additional data,
here is my code;
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=aname.xls");
string header = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<title></title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1254\" />\n<style>\n</style>\n</head>\n<body>\n";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1254");
Response.Charset = "windows-1254"; //ISO-8859-9 windows-1254
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
gvMaster.AllowPaging = false;
gvMaster.RenderControl(hw);
//write the output
Response.Output.Write(header.ToString());
Response.Output.Write("Some title\n");
Response.Output.Write("\n");
//to write current date and time
System.DateTime tDate1 = System.DateTime.Now;
Response.Output.Write(tDate1);
Response.Output.Write("\n");
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
The problem is when I add header next Line is not working and the Title and system time are all in one row,
My question is how can I write the gridview to specific row in output excel or how can I add some line on top of gridview and also how can I add some style to title?
Thanks in advance
Maate
Participant
1525 Points
248 Posts
Re: Export Gridview to Excel Problem
Jul 24, 2009 08:59 AM|LINK
I haven't tested it, but your html is invalid, when you add the header, so perhaps it will help adding this to your response stream:
Response.output.Write("\n</body>\n</html>");Right before your Response.Flush(); code.
Br. Morten
vinz
All-Star
126946 Points
17922 Posts
MVP
Re: Export Gridview to Excel Problem
Jul 24, 2009 09:01 AM|LINK
Try to check these articles:
Export GridView To Word/Excel/PDF/CSV in ASP.Net
Export GridView with Images from database to Word, Excel and PDF Formats
Export GridView with Images to Word, Excel and PDF Formats in ASP.Net
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
Maate
Participant
1525 Points
248 Posts
Re: Export Gridview to Excel Problem
Jul 24, 2009 09:01 AM|LINK
If this does not help, also you could try encapsulating your title and date like this:
Response.Output.Write("<p>Some title</p>\n");
paaresh
Contributor
2104 Points
311 Posts
Re: Export Gridview to Excel Problem
Jul 24, 2009 09:11 AM|LINK
Hi,
try to write the title and date in <table> tag. it will display in separate line.
Response.Output.Write(header.ToString());
Response.Output.Write(<table border=\"0\"><tbody><tr><td>");
Response.Output.Write("Some title");
Response.Output.Write("</td></tr><tr><td>");
//to write current date and time
System.DateTime tDate1 = System.DateTime.Now;
Response.Output.Write(tDate1);
Response.Output.Write("</td></tr></tbody></table>");
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
[Ahmedabad]
|| Blog ||
arny1
Member
27 Points
133 Posts
Re: Export Gridview to Excel Problem
Jul 24, 2009 10:27 AM|LINK
Thank you Maate & Paaresh
You both are exactly Right,
Regards