导出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='width: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();