62,025
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 导出Excel静态方法
/// </summary>
/// <param name="gv">gridView控件</param>
/// <param name="ds">数据源DataSet</param>
/// <param name="tableHead">表头,就是表的标题</param>
/// <param name="tableName">表名</param>
public static void OutExcel(GridView gv, DataSet ds, string tableHead, string tableName)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "GB2312";
if (!String.IsNullOrEmpty(tableName))
{ HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(tableName) + ".xls"); }
else
{ HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=考试信息表.xls"); }
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
if (ds.Tables[0].Rows.Count > 0)
{ HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">"); }
//this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
GridView excel = new GridView();
//设置显示样式
System.Web.UI.WebControls.TableItemStyle AlternatingStyle = new TableItemStyle();
System.Web.UI.WebControls.TableItemStyle headerStyle = new TableItemStyle();
System.Web.UI.WebControls.TableItemStyle itemStyle = new TableItemStyle();
AlternatingStyle.BackColor = System.Drawing.Color.FromArgb(233, 234, 244);
headerStyle.BackColor = System.Drawing.Color.LightGray;
headerStyle.Font.Bold = true;
headerStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
itemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
gv.AlternatingRowStyle.MergeWith(AlternatingStyle);
gv.HeaderStyle.MergeWith(headerStyle);
gv.RowStyle.MergeWith(itemStyle);
gv.GridLines = GridLines.Both;
gv.HeaderStyle.Font.Bold = true;
gv.Font.Size = System.Web.UI.WebControls.FontUnit.Point(9);
gv.DataSource = ds.Tables[0];
gv.DataBind();
System.Text.StringBuilder ExcelTable = new StringBuilder();
if (ds.Tables[0].Rows.Count > 0)
{
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
gv.RenderControl(oHtmlTextWriter);
//System.Text.StringBuilder ExcelTable = new System.Text.StringBuilder("<div align='center' style='height:50px'><br><b>" + tableHead + "</b></div><div align='center' style='height:50px'><br></div>");
ExcelTable.Append("<div align='center' style='height:50px'><br><b>" + tableHead + "</b></div><div align='center' style='height:50px'><br></div>");
ExcelTable.Append(oStringWriter.ToString());
ExcelTable.Append("<div align='right' style='height:50px;font-size:12px'>创建时间:" + DateTime.Now.ToShortDateString() + "</div>");
}
HttpContext.Current.Response.Write(ExcelTable);
HttpContext.Current.Response.End();
}