共享一个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, ...
随机推荐
- servlet & filter & listener & interceptor
web.xml 的加载顺序是:context- param -> listener -> filter -> servlet * Servlet 对URL生效,用户处理用户的URL请 ...
- jquery.find()
http://www.365mini.com/page/jquery-find.htm
- C# WinForm开发系列 - ComboBox
5.一个带CheckBox,分组,颜色标记等功能的下拉框 PowerComboBoxCSharp.rar 包含自动完成下拉框,字体下拉框,电脑盘符,多列下拉框,带CheckBox,树型下拉框等.代码 ...
- bzoj2152 聪聪可可
Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好 ...
- webstorm 10 设置文件的默认编码
我在使用webstorm时,发现文件的默认编码是GBK 然后我找到了点击此处可以修改这个文件的编码,但是以后新建文件和项目默认生成的文件还是GBK, 设置项目文件的默认编码可以在 File----Se ...
- windows7使用Source insight上远程修改ubuntu共享内核源码
由于本人阅读喜欢使用source insight.前段时间接触了linux核代码,而这份代码只能放在ubuntu服务器上编译,刚开始的时候是在windows上修改,完了之后再copy到服务器上去编译, ...
- 黄聪:WordPress 的 Hook 机制与原理(add_action、add_filter)
稍有接触过 WordPress 主题或插件制作修改的朋友,对 WordPress 的Hook机制应该不陌生,但通常刚接触WordPress Hook 的新手,对其运作原理可能会有点混乱或模糊.本文针对 ...
- 2. redis的数据类型
一. string类型 字符串类型是redis中最基本的数据类型,它能存储任何形式的内容,包含二进制数据,甚至是一张图片(二进制内容).一个字符串类型的值存储的最大容量是1GB 命令 (1)setnx ...
- (C#) 多线程修改布尔值, volatile
参考: https://msdn.microsoft.com/en-us/library/x13ttww7(VS.80).aspx http://stackoverflow.com/questions ...
- (C#) Action, Func, Predicate 等泛型委托
(转载网络文章) (1). delegate delegate我们常用到的一种声明 Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型. 例:public del ...