Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 ASP.NET
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:
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 7-Jun-11 11:01am
nilu16524
Edited 8-Jun-11 7:44am
v4

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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>
  Permalink  
v2
Comments
nilu16 at 8-Jun-11 10:12am
   
can u tell me how to use i frames here
 
i have not use it
Yvan Rodrigues at 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)

  Print Answers RSS
0 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Shweta N Mishra 216
3 Maciej Los 210
4 PIEBALDconsult 184
0 OriginalGriff 7,630
1 Sergey Alexandrovich Kryukov 7,022
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,525


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100