Click here to Skip to main content
11,921,316 members (62,097 online)
Rate this:
Please Sign up or sign in to vote.
See more: C#2.0 C# 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.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 11:01am
Edited 8-Jun-11 7: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
0 OriginalGriff 583
1 Dave Kreskowiak 245
2 phil.o 160
3 KrunalRohit 134
4 F-ES Sitecore 130
0 OriginalGriff 6,951
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,171
3 George Jonsson 2,840
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web02 | 2.8.151120.1 | Last Updated 6 Nov 2012
Copyright © CodeProject, 1999-2015
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