首页 > 代码库 > 用NPOI操作EXCEL-锁定列CreateFreezePane()

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

 public void ExportPermissionRoleData(string search, int roleStatus)        {            var workbook = new HSSFWorkbook();            string random = DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next(100);            string fileName = HttpUtility.UrlEncode("sheet" + random + ".xls", System.Text.Encoding.UTF8);            #region 表头            ISheet worksheet = workbook.CreateSheet("sheet");            IRow headerRow = worksheet.CreateRow(0);            ICell cell = headerRow.CreateCell(0);            cell.SetCellValue("test1");            ICell cellType = headerRow.CreateCell(1);            cellType.SetCellValue("test2");            ICell cellOper = headerRow.CreateCell(2);            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(0, 13 * 256);            worksheet.SetColumnWidth(1, 20 * 256);            worksheet.SetColumnWidth(2, 30*256);            //冻结前3列            worksheet.CreateFreezePane(3, 0, 3, 0);            //动态加载所有的角色名称,创建时间倒序排列            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 != -1)            {                bool status = roleStatus ==1? 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 = 3;            for (int head = 0; 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 = 0;            var configService = DependencyResolver.Current.GetService<IConfigAppService>();            var configs = configService.GetOnlyPermissionConfig().ToAllPermissionList();            int bodyCount = configs.Count;            for (int body = 0; body < bodyCount; body++)            {                try                {                    IRow dataRow = worksheet.CreateRow(rowIndex + 1);                    dataRow.CreateCell(0).SetCellValue(configs[body].Moudle);                    dataRow.CreateCell(1).SetCellValue(configs[body].Type);                    dataRow.CreateCell(2).SetCellValue(configs[body].Name);                    //根据角色表权限Code找对应的权限Code                    //匹配相应的【权限行】 对应的 【角色列】                    for (int head = 0; 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 = 0;                Response.Charset = "UTF-8";                Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);                Response.ContentType = "application/ms-excel";                ms.WriteTo(Response.OutputStream);            }        }

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

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