将html导出excel的方法之一
时间:2010-12-26 来源:吖龙
/// <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);
}
相关阅读 更多 +