asp.net导出EXCEL的功能代码
asp.net导出EXCEL的功能代码
发布时间:2016-12-29 来源:查字典编辑
摘要:复制代码代码如下://由gridviw导出为ExcelpublicstaticvoidToExcel(System.Web.UI.Contr...

复制代码 代码如下:

//由gridviw导出为Excel

public static void ToExcel(System.Web.UI.Control ctl)

{

HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");

HttpContext.Current.Response.Charset = "UTF-8";

HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;

HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword

ctl.Page.EnableViewState = false;

System.IO.StringWriter tw = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

ctl.RenderControl(hw);

HttpContext.Current.Response.Write(tw.ToString());

HttpContext.Current.Response.End();

}

这是一段网上很多人使用的导出EXCEL代码,但使用过程中发现很多不足的地方,其不用引入其它控件。

缺点:

1、我采用ASPNETPAGER分页时只能导出第一页。

2、使用十分不灵活,对样式的控制,字段的控制不灵活。

使用中需要注意:

1、<%@PageLanguage="C#"AutoEventWireup="true"EnableEventValidation="false"CodeFile="Default.aspx.cs"Inherits="_Default"%>

加上这句。

2、在后台文件中加上:

publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)

{

}

3、需引入usingSystem.IO;

其它相关文章:

ASP.NET操作Excel备忘录

ASP.net连接Excel的代码

asp.net 读取并显示excel数据的实现代码

在Asp.net用C#建立动态Excel

asp.net 操作excel的实现代码

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新asp.net教程学习
热门asp.net教程学习
编程开发子分类