ASP

本类阅读TOP10

·asp常用数据库连接方法和技巧
·无组件生成BMP验证码
·一些常用的辅助代码 (网络收藏)
·JavaScript实现的数据表格:冻结列、调整列宽和客户端排序
·VisualStudio.NET_2003及其 MSDN 下载地址
·ASP模拟MVC模式编程
·图片以二进制流输出到网页
·MD5加密算法 ASP版
·ASP.NET编程中的十大技巧
·改进 ASP 的字符串处理性能

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
把Web Control导出为Excel或Word

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

/// <summary>

/// Web控件导出

/// </summary>

/// <param name="source">控件实例</param>

/// <param name="type">类型:ExcelWord</param>

public void ExpertControl(System.Web.UI.Control source, DocumentType type)

{

     //设置Http的头信息,编码格式

     if (type == DocumentType.Excel)

     {

         //Excel

         Response.AppendHeader("Content-Disposition","attachment;filename=result.xls");

         Response.ContentType = "application/ms-excel";

 

     }

     else if (type == DocumentType.Word)

     {

         //Word

         Response.AppendHeader("Content-Disposition","attachment;filename=result.doc");

         Response.ContentType = "application/ms-word";

     }

     Response.Charset = "UTF-8";  

     Response.ContentEncoding = System.Text.Encoding.UTF8;

 

     //关闭控件的视图状态

     source.Page.EnableViewState =false;  

 

     //初始化HtmlWriter

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

     System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);

     source.RenderControl(htmlWriter);

 

     //输出

     Response.Write(writer.ToString());

     Response.End();

}

 

//文档类型

public enum DocumentType

{

     Word,

     Excel

}

 

调用方法:

ExpertControl(this, DocumentType.Word);

这是将整个页面导出为Word




相关文章

相关软件