把Web Control导出为Excel或Word

王朝asp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

/// <summary>

/// 将Web控件导出

/// </summary>

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

/// <param name="type">类型:Excel或Word</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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航