zoukankan      html  css  js  c++  java
  • 用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()

  • 相关阅读:
    树莓派开发实战(第2版)
    Windows 7+Office 2010综合应用培训教程
    Adobe Edge Animate CC
    从零开始学采购:供应商管理与采购过程控制
    iOS项目开发全程实录
    SEO搜索引擎实战详解
    Photoshop移动UI设计完全实例教程
    游戏开发物理学
    psd,
    放到 userdefault,而不是 cache里面,
  • 原文地址:https://www.cnblogs.com/yexiaoyanzi/p/6677450.html
Copyright © 2011-2022 走看看