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的通用方法的更多相关文章

  1. ASP.NET MVC 使用NPOI导出Excel 无法访问已关闭的流(转)

    第一步重写MemoryStream , 让它不能自动关闭. //新建类 重写Npoi流方法 public class NpoiMemoryStream : MemoryStream { public ...

  2. MVC中用NPOI导出Excel相关问题

    情形1:可以直接带参数 前端页面: @.ActionLink("导出Excel", "DownLoadExcel", new { 参数名= '参数值' }, n ...

  3. 写一个通用的List集合导出excel的通用方法

    前几天要做一个数据导出Excel 我就打算写一个通用的. 这样一来用的时候也方便,数据主要是通过Orm取的List.这样写一个通用的刚好. public static void ListToExcel ...

  4. asp.net Mvc 使用NPOI导出Excel文件

    1.新建MVC项目,新建控制器.视图 添加控制器: 添加视图(将使用布局页前面的复选框里的勾勾去掉) 2.在Models里新建一个类 public class Shop { /// <summa ...

  5. Asp.net MVC NPOI导出Excel

    public class NpoiMemoryStream : MemoryStream { public NpoiMemoryStream() { AllowClose = true; } publ ...

  6. 使用NPOI导出Excel文件

    使用NPOI导出Excel文件,本实例使用了ASP.NET MVC. 1.使用NPOI导出Excel文件 实例:导出商品列表. 要求:1.通过NPOI导出导出商品列表信息: 2.使用Excel函数计算 ...

  7. NPOI导出Excel (C#) 踩坑 之--The maximum column width for an individual cell is 255 charaters

    /******************************************************************* * 版权所有: * 类 名 称:ExcelHelper * 作 ...

  8. Asp.Net 使用Npoi导出Excel

    引言 使用Npoi导出Excel 服务器可以不装任何office组件,昨天在做一个导出时用到Npoi导出Excel,而且所导Excel也符合规范,打开时不会有任何文件损坏之类的提示.但是在做导入时还是 ...

  9. .NET NPOI导出Excel详解

    NPOI,顾名思义,就是POI的.NET版本.那POI又是什么呢?POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office的文件. 支持的文件格式包括xls, ...

随机推荐

  1. VBA 插入一行保留样式

    Rows(processingRow).Insert ' 在指定的行数processingRow处插入一行 Rows(processingRow - 1).Select ' 选择上一行的整行 Sele ...

  2. c++中引用和指针的区别

    1.指针和引用的主要区别. 引用是为变量起另一个名字(小名),一般在变量初始化时,初始值直接拷贝给变量,定义引用时,把引用(小名)和初始值“绑定”在一起,而不是将初始值拷贝给引用.因为 无法将引用重新 ...

  3. Python 函数的创建和调用

    >>> movies =[ "the holy grail", 1975,"terry jones",91, ["graham ch ...

  4. 报错:java.io.FileNotFoundException: (系统找不到指定的路径。)

    报错如下: java.io.FileNotFoundException: E:\apache-tomcat-8.0.37\webapps\20161028-FileUpLoad\WEB-INF\fil ...

  5. 拒绝了对对象 'sp_sdidebug'(数据库 'master',所有者 'dbo')的 EXECUTE 权限。

    如果在调试过程中出现异常“拒绝了对对象 'sp_sdidebug'(数据库 'master',所有者 'dbo')的 EXECUTE 权限.”则可以通过以下方式解决: 打开master数据库,打开扩展 ...

  6. Chrome安装FlashPlayer Debug

    首先: 在谷歌浏览器上打开chrome://plugins 找到 Adobe Flash Player 然后将谷歌自带的版本停用,如果已经安装了for Netscape版,则会显示另外一个版本.(建议 ...

  7. OpenGL®ES基础

    参考:http://www.cnblogs.com/salam/archive/2016/01/08/5113572.html http://blog.csdn.net/wangyuchun_799/ ...

  8. AsyncTask的学习

    具体的用法请看我之前的一篇随笔,用php+mysql+json实现用户反馈. AsyncTask的目标是为你的线程提供管理服务. AsyncTask的执行分为四个步骤,每一步都对应一个回调方法,这些方 ...

  9. CAP原则(CAP定理)

    CAP原则又称CAP定理,指的是在一个分布式系统中, Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性),三者不可得兼. CA ...

  10. Python2.7.5 安装(转载)

    From:http://www.cnblogs.com/balaamwe/p/3480430.html From:http://www.chgon.com/?p=1340 安装python2.7.5纠 ...