将html导出excel的方法之一

在工作过程中用到了这种导出方式,记录下来,以便可以改进和积累

 

/// <summary>
/// 将html导出Excel
/// </summary>
/// <param name="content">html内容</param>
/// <param name="FileName">导出时的默认文件名</param>
public void ExportToExcel(string content, string FileName)
{
var resp
= this.Response;
string filenames = HttpUtility.UrlEncode(FileName + DateTime.Now.ToString("yyyy-MM-dd"), Encoding.UTF8) + ".xls";
Dictionary
<string, string> cols = new Dictionary<string, string>();
//设定导出文件的格式
resp.ContentType = "application/vnd.ms-excel";
//设定编码方式
//resp.Charset = "gb2312";
resp.ContentEncoding = System.Text.Encoding.UTF7;
//关闭ViewState
EnableViewState = false;
//把HTML写回浏览器
resp.AppendHeader("Content-Disposition", "attachment;filename=" + filenames);
resp.Write(content);

}

 

 

 

posted on 2010-12-26 16:49  吖龙  阅读(519)  评论(0编辑  收藏  举报

导航