C#(asp.net)实现数据导出Excel表详细代码

C#(asp.net)实现数据导出Excel表详细代码
2010-6-25 22:57:32 来源:25亿CMS系统
本例介绍的用C#(asp.net)实现数据导出Excel详细代码的方法,会输出标准的Excel格式文件,非常稳定,不会死锁Excel进程,支持中文文件名,支持表头导出,支持大多数数据库导入。

C#(asp.net)数据导出Excel实现算法:利用Excel组件将DataGrid控件内容生成Excel临时文件,并存放在服务器上,然后用Response方法将生成的Excel文件下载到客户端然后再将生成的临时文件删除。

25亿CMS系统v2.9实现此功能,大家可以25亿CMS系统后台查看并研究。

C#(asp.net)数据导出Excel具体步骤:
1,在项目中引用Excel组件(Interop.Excel.dll),如需此组件,可以到技术社区(http://bbs.25yi.com)索取,25亿官方技术部会提供。
2,aspx页面代码:
Width="690px" onpageindexchanging="gridview1_PageIndexChanging" PageSize="15" >































NextPageText="下一页" PreviousPageText="上一页" Mode="NextPreviousFirstLast" />


3,cs代码:
public void DataSourse()
{
//GridView数据绑定
GridView1.DataSource = bllintable.GetModds(9, Remark, this.pbComment.CurrentPage - 1, this.pbComment.PageSize, Language);
GridView1.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{

}

private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hell = new HtmlTextWriter(tw);
GridView1.AllowPaging = false;
DataSourse();
GridView1.RenderControl(hell);
Response.Write(tw.ToString());
Response.End();
GridView1.AllowPaging = true;
DataSourse();


}
直接把上述代码复制到页面中,修改GridView绑定参数既可以运行,如有疑问,可以官方技术社区(http://bbs.25yi.com)提问。
作者: 网站设计@ CMS系统
原载: 25亿企业网站管理系统
版权所有。转载时必须以链接形式注明作者和原始出处及本声明。

posted on 2011-01-10 10:47 aiaiwoo 阅读(638) 评论(0)  编辑  收藏 所属分类: ASP.NET


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿

随笔分类

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜