public class CommonTool
{
/// <summary>
/// 以流的形式,可以配置很富厚巨大的样式
/// </summary>
/// <param>Excel中内容(Table名目)</param>
/// <param>文件名</param>
/// <param>样式内容</param>
public static void ExportToExcel(string filename, string content,string cssText)
{
var res = HttpContext.Current.Response;
content = String.Format("<style type='text/css'>{0}</style>{1}",cssText,content);
res.Clear();
res.Buffer = true;
res.Charset = "UTF-8";
res.AddHeader("Content-Disposition", "attachment; filename=" + filename);
res.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
res.ContentType = "application/ms-excel;charset=UTF-8";
res.Write(content);
res.Flush();
res.End();
}
}
这种要领较量机动,并且可以通过选择器来添加样式,相当不错。缺点就是需要将数据转换成字符串。
您大概感乐趣的文章: