将IList、DataTable数据导出到Excel
/// <summary> /// IList导出Excel /// </summary> /// <typeparam name="T"></typeparam> /// <param name="list">集合</param> /// <param name="dataColumn">字段</param> /// <param name="fileName"></param> public static void ListToExcel<T>(IList list, string[] dataColumn, string fileName) { 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(); } /// <summary> /// DataTable导出Excel /// </summary> /// <param name="data">集合</param> /// <param name="dataColumn">字段</param> /// <param name="fileName">文件名称</param> public static void DataTableToExcel(DataTable data, string[] dataColumn, string fileName) { 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) { sbHtml.AppendLine("<td>" + item + "</td>"); } sbHtml.AppendLine("</tr>"); //写数据 foreach (DataRow row in data.Rows) { sbHtml.Append("<tr>"); foreach (string item in dataColumn) { sbHtml.Append("<td>").Append(row[item]).Append("</td>"); } sbHtml.AppendLine("</tr>"); } sbHtml.AppendLine("</table>"); HttpContext.Current.Response.Write(sbHtml.ToString()); HttpContext.Current.Response.End(); }
作 者:大師兄丶
出 处:http://www.cnblogs.com/zhao-yi
Git 地 址:https://github.com/ZhaoYis
个人博客:http://www.zhaoyis.com.cn
关于作者:主要从事基于.Net Framework平台的项目开发。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是作者坚持原创和持续写作的最大动力!
出 处:http://www.cnblogs.com/zhao-yi
Git 地 址:https://github.com/ZhaoYis
个人博客:http://www.zhaoyis.com.cn
关于作者:主要从事基于.Net Framework平台的项目开发。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是作者坚持原创和持续写作的最大动力!