TG.WXCRM.V4/WebHelper/Excel2007Helper.cs

135 lines
5.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
{
/// <summary>
/// 不推荐导出2007因为只能先导出到服务器然后才能作为下载,而且效率更慢
/// </summary>
public class Excel2007Helper
{
#region List反射导出Excel
/// <summary>
/// List反射导出Excel
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="listModel">实体列表</param>
/// <param name="sheetName">工作表名称</param>
/// <param name="sheetSize">一个工作簿的数据量不能超过65536</param>
/// <returns>返回一个流</returns>
public static void ExportListModelToExcel<T>(List<T> 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
/// <summary>
/// List反射导出Excel根据以选择的字段
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="listModel">实体列表</param>
/// <param name="sheetName">工作表名称</param>
/// <param name="sheetSize">一个工作簿的数据量不能超过65536</param>
/// <param name="checkedFilds">已经选择的字段</param>
/// <returns>返回一个流</returns>
public static void ExportListModelToExcel<T>(List<T> 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
}
}