public void ExportPermissionRoleData(string search, int roleStatus)
{
var workbook = new HSSFWorkbook();
string random = DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next();
string fileName = HttpUtility.UrlEncode("sheet" + random + ".xls", System.Text.Encoding.UTF8); #region 表头
ISheet worksheet = workbook.CreateSheet("sheet");
IRow headerRow = worksheet.CreateRow();
ICell cell = headerRow.CreateCell();
cell.SetCellValue("test1");
ICell cellType = headerRow.CreateCell();
cellType.SetCellValue("test2");
ICell cellOper = headerRow.CreateCell();
cellOper.SetCellValue("test3");
//设置颜色
ICellStyle style = workbook.CreateCellStyle();
style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Yellow.Index;
style.FillPattern = FillPattern.SolidForeground;
cell.CellStyle = style;
cellType.CellStyle = style;
cellOper.CellStyle = style;
//设置宽度
worksheet.SetColumnWidth(, * );
worksheet.SetColumnWidth(, * );
worksheet.SetColumnWidth(, *);
//冻结前3列
worksheet.CreateFreezePane(, , , );
//动态加载所有的角色名称,创建时间倒序排列
var predicate = PredicateBuilder.True<Role>();
if (!string.IsNullOrWhiteSpace(search))
{
predicate = predicate.And(r => r.Name.ToLower().Contains(search) || (r.Description != null && r.Description.ToLower().Contains(search)));
}
if (roleStatus != -)
{
bool status = roleStatus ==? true : false;
predicate = predicate.And(c => c.Status == status);
}
var roleList = RoleService.Find(predicate).OrderByDescending(t => t.CreateDateTime).ToList();
var roleCount = roleList.Count();
int headIndex = ;
for (int head = ; head < roleCount; head++)
{
//headerRow.CreateCell(headIndex + head).SetCellValue(roleList[head].Name);
ICell cellRole = headerRow.CreateCell(headIndex + head);
cellRole.SetCellValue(roleList[head].Name);
//设置颜色
ICellStyle styleRole = workbook.CreateCellStyle();
styleRole.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LightGreen.Index;
styleRole.FillPattern = FillPattern.SolidForeground;
cellRole.CellStyle = styleRole;
}
#endregion #region 填充表头数据
int rowIndex = ;
var configService = DependencyResolver.Current.GetService<IConfigAppService>();
var configs = configService.GetOnlyPermissionConfig().ToAllPermissionList();
int bodyCount = configs.Count;
for (int body = ; body < bodyCount; body++)
{
try
{
IRow dataRow = worksheet.CreateRow(rowIndex + );
dataRow.CreateCell().SetCellValue(configs[body].Moudle);
dataRow.CreateCell().SetCellValue(configs[body].Type);
dataRow.CreateCell().SetCellValue(configs[body].Name); //根据角色表权限Code找对应的权限Code
//匹配相应的【权限行】 对应的 【角色列】
for (int head = ; head < roleCount; head++)
{
var RoleCodes = roleList[head].PermissionCodes.Split(',');
foreach (var roleCode in RoleCodes)
{
if (roleCode == configs[body].Code)
{
dataRow.CreateCell(headIndex + head).SetCellValue("√");
}
}
}
rowIndex++;
}
catch (Exception ex)
{
throw new OnlyException("导出出错!详细信息:"+ex.Message);
}
}
#endregion using (MemoryStream ms = new MemoryStream())
{
workbook.Write(ms);
ms.Flush();
ms.Position = ;
Response.Charset = "UTF-8";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.ContentType = "application/ms-excel";
ms.WriteTo(Response.OutputStream);
}
}

用NPOI操作EXCEL-锁定列CreateFreezePane()

最新文章

  1. MySQL基础之索引
  2. 使用guava进行对字符串的加锁
  3. JavaScript字符转Unicode,顺便说句:GitHub的Oh no页面很亮
  4. oracle sql初次接触
  5. Ioc容器Autofac系列(1)-- 初窥
  6. [Python] dir() 与 __dict__,__slots__ 的区别
  7. OpenGL的消隐与双缓冲
  8. C#+AE 用MapControl加载栅格格式文件
  9. ubuntu u盘安装
  10. UML 的基本组成
  11. 绝对好文C#调用C++DLL传递结构体数组的终极解决方案
  12. QT小技巧—更好管理项目(增加预编译头文件,并且指定moc文件的生成位置)good
  13. ThinkPHP配置项(六)
  14. PHP:class static
  15. C语言——第二次作业(2)
  16. day23.面向对象之继承
  17. mvc 中合并两个list集合
  18. 一篇极好的Git 总结
  19. HDU 3046 Pleasant sheep and big wolf(最小割最大流+Dinic)
  20. 如何配置Notepad++的C_C++语言开发环境

热门文章

  1. 微信小程序之自定义组件与使用
  2. 7.12实习培训日志 Linux Docker
  3. HDU - 4704 sum 大数取余+欧拉降幂
  4. UIButton和UINavigationItem设置图片和文字位置
  5. Newtonsoft.Json序列化字符串-格式化和时间格式问题
  6. Mujin Programming Challenge 2017A - Robot Racing【思维题】
  7. base64编码后的pdf文件前端页面展示--pdf.js的应用
  8. Java - 一道关于整型和字符类型相加的题目
  9. Vuex有哪些作用
  10. 第一个Three.js程序——动手写一个简单的场景