泛型List导出Excel
来源:互联网 发布:网络言情小说合集 编辑:程序博客网 时间:2024/06/10 06:27
今天整了个Excel泛型导出,切出来大家看看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace Utility.Util
{
public class Ecel<T>
{
public static void DataBindTitleExcel(System.Web.HttpResponseBase response, List<T> list, List<string> columnName, List<string> propertyName, string ExcelTitle, string strUserMsg)
{
if (list.Count == 0)
{
response.Write("<script>alert('对不起,没有查询到任何记录,导出失败!')</script>");
response.End();
}
response.ContentEncoding = Encoding.GetEncoding("GB2312");
response.ContentType = "application/ms-excel";
response.AppendHeader("Content-Disposition", "attachment;filename=Export.xls");
int count = list.Count;
StringBuilder builder = new StringBuilder();
builder.Append("<html><head>/n");
builder.Append("<meta http-equiv=/"Content-Language/" content=/"zh-cn/">/n");
builder.Append("<meta http-equiv=/"Content-Type/" content=/"text/html; charset=gb2312/">/n");
builder.Append("</head>/n");
builder.Append("<table border=1>");
if (ExcelTitle != "")
{
string str = "<font size=4><b>" + ExcelTitle + "</b></font>";
if (strUserMsg != "")
{
str = str + "(" + strUserMsg + ")";
}
builder.Append(string.Concat(new object[] { "<tr><td colspan=", count, ">", str, "</td></tr>" }));
}
builder.Append("<tr><td colspan=" + count + " valign=middle height=24>");
builder.Append("查询时间:" + DateTime.Now.ToString("G") + "</td></tr>");
builder.Append("<tr>/n");
for (int i = 0; i < columnName.Count; i++)
{
builder.Append("<td bgcolor=#969696><b>" + columnName[i] + "</b></td>/n");
}
Type objType = typeof(T);
BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static;//反射标识
PropertyInfo[] propInfoArr = objType.GetProperties(bf);
foreach (T model in list)
{
builder.Append("<tr>");
foreach (PropertyInfo propInfo in propInfoArr)
{
foreach (string propName in propertyName)
{
if (string.Compare(propInfo.Name.ToUpper(), propName.ToUpper()) == 0)
{
PropertyInfo modelProperty = model.GetType().GetProperty(propName);
if (modelProperty != null)
{
object objResult = modelProperty.GetValue(model, null);
builder.Append("<td style='vnd.ms-excel.numberformat:@'>" + ((objResult == null) ? string.Empty : objResult) + "</td>");
}
else
{
throw new Exception("Property name may be not exists!");
}
}
}
}
builder.Append("</tr>/n");
}
builder.Append("</table>/n");
response.Write(builder.ToString());
response.End();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace Utility.Util
{
public class Ecel<T>
{
public static void DataBindTitleExcel(System.Web.HttpResponseBase response, List<T> list, List<string> columnName, List<string> propertyName, string ExcelTitle, string strUserMsg)
{
if (list.Count == 0)
{
response.Write("<script>alert('对不起,没有查询到任何记录,导出失败!')</script>");
response.End();
}
response.ContentEncoding = Encoding.GetEncoding("GB2312");
response.ContentType = "application/ms-excel";
response.AppendHeader("Content-Disposition", "attachment;filename=Export.xls");
int count = list.Count;
StringBuilder builder = new StringBuilder();
builder.Append("<html><head>/n");
builder.Append("<meta http-equiv=/"Content-Language/" content=/"zh-cn/">/n");
builder.Append("<meta http-equiv=/"Content-Type/" content=/"text/html; charset=gb2312/">/n");
builder.Append("</head>/n");
builder.Append("<table border=1>");
if (ExcelTitle != "")
{
string str = "<font size=4><b>" + ExcelTitle + "</b></font>";
if (strUserMsg != "")
{
str = str + "(" + strUserMsg + ")";
}
builder.Append(string.Concat(new object[] { "<tr><td colspan=", count, ">", str, "</td></tr>" }));
}
builder.Append("<tr><td colspan=" + count + " valign=middle height=24>");
builder.Append("查询时间:" + DateTime.Now.ToString("G") + "</td></tr>");
builder.Append("<tr>/n");
for (int i = 0; i < columnName.Count; i++)
{
builder.Append("<td bgcolor=#969696><b>" + columnName[i] + "</b></td>/n");
}
Type objType = typeof(T);
BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static;//反射标识
PropertyInfo[] propInfoArr = objType.GetProperties(bf);
foreach (T model in list)
{
builder.Append("<tr>");
foreach (PropertyInfo propInfo in propInfoArr)
{
foreach (string propName in propertyName)
{
if (string.Compare(propInfo.Name.ToUpper(), propName.ToUpper()) == 0)
{
PropertyInfo modelProperty = model.GetType().GetProperty(propName);
if (modelProperty != null)
{
object objResult = modelProperty.GetValue(model, null);
builder.Append("<td style='vnd.ms-excel.numberformat:@'>" + ((objResult == null) ? string.Empty : objResult) + "</td>");
}
else
{
throw new Exception("Property name may be not exists!");
}
}
}
}
builder.Append("</tr>/n");
}
builder.Append("</table>/n");
response.Write(builder.ToString());
response.End();
}
}
}
看看效果,这是在MVC2中的导出
在Webforms中只要你将response换成System.Web.UI.Page page对象的Response即可。
本文出自 “微软技术” 博客,请务必保留此出处http://leelei.blog.51cto.com/856755/346978
- 泛型List导出Excel
- List导出数据Excel
- List导出数据Excel
- C# list导出excel文件
- Java后台list导出excel
- java poi导出list数据到excel
- java从List<Map>导出excel
- C#导出泛型List到Excel
- 【.Net码农】【PropertyInfo】C# list导出Excel
- List集合导出成Excel表格
- list集合导出Excel文件(一)
- list集合导出Excel文件(二)
- Excel通用导出List<Object>对象到excel文件
- jsp页面导出当前list数据列表 excel
- ASP.Net 后台执行导出Excel list集合 有跨行合并
- 使用NPOI导出List数据为Excel文件
- 利用NPOI和List泛型导出Excel
- 【ITOO 1】将List数据导出Excel表
- smartform输出格式设置
- 自定义ListView item的例子
- 动态Sql获取动态表数据
- 深入浅出VC++串口编程之基于Win32 API
- PHP 脚本加密 工具(软件)
- 泛型List导出Excel
- Windows 7最全的硬盘安装方法
- [zt] Perl的简单语法(与C语言语法的异同)
- vim usage
- extjs表单的初始化前设只读和之后设只读
- VC打开工程时 弹出 Visual SourceSafe Login的解决办法
- 对IEEE和SCI等的通俗简介
- 操作符重载的限制
- 关于google code