/// <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)
{
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();
}