Click here to Skip to main content
13,045,790 members (46,965 online)
Rate this:
Please Sign up or sign in to 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:
string attachment = "attachment; filename=Export.xls";
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
frm.Attributes["runat"] = "server";

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 10:01am
Updated 8-Jun-11 6:44am

1 solution

Rate this: bad
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="" >
    <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>
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2017
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