Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi All,

I have imported gridview data to a PDF but the problem is that I am unable to create a heading for the sheet through code

Below is the code:
C#
string attachment = "attachment; filename=Export.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
GridView1.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(GridView1);
frm.RenderControl(htw);

//GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();


My code is working 100% to output the excel sheet. Please tell me how to add a heading.
Thanks in Advance.
Posted
Updated 8-Jun-11 6:44am
v4

1 solution

In short, you can't.

From your code it looks like you are delivering a spreadsheet to the user at runtime by responding to an HTTP request. You are doing so by setting the MIME Content-type header. This tells the client's browser what type of document to expect; in this case, Excel. This is the only document the client's browser expects, so the only way of adding a "header" would be to modify the spreadsheel and add a header to it before it is sent.

Another option would be to use iframes. The outer frame would container the header/title and the inner frame would link to your script above:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Report</title>
</head>
<body>
    <h1>Report of best songs of all time</h1>
    <!-- This should point to your script above. -->  
    <iframe src="excel_report.aspx" width="100%" height="300px">
       <p>Your browser does not support iframes.</p>
     </iframe>
</body>
 
Share this answer
 
v2
Comments
nilu16 8-Jun-11 10:12am    
can u tell me how to use i frames here

i have not use it
Yvan Rodrigues 8-Jun-11 10:23am    
I've added an example to my solution. This will only work if the user's browser supports display of inline Office documents.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900