C#导出⽂件函数
/// <summary>
/// 导出⽂件类型
/// </summary>
public enum FileType
{
/// <summary>
/// Excel⽂档
/// </summary>
Excel,
/// <summary>
/
// Html⽂件
/// </summary>
Html,
/// <summary>
/// Word⽂档
/// </summary>
Word
}
/// <summary>
/// ⽂件到处函数
/// </summary>
/
// <param name="controlName">需要导出成为⽂件服务器控件</param>
/// <param name="fileType">⽂件类型</param>
/// <param name="page">页⾯</param>
public void ExportFile(System.Web.UI.Control controlName, FileType fileType, Page page)        {
page.Response.Clear();
page.Response.Buffer = true;
#region 处理输出类型
switch (fileType)
enum函数{
case FileType.Excel:
page.Response.Charset = "GB2312";
page.Response.AppendHeader("Content-Disposition", "attachment;filename=ExportFile.xls");
page.Response.ContentType = "application/ms-excel";
break;
case FileType.Html:
page.Response.Charset = "GB2312";
page.Response.AppendHeader("Content-Disposition", "attachment;filename=ExportFile.html");
page.Response.ContentType = "application/ms-html";
break;
case FileType.Word:
page.Response.Charset = "GB2312";
page.Response.AppendHeader("Content-Disposition", "attachment;filename=ExportFile.doc");
page.Response.ContentType = "application/ms-word";
break;
default:
page.Response.Charset = "GB2312";
page.Response.AppendHeader("Content-Disposition", "attachment;filename=ExportFile.xls");
page.Response.ContentType = "application/ms-excel";
break;
}
#endregion
page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");    //设置输出流为简体中⽂            //this.EnableViewState = false;
//System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
controlName.RenderControl(oHtmlTextWriter);
page.Response.Write(oStringWriter.ToString());
page.Response.End();
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。