.NET 导出到excel

   /// <summary>
        /// Excel导出数据
        /// </summary>
        /// <param name="data">数据源</param>
        /// <param name="fileName">文件名</param>
        public static void ExportExcel(DataTable data, string fileName)
        {
            try
            {
                if (data != null && data.Rows.Count > 0)
                {
                    HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
                    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                    HttpContext.Current.Response.Charset = "Utf-8";
                    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName + ".xls", System.Text.Encoding.UTF8));

                    StringBuilder sbHtml = new StringBuilder();
                    sbHtml.AppendLine("<meta http-equiv="Content-Type" content="text/html; charset=utf-8">");
                    sbHtml.AppendLine("<table cellspacing="0" cellpadding="5" rules="all" border="1">");

                    //写出列名
                    sbHtml.AppendLine("<tr style="background-color: #FFE88C;font-weight: bold; white-space: nowrap;">");

                    foreach (System.Data.DataColumn column in data.Columns)
                    {
                        sbHtml.AppendLine("<td>" + column.ColumnName + "</td>");
                    }
                    sbHtml.AppendLine("</tr>");

                    //写数据
                    foreach (System.Data.DataRow row in data.Rows)
                    {
                        sbHtml.Append("<tr>");

                        foreach (System.Data.DataColumn column in data.Columns)
                        {
                            sbHtml.Append("<td>").Append(row[column].ToString()).Append("</td>");
                        }
                        sbHtml.AppendLine("</tr>");
                    }
                    sbHtml.AppendLine("</table>");
                    HttpContext.Current.Response.Write(sbHtml.ToString());
                    HttpContext.Current.Response.End();
                }
            }
            catch (Exception ex)
            {
            //;    Logger.WriteLog("-----------Excel导出数据异常----------- " + ex.ToString() + " ");
            }
        }






     #region 导出Excel Demo
        /// <summary>
        /// 导出Excel Demo
        /// </summary>
        /// <returns></returns>
        public string Excel()
        {
            var cServ = new Member();
            QueryInfo queryModel = new QueryInfo();
            var pager = new PagerInfo(queryModel.Id ?? 1);
            cServ.GetStat(queryModel);
            var list = cServ.GetList(pager.StartIndex, pager.PageSize);
            Dictionary<string, string> name = new Dictionary<string, string>();
            name.Add("Id", "llId");
            name.Add("MemberNumber", "用户ID");
            name.Add("MemberType", "ip");
            name.Add("Name", "登陆时间");

            name.Add("Gender", "登陆时间");
            ExcelHelper excle = new ExcelHelper();
            excle.ExExcel<MembersInfo>(list, "55.xls", name);
            return "";
        }
        #endregion
原文地址:https://www.cnblogs.com/wybshyy/p/13783780.html