using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Web;
namespace WX.CRM.WebHelper
{
///
/// 不推荐导出2007,因为只能先导出到服务器然后才能作为下载,而且效率更慢
///
public class Excel2007Helper
{
#region List反射导出Excel
///
/// List反射导出Excel
///
///
/// 实体列表
/// 工作表名称
/// 一个工作簿的数据量(不能超过65536)
/// 返回一个流
public static void ExportListModelToExcel(List listModel, string sheetName, int sheetSize, string fileName)
{
XSSFWorkbook workbook = new XSSFWorkbook();
MemoryStream ms = new MemoryStream();
string columnName = string.Empty;
int x = 0;
DisplayNameAttribute attribute = null;
if (sheetSize > 65536)
sheetSize = 65536;
int sheetCout = (listModel.Count / sheetSize) + 1;//获取一个工作簿中多少页
for (int i = 0; i < sheetCout; i++)
{
ISheet sheet = workbook.CreateSheet(sheetName + (i + 1));
IRow headerRow = sheet.CreateRow(0);
// 设置表头.
x = 0;
foreach (PropertyInfo info in typeof(T).GetProperties())
{
attribute = (DisplayNameAttribute)Attribute.GetCustomAttribute(info, typeof(DisplayNameAttribute));
headerRow.CreateCell(x).SetCellValue(attribute == null ? info.Name : attribute.DisplayName);//获取类中的DisplayName属性
x++;
}
// 绑定值
int rowIndex = 1;
int mcount = (i + 1) * sheetSize > listModel.Count ? listModel.Count : (i + 1) * sheetSize;
for (int m = (i * sheetSize); m < mcount; m++)
{
x = 0;
IRow dataRow = sheet.CreateRow(rowIndex);
foreach (PropertyInfo column in listModel[m].GetType().GetProperties())
{
dataRow.CreateCell(x).SetCellValue(column.GetValue(listModel[m], null).ToString());
x++;
}
rowIndex++;
}
}
using (var f = File.Create(HttpContext.Current.Server.MapPath(fileName)))//只能先导出excle到服务器在做下载功能
{
workbook.Write(f);
}
}
#endregion
#region List反射导出Excel(根据已经选择的字段)
///
/// List反射导出Excel(根据以选择的字段)
///
///
/// 实体列表
/// 工作表名称
/// 一个工作簿的数据量(不能超过65536)
/// 已经选择的字段
/// 返回一个流
public static void ExportListModelToExcel(List listModel, string sheetName, int sheetSize, string checkedFilds, string fileName)
{
checkedFilds = checkedFilds.ToUpper();
XSSFWorkbook workbook = new XSSFWorkbook();
MemoryStream ms = new MemoryStream();
string columnName = string.Empty;
int x = 0;
DisplayNameAttribute attribute = null;
if (sheetSize > 65536)
sheetSize = 65536;
int sheetCout = (listModel.Count / sheetSize) + 1;//获取一个工作簿中多少页
for (int i = 0; i < sheetCout; i++)
{
ISheet sheet = workbook.CreateSheet(sheetName + (i + 1));
IRow headerRow = sheet.CreateRow(0);
// 设置表头.
x = 0;
foreach (PropertyInfo info in typeof(T).GetProperties())
{
if (checkedFilds.IndexOf("[" + info.Name.ToUpper() + "]") == -1)//如果没有选中该字段跳出这个继续
continue;
attribute = (DisplayNameAttribute)Attribute.GetCustomAttribute(info, typeof(DisplayNameAttribute));
headerRow.CreateCell(x).SetCellValue(attribute == null ? info.Name : attribute.DisplayName);//获取类中的DisplayName属性
x++;
}
// 绑定值
int rowIndex = 1;
int mcount = (i + 1) * sheetSize > listModel.Count ? listModel.Count : (i + 1) * sheetSize;
for (int m = (i * sheetSize); m < mcount; m++)
{
x = 0;
IRow dataRow = sheet.CreateRow(rowIndex);
foreach (PropertyInfo column in listModel[m].GetType().GetProperties())
{
if (checkedFilds.IndexOf("[" + column.Name.ToUpper() + "]") == -1)//如果没有选中该字段跳出这个继续
continue;
dataRow.CreateCell(x).SetCellValue(column.GetValue(listModel[m], null).ToString());
x++;
}
rowIndex++;
}
}
using (var f = File.Create(HttpContext.Current.Server.MapPath(fileName)))//只能先导出excle到服务器在做下载功能
{
workbook.Write(f);
}
}
#endregion
}
}