public static void ExportExcel <T>(IList list, string[] DataColumn, string fileName) { try { if (list.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 (string item in DataColumn) { string[] stritem = item.Split(':'); sbHtml.AppendLine("<td>" + stritem[0] + "</td>"); } sbHtml.AppendLine("</tr>"); //写数据 foreach (T entity in list) { Hashtable ht = HashtableHelper.GetModelToHashtable <T>(entity); sbHtml.Append("<tr>"); foreach (string item in DataColumn) { string[] stritem = item.Split(':'); sbHtml.Append("<td>").Append(ht[stritem[1]]).Append("</td>"); } sbHtml.AppendLine("</tr>"); } sbHtml.AppendLine("</table>"); HttpContext.Current.Response.Write(sbHtml.ToString()); HttpContext.Current.Response.End(); } } catch (Exception ex) { Logger.WriteLog("-----------Excel导出数据异常-----------\r\n" + ex.ToString() + "\r\n"); } }
/// <summary> /// 表格返回JSON /// </summary> /// <param name="dt">数据行</param> /// <param name="pageIndex">当前页面</param> /// <param name="pqGrid_Sort">要显示字段</param> /// <param name="count"></param> /// <returns></returns> public static string PqGridPageJson <T>(IList list, int pageIndex, string pqGrid_Sort, int count) { try { string[] Sort_Field = pqGrid_Sort.Split(','); StringBuilder sb = new StringBuilder(); sb.Append("{"); sb.Append("\"totalRecords\": " + count + ","); sb.Append("\"curPage\": " + pageIndex + ","); sb.Append("\"data\": ["); if (list.Count > 0) { foreach (T entity in list) { Hashtable ht = HashtableHelper.GetModelToHashtable <T>(entity); sb.Append("["); foreach (string item in Sort_Field) { sb.Append("\""); if (ht[item] != null && ht[item] != DBNull.Value && ht[item].ToString() != "") { sb.Append(ht[item]); } else { sb.Append(""); } sb.Append("\","); } sb = sb.Remove(sb.Length - 1, 1); sb.Append("],"); } sb = sb.Remove(sb.Length - 1, 1); } sb.Append("]"); sb.Append("}"); return(sb.ToString()); } catch (Exception ex) { Logger.WriteLog(ex.Message); return(""); } }
/// <summary> /// 实体类给服务器控件赋值 /// </summary> /// <param name="page"></param> /// <param name="entity">实体类</param> public static void SetWebControls <T>(Control page, T entity) { SetWebControls(page, HashtableHelper.GetModelToHashtable(entity)); }