繁体中文
设为首页
加入收藏
当前位置:ASP技术首页 >> ASP基础 >> 把Web Control导出为Excel或Word

把Web Control导出为Excel或Word

2006-05-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介:/// /// 将Web控件导出 /// /// 控件实例 /// 类型:Excel或word public void ExpertControl(System.Web.UI.Control source, DocumentType type) { //设置Http的头信息,编码格式 if (type == DocumentType.Excel)...
关键字:Control Excel Word Web

///

/// 将Web控件导出

///

///

控件实例

///

类型:Excel或word

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

责任编辑:admin
相关文章