共享一个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, ...
随机推荐
- exp命令ORACLCE10G导出ORACLE11G的数据1455错误
异常:1455 解决: 命令:exp youruser/password@192.xxx.x.xx:1521/orcl owner=youruser file=c:\export.dmp trigge ...
- oracle分析函数 之分组累加求和
select s.slice_date_to ,s.made_id ,sum(s.steup_count)over(partition by s.made_id order by s.slice_da ...
- 本地同时启动两个tomcat
本地同时启动两个tomcat 这几天开发用到了Ext JS4,所以着手学习Ext JS4,由于官方很多demo都是需要与服务器端进行数据交互,因此需要在tomcat里部署上官方的demo.而本地mye ...
- RSA加密工具包
主要参考: http://www.blogjava.net/icewee/archive/2012/05/19/378570.html http://snowolf.iteye.com/ 基于以上代码 ...
- 多线程要点--CLR C#学习笔记
1.windows永远不会调度一个进程,只调度线程. 2.线程和操作系统的关系:CLR(X)--AppDomain--线程池(包含工作者线程和I/O线程) 3.线程的关键组成部分 A.线程执行上下文 ...
- 动态SQL之标签
本节主要讲了动态SQL的几个标签:where set trim where: 检出where语句的最前面是否含有AND和一个空格 或者 or和一个空格 ,如果有的话删除 set: 检出set的最后是否 ...
- js键盘事件全面控制详解【转】
js键盘事件全面控制 主要分四个部分第一部分:浏览器的按键事件第二部分:兼容浏览器第三部分:代码实现和优化第四部分:总结 第一部分:浏览器的按键事件 用js实现键盘记录,要关注浏览器的三种按键事件类型 ...
- HackerRank "Bike Racer"
Just for study from its editorial~ Lesson learnt: an optimized Hungarian Algorithm: Hopcroft-Karp Al ...
- PLSQL_性能优化系列15_Oracle Explain Plan解析计划解读
2014-12-19 Created By BaoXinjian
- IGS_学习笔记06_IREP发布客户化集成接口为Web Service(案例)
2015-01-03 Created By BaoXinjian