用NPOI操作EXCEL-锁定列Cr

用NPOI操作EXCEL-锁定列Cr

 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()

05-03 22:19