无法导出到Excel [英] unable to export to excel

查看:235
本文介绍了无法导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码从Gridview控件中的数据导出到excel.

I am using following code for export to excel from a data from a Gridview control.

lblError.Visible = false;
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=BorrowerReport.xls");
Response.Charset = "";
// If you want the option to open the Excel file without saving than
// comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
StringWriter stringWrite = new System.IO.StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
datagrid.RenderControl(htmlWrite);
            
Response.Write(stringWrite.ToString());
Response.End();


但是现在添加一个linkbutton作为该控件的项目模板.然后显示错误为

类型为"LinkBut​​ton"的控件"ctl00_Contentplaceholder2_grdBorrowerMaster_ctl02_LinkBut​​ton1"必须放置在带有runat = server的表单标记中.

那么如何导出?


But now add a linkbutton as item template of this control .Then show a error as

Control ''ctl00_Contentplaceholder2_grdBorrowerMaster_ctl02_LinkButton1'' of type ''LinkButton'' must be placed inside a form tag with runat=server.

So How can I export?

推荐答案

尝试在您的代码后面添加以下代码:

Try adding below code in your code behind :

public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
{
}


问题似乎与渲染有关.下面的文章讨论了类似的问题.看看有关渲染html部分的内容,您可能会有所了解. br/>
Looks like the issue is with rendering.This below article discuss on similar issue.Have a look at this on rendering the html part,you may get some idea.

http://dotnetforyou.wordpress.com/2011/01/23/dynamically-loading-usercontrol-in-asp-net-page-resulting-exception-control-button1-of-type-button-must-be-placed-inside-a-form-tag-with-runatserver/[^]

This discuss on loading UserControl,replace that with DataGrid & try.


尝试一下,
http://toniyojackson.blogspot.com/2010/10/gridview-export-to- excel.html [ ^ ]
Try this,
http://toniyojackson.blogspot.com/2010/10/gridview-export-to-excel.html[^]


这篇关于无法导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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