• 导出EXCEL【Web方式HTML通过拼接html中table】


    DataTable dt= GetTaskList(int.MaxValue);

     

                StringBuilder table = new StringBuilder();

                table.Append("<meta http-equiv="content-type" content="application/ms-excel; charset=UTF-8"/>"); 

           //在拼接table时,需要加入这句,否则会出现乱码

     

                table.Append("<table cellpadding='1' cellspacing='1' style='1200px; background:#CCCCCC;font-size:13px;'>");

                table.Append("<tr>");

                table.Append("<td style='background:#666666; height:25px; color:#FFF; font-weight:bold; text-align:center;'>酒店ID</td>");

                table.Append("<td style='background:#666666; height:25px; color:#FFF; font-weight:bold; text-align:center;'>酒店名称</td>");

                table.Append("<td style='background:#666666; height:25px; color:#FFF; font-weight:bold; text-align:center;'>检查人</td>");

                table.Append("<td style='background:#666666; height:25px; color:#FFF; font-weight:bold; text-align:center;'>开始日期</td>");

                table.Append("<td style='background:#666666; height:25px; color:#FFF; font-weight:bold; text-align:center;'>结束日期</td>");

                table.Append("<td style='background:#666666; height:25px; color:#FFF; font-weight:bold; text-align:center;'>自查日期</td>");

                table.Append("<td style='background:#666666; height:25px; color:#FFF; font-weight:bold; text-align:center;'>整改日期</td>");

                table.Append("<td style='background:#666666; height:25px; color:#FFF; font-weight:bold; text-align:center;'>复核日期</td>");

                table.Append("<td style='background:#666666; height:25px; color:#FFF; font-weight:bold; text-align:center;'>类型</td>");

                table.Append("<td style='background:#666666; height:25px; color:#FFF; font-weight:bold; text-align:center;'>状态</td>");

                table.Append("</tr>");

     

                foreach (DataRow dr in dt.Rows)

                {

                    table.Append("<tr>");

                    table.AppendFormat("<td style='background:#FFFFFF; height:25px;'>{0}</td>", dr["HotelID"]);

                    table.AppendFormat("<td style='background:#FFFFFF; height:25px;'>{0}</td>", dr["HotelName"]);

                    table.AppendFormat("<td style='background:#FFFFFF; height:25px;'>{0}</td>", dr["CheckUser"]);

                    table.AppendFormat("<td style='background:#FFFFFF; height:25px;'>{0}</td>", HotelInspection.Component.CommonUtility.StrToDateTime(dr["BeginDate"]).ToString("yyyy-MM-dd"));

                    table.AppendFormat("<td style='background:#FFFFFF; height:25px;'>{0}</td>", HotelInspection.Component.CommonUtility.StrToDateTime(dr["EndDate"]).ToString("yyyy-MM-dd"));

                    table.AppendFormat("<td style='background:#FFFFFF; height:25px;'>{0}</td>", HotelInspection.Component.CommonUtility.StrToDateTime(dr["CheckSumbitDate"]).ToString("yyyy-MM-dd"));

                    table.AppendFormat("<td style='background:#FFFFFF; height:25px;'>{0}</td>", HotelInspection.Component.CommonUtility.StrToDateTime(dr["RectifySumbitDate"]).ToString("yyyy-MM-dd"));

                    table.AppendFormat("<td style='background:#FFFFFF; height:25px;'>{0}</td>", HotelInspection.Component.CommonUtility.StrToDateTime(dr["ReviewSumbitDate"]).ToString("yyyy-MM-dd"));

                    table.AppendFormat("<td style='background:#FFFFFF; height:25px;'>{0}</td>", dr["TaskFrom"]);

                    table.AppendFormat("<td style='background:#FFFFFF; height:25px;'>{0}</td>", dr["TaskStatus"]);

                    table.Append("</tr>");

                }

     

     

                table.Append("</table>");

     

                string filename = DateTime.Now.ToString("yyyyMMddHHmmss")+".xls";

                HttpContext.Current.Response.Clear();

                HttpContext.Current.Response.Buffer = true;

                HttpContext.Current.Response.Charset = "UTF-8";

                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename="" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + """);

                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;

                HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.UTF8;

                HttpContext.Current.Response.ContentType = "application/ms-excel;";

                HttpContext.Current.Response.Write(table.ToString());

                HttpContext.Current.Response.End();

  • 相关阅读:
    vim/vi——\n只能被替换或删除 \r只能用来插入或替换
    程序算法与人生选择【转】
    【R】大型机Linux系统安装R及bsub提交R任务
    Mothur 命令手册Mothur命令中文解释(一)
    集群与分布式的区别
    【R】Linux安装R语言包(Installing R packages on Linux)
    Android TableLayout 常用的属性介绍及演示
    handler.obtainMessage()理解
    利用sax处理xml(推荐)
    Android 的权限设置大全3
  • 原文地址:https://www.cnblogs.com/stonespawn/p/3342543.html
Copyright © 2020-2023  润新知