用反射技术实现将泛型集合类中的数据导出成EXCEL
2012-11-09 17:08
453 查看
最近在工作中碰到许多地方需要将各种类型的集合对象导出到EXCEL中,之前在网上找了NOPI的EXCEL导出工具类,都是将datatable数据导出成excel。但我们这里的数据都是通过对象返回的。于是对工具类进行了改写,使用反射读取到集合类中的属性和数据,可实现直接从集合类中导出数据到excel。废话不多说,贴代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
public class ExcelHelper<T>
{
/// <summary>
/// 泛型集合类导出成excel
/// </summary>
/// <param name="list">泛型集合类</param>
/// <param name="fileName">生成的excel文件名</param>
/// <param name="propertyName">excel的字段列表</param>
public static void ListToExcel(IList<T> list, string fileName, params string[] propertyName)
{
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel;charset=UTF-8";
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", fileName));
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.BinaryWrite(ListToExcel<T>(list, propertyName).GetBuffer());
HttpContext.Current.Response.End();
}
public static MemoryStream ListToExcel<T>(IList<T> list, params string[] propertyName)
{
//创建流对象
using (MemoryStream ms = new MemoryStream())
{
//将参数写入到一个临时集合中
List<string> propertyNameList = new List<string>();
if (propertyName != null)
propertyNameList.AddRange(propertyName);
//床NOPI的相关对象
IWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet();
IRow headerRow = sheet.CreateRow(0);
if (list.Count > 0)
{
//通过反射得到对象的属性集合
PropertyInfo[] propertys = list[0].GetType().GetProperties();
//遍历属性集合生成excel的表头标题
for (int i=0;i<propertys.Count();i++)
{
//判断此属性是否是用户定义属性
if (propertyNameList.Count == 0)
{
headerRow.CreateCell(i).SetCellValue(propertys[i].Name);
}
else
{
if (propertyNameList.Contains(propertys[i].Name))
headerRow.CreateCell(i).SetCellValue(propertys[i].Name);
}
}
int rowIndex = 1;
//遍历集合生成excel的行集数据
for (int i = 0; i < list.Count; i++)
{
IRow dataRow = sheet.CreateRow(rowIndex);
for (int j = 0; j < propertys.Count(); j++)
{
if (propertyNameList.Count == 0)
{
object obj = propertys[j].GetValue(list[i], null);
dataRow.CreateCell(j).SetCellValue(obj.ToString());
}
else
{
if (propertyNameList.Contains(propertys[j].Name))
{
object obj = propertys[j].GetValue(list[i], null);
dataRow.CreateCell(j).SetCellValue(obj.ToString());
}
}
}
rowIndex++;
}
}
workbook.Write(ms);
ms.Flush();
ms.Position = 0;
return ms;
}
}
}使用时只需调用 ListToExcel方法即可,例如:
string[] propertyName =new string[]{"id","name","content"};
ExcelHelper<dictionaryInfo>.ListToExcel(di, "result", propertyName);但是有一个小问题,生成的excel中总是从第2列开始,第1列是空的,还没找到原因。请各位大大给看看。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
public class ExcelHelper<T>
{
/// <summary>
/// 泛型集合类导出成excel
/// </summary>
/// <param name="list">泛型集合类</param>
/// <param name="fileName">生成的excel文件名</param>
/// <param name="propertyName">excel的字段列表</param>
public static void ListToExcel(IList<T> list, string fileName, params string[] propertyName)
{
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel;charset=UTF-8";
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", fileName));
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.BinaryWrite(ListToExcel<T>(list, propertyName).GetBuffer());
HttpContext.Current.Response.End();
}
public static MemoryStream ListToExcel<T>(IList<T> list, params string[] propertyName)
{
//创建流对象
using (MemoryStream ms = new MemoryStream())
{
//将参数写入到一个临时集合中
List<string> propertyNameList = new List<string>();
if (propertyName != null)
propertyNameList.AddRange(propertyName);
//床NOPI的相关对象
IWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet();
IRow headerRow = sheet.CreateRow(0);
if (list.Count > 0)
{
//通过反射得到对象的属性集合
PropertyInfo[] propertys = list[0].GetType().GetProperties();
//遍历属性集合生成excel的表头标题
for (int i=0;i<propertys.Count();i++)
{
//判断此属性是否是用户定义属性
if (propertyNameList.Count == 0)
{
headerRow.CreateCell(i).SetCellValue(propertys[i].Name);
}
else
{
if (propertyNameList.Contains(propertys[i].Name))
headerRow.CreateCell(i).SetCellValue(propertys[i].Name);
}
}
int rowIndex = 1;
//遍历集合生成excel的行集数据
for (int i = 0; i < list.Count; i++)
{
IRow dataRow = sheet.CreateRow(rowIndex);
for (int j = 0; j < propertys.Count(); j++)
{
if (propertyNameList.Count == 0)
{
object obj = propertys[j].GetValue(list[i], null);
dataRow.CreateCell(j).SetCellValue(obj.ToString());
}
else
{
if (propertyNameList.Contains(propertys[j].Name))
{
object obj = propertys[j].GetValue(list[i], null);
dataRow.CreateCell(j).SetCellValue(obj.ToString());
}
}
}
rowIndex++;
}
}
workbook.Write(ms);
ms.Flush();
ms.Position = 0;
return ms;
}
}
}使用时只需调用 ListToExcel方法即可,例如:
string[] propertyName =new string[]{"id","name","content"};
ExcelHelper<dictionaryInfo>.ListToExcel(di, "result", propertyName);但是有一个小问题,生成的excel中总是从第2列开始,第1列是空的,还没找到原因。请各位大大给看看。
相关文章推荐
- JAVA实现数据库数据导入/导出到Excel(POI技术)
- Java导出页面数据或数据库数据至Excel文件并下载,采用JXL技术,小demo(servlet实现)
- 基于Spring3 MVC实现批量导出数据成Excel文件!
- js实现导出数据到excel
- Lotus Domino 实现将视图中的数据导出到Excel中---http://www.blogjava.net/17learning/archive/2012/01/05/367933.html
- 黑马程序员----List集合中的另外两个小弟,Vector LinkedList(实现结构,本身特点,自己的独有方法). 泛型技术:
- C#实现从EXCEL将数据导出到datagridview
- PHPEXCEL结合MYSQL+PHP实现数据库数据导出EXCEL实例
- 轻松实现SQL Server与Access、Excel数据表间的导入导出
- python实现数据导出到excel--普通格式
- 用反射、注解、poi实现的excel导出
- C# WinForm 实现增删改查等功能(Access版) 系列之八-导出数据到Excel
- C#实现数据导出Excel工作表
- 轻松实现SQL Server与Access、Excel数据表间的导入导出
- 使用反射技术实现的导入Excel文件到数据库的公共方法
- Java使用POI实现数据导出excel报表
- C#实现Excel模板导出和从Excel导入数据
- thinkphp实现excel数据的导入导出
- phpExcel实现Excel数据的导入导出(全步骤详细解析)
- ThinkPHP使用PHPExcel实现Excel数据导入导出完整实例