共享一个MVC通过NPOI导出excel的通用方法
public static System.IO.MemoryStream ExportExcel<T>(string title, List<T> objList, params string[] excelPropertyNames)
{
NPOI.SS.UserModel.IWorkbook workbook = new NPOI.HSSF.UserModel.HSSFWorkbook();
NPOI.SS.UserModel.ISheet sheet = workbook.CreateSheet("Sheet1");
NPOI.SS.UserModel.IRow row;
NPOI.SS.UserModel.ICell cell;
NPOI.SS.UserModel.ICellStyle cellStyle; int rowNum = ;
if (!string.IsNullOrEmpty(title))
{
#region 标题
#region 标题样式
cellStyle = workbook.CreateCellStyle();
cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;//垂直居中有问题
NPOI.SS.UserModel.IFont font = workbook.CreateFont();
font.FontHeightInPoints = ;
cellStyle.SetFont(font);
#endregion
row = sheet.CreateRow(rowNum);
cell = row.CreateCell(, NPOI.SS.UserModel.CellType.String);
cell.SetCellValue(title);
cell.CellStyle = cellStyle;
sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(, , , excelPropertyNames.Length > ? excelPropertyNames.Length - : ));
rowNum++;
#endregion
} if (objList.Count > )
{
Type type = objList[].GetType();
if (type != null)
{
System.Reflection.PropertyInfo[] properties = type.GetProperties();
if (properties.Length > )
{
#region 表头
#region 表头样式
cellStyle = workbook.CreateCellStyle();
cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
#endregion
if (excelPropertyNames.Length > )
{
row = sheet.CreateRow(rowNum);
int count = ;
for (int m = ; m < properties.Length; m++)
{
if (excelPropertyNames.Contains(properties[m].Name))
{
cell = row.CreateCell(count, NPOI.SS.UserModel.CellType.String);
string displayName = GetDisplayNameByPropertyName(properties[m].Name);
cell.SetCellValue(displayName == null ? "" : displayName);
cell.CellStyle = cellStyle;
count++;
}
}
rowNum++;
}
#endregion #region 表体
if (excelPropertyNames.Length > )
{
for (int i = ; i < objList.Count; i++)
{
row = sheet.CreateRow(i + rowNum);
int count = ;
for (int j = ; j < properties.Length; j++)
{
if (excelPropertyNames.Contains(properties[j].Name))
{
cell = row.CreateCell(count);
object obj = properties[j].GetValue(objList[i]);
cell.SetCellValue(obj == null ? "" : obj.ToString());
cell.CellStyle = cellStyle;
count++;
}
}
}
}
#endregion
}
}
}
System.IO.MemoryStream ms = new System.IO.MemoryStream();
workbook.Write(ms);
return ms;
} public static string GetDisplayNameByPropertyName(string propertyName)
{
string result = null;
foreach (KeyValuePair<string,string> dic in NameDictionary())
{
if (dic.Key == propertyName)
{
result = dic.Value;
}
continue;
}
return result;
} public static Dictionary<string, string> NameDictionary()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("AdminID", "编号"); dic.Add("AdminName", "用户名"); dic.Add("AdminMobile", "手机号"); dic.Add("RealName", "真实姓名"); return dic;
}
调用很简单
public ActionResult Test()
{
int totalCount;
List<AdminModel> adminModelList = adminBLL.GetPageList(, , out totalCount);
if (adminModelList == null)
{
adminModelList = new List<AdminModel>();
}
return File(ExcelHelper.ExportExcel<AdminModel>("表头", adminModelList, "AdminID", "AdminName", "AdminMobile", "RealName").ToArray(), "application/vnd.ms-excel", "工作簿.xls");
}
共享一个MVC通过NPOI导出excel的通用方法的更多相关文章
- ASP.NET MVC 使用NPOI导出Excel 无法访问已关闭的流(转)
第一步重写MemoryStream , 让它不能自动关闭. //新建类 重写Npoi流方法 public class NpoiMemoryStream : MemoryStream { public ...
- MVC中用NPOI导出Excel相关问题
情形1:可以直接带参数 前端页面: @.ActionLink("导出Excel", "DownLoadExcel", new { 参数名= '参数值' }, n ...
- 写一个通用的List集合导出excel的通用方法
前几天要做一个数据导出Excel 我就打算写一个通用的. 这样一来用的时候也方便,数据主要是通过Orm取的List.这样写一个通用的刚好. public static void ListToExcel ...
- asp.net Mvc 使用NPOI导出Excel文件
1.新建MVC项目,新建控制器.视图 添加控制器: 添加视图(将使用布局页前面的复选框里的勾勾去掉) 2.在Models里新建一个类 public class Shop { /// <summa ...
- Asp.net MVC NPOI导出Excel
public class NpoiMemoryStream : MemoryStream { public NpoiMemoryStream() { AllowClose = true; } publ ...
- 使用NPOI导出Excel文件
使用NPOI导出Excel文件,本实例使用了ASP.NET MVC. 1.使用NPOI导出Excel文件 实例:导出商品列表. 要求:1.通过NPOI导出导出商品列表信息: 2.使用Excel函数计算 ...
- NPOI导出Excel (C#) 踩坑 之--The maximum column width for an individual cell is 255 charaters
/******************************************************************* * 版权所有: * 类 名 称:ExcelHelper * 作 ...
- Asp.Net 使用Npoi导出Excel
引言 使用Npoi导出Excel 服务器可以不装任何office组件,昨天在做一个导出时用到Npoi导出Excel,而且所导Excel也符合规范,打开时不会有任何文件损坏之类的提示.但是在做导入时还是 ...
- .NET NPOI导出Excel详解
NPOI,顾名思义,就是POI的.NET版本.那POI又是什么呢?POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office的文件. 支持的文件格式包括xls, ...
随机推荐
- IOS的沙盒机制
ios的沙盒(bandbox)机制:一种安全体系,ios应用程序只能对自己创建的应用程序进行读取文件,这个独立.封闭.安全的空间,就我们说的沙盒.它里面一般存放着你的程序需要的文件,数据持久化的一些文 ...
- UIDatePicker自定义背景
selectDatePicker = [[UIDatePicker alloc]init]; selectDatePicker.frame = CGRectMake(0, 10, 280, 21 ...
- RDO部署openstack(2)
配置ML2和VXLAN 1. 安装和配置Neutron ML2 框架 (1) 安装在控制节点上(运行Neutron-server的节点) service neutron-server stop y ...
- VoLTE、呼叫等待(保持)
VoLTE 的出现是手机通话的革命,VoLTE带来更好通话质量,更快的接通时间,接近0掉线这些特点,还可以一边通话一边上网,一方面VoLTE需要运营商的支持,另外一方面也需要手机终端的支持. 什么手机 ...
- 【sql】之使用sql根据身份证查询过生日人数
根据当前日期查询有多少人过生日 ,) = DATE_FORMAT(NOW(),'%m'); 查询price一样的人数 select * from people where price in (sele ...
- CGI技术原理
一.CGI技术 1.1 CGI的提出 CGI是外部扩展应用程序与WWW服务器交互的一个标准接口.按照CGI标准编写的外部扩展应用程序可以处理客户端(一般是WWW浏览器)输入的协同工作数据,完成客户端与 ...
- Env:ctags和Taglist安装与配置
注:文章参照http://blog.csdn.net/vaqeteart/article/details/4146618 想必用过Source Insight的人都记得这样一个功能: SI能够把当前文 ...
- 黄聪:主机宝IIS版ISAPIRewrite伪静态软件操作演示
下载ISAPIRewrite伪静态破解文件 链接: http://pan.baidu.com/s/1dDEOLl3 密码: yx15 解压到主机宝ISAPIRewrite安装目录即可.如果提示有文件正 ...
- (C/C++) Interview in English. - Memory Allocation/Deallocation.
Q: What is the difference between new/delete and malloc/free? A: Malloc/free do not know about const ...
- NeHe OpenGL教程 第四十四课:3D光晕
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...