/// <summary>
/// 导出GridView数据到Excel
/// </summary>
/// <param name="dc">GridView</param>
/// <param name="filter">前面要显示0的项</param>
protected void DaoChuExcel(GridView dc, string filter)
{
//ArrayList $al=filter.Split('$');
string[] al = filter.Split('$');
Response.Clear();
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
curContext.Response.AddHeader("content-disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls");
curContext.Response.Charset = "UTF-8";//设置字符集
curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;//设置编码集
curContext.Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
foreach (GridViewRow dg in this.dc.Rows)
{
for (int i = 0; i < al.Length; i++)
{
dg.Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat: @;");
}
}
dc.RenderControl(htmlWrite);
curContext.Response.Write("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=UTF-8\"/>" + stringWrite.ToString());
curContext.Response.End();
}
asp.net GridView数据导出到Excel
最新推荐文章于 2021-08-27 21:02:31 发布