/// <summary>
/// 导出Excel
/// </summary>
/// <param name="obj"></param>
public void ExportData(GridView obj)
{
try
{
string style = "";
if (obj.Rows.Count > 0)
{
style = @"<style> .text { mso-number-format:\@; } </script> ";
}
else
{
style = "no data.";
}
HttpContext.Current.Response.ClearContent();
DateTime dt = DateTime.Now;
string filename = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=ExportData" + filename + ".xls");
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
obj.RenderControl(htw);
HttpContext.Current.Response.Write(style);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
catch
{
}
}
相关阅读 >>
更多相关阅读请进入《导出Excel》频道 >>

C#高级编程(第11版) C# 7 & .NET Core 2.0(.NET开发经典名著)
作者:[美]克里斯琴·内格尔(Christian Nagel)著。出版时间:2019年3月。