在将gridview数据导出到excell工作表的同时,如何在excell工作表的顶部提供标题 [英] how to give heading on top of excell sheet while exporting gridview data to excell sheet

查看:73
本文介绍了在将gridview数据导出到excell工作表的同时,如何在excell工作表的顶部提供标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我已将gridview数据导入到PDF中,但问题是我无法通过代码创建工作表的标题

下面是代码:

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();



我的代码正在100%输出Excel工作表.请告诉我如何添加标题.
提前致谢.



My code is working 100% to output the excel sheet. Please tell me how to add a heading.
Thanks in Advance.

推荐答案

总之,你不能.

从您的代码看来,您似乎是在运行时通过响应HTTP请求将电子表格传递给用户.您可以通过设置MIME Con​​tent-type标头来实现.这告诉客户的浏览器期望什么类型的文档;在这种情况下,使用Excel.这是客户端浏览器所期望的唯一文档,因此添加标头"的唯一方法是修改扩展名并在发送前向其添加标头.

另一种选择是使用iframe.外框将包含标题/标题,而内框将链接到上面的脚本:

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>


这篇关于在将gridview数据导出到excell工作表的同时,如何在excell工作表的顶部提供标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆