zoukankan      html  css  js  c++  java
  • 解决ExcelReport导出Excel报Number of rules must not exceed 3错误的问题

    报错信息:

    Number of rules must not exceed 3

    [ArgumentException: Number of rules must not exceed 3]
       NPOI.XSSF.UserModel.XSSFSheetConditionalFormatting.AddConditionalFormatting(CellRangeAddress[] regions, IConditionalFormattingRule[] cfRules) +870
       NPOI.Extend.SheetExtend.CopyRows(ISheet sheet, Int32 startRowIndex, Int32 endRowIndex) +620
       ExcelReport.SheetAdapter.CopyRow(Int32 rowIndex, Action processTemplate) +37
       ExcelReport.TableFormatter`1.Format(SheetAdapter sheetAdapter) +485
       ExcelReport.SheetFormatter.Format(IWorkbook workbook) +225
       ExcelReport.Export.ExportToBuffer(String templateFile, SheetFormatter[] sheetFormatters) +46
       ExcelReport.ExportHelper.ExportToWeb(String templateFile, String targetFile, SheetFormatter[] sheetFormatters) +294

    使用源代码进行调试发现错误原因在于ExcelReport调用的NPOI.Extend这个拓展的问题
    有问题的方法:CellExtend类下的AddConditionalFormattingRules方法

    
    

    cell.Sheet.SheetConditionalFormatting.AddConditionalFormatting(regions, cfrs); 该方法的cfrs数组最大长度只能支持3个条件格式规则

    可以改为循环添加条件格式规则或者按长度判断,长度大于3则循环添加

    原方法:

            /// <summary>
            ///     添加条件格式规则
            /// </summary>
            /// <param name="cell">单元格</param>
            /// <param name="cfrs">条件格式规则</param>
            public static void AddConditionalFormattingRules(this ICell cell, IConditionalFormattingRule[] cfrs)
            {
                CellRangeAddress[] regions =
                {
                    new CellRangeAddress(cell.RowIndex, cell.RowIndex, cell.ColumnIndex, cell.ColumnIndex)
                };
                cell.Sheet.SheetConditionalFormatting.AddConditionalFormatting(regions, cfrs);
            }

    更改后的方法:

            #region 1.0 添加条件格式规则
    
            /// <summary>
            ///     添加条件格式规则
            /// </summary>
            /// <param name="cell">单元格</param>
            /// <param name="cfrs">条件格式规则</param>
            public static void AddConditionalFormattingRules(this ICell cell, IConditionalFormattingRule[] cfrs)
            {
                CellRangeAddress[] regions =
                {
                    new CellRangeAddress(cell.RowIndex, cell.RowIndex, cell.ColumnIndex, cell.ColumnIndex)
                };
                if (cfrs.Length <= 3)
                {
                    cell.Sheet.SheetConditionalFormatting.AddConditionalFormatting(regions, cfrs);
                }
                else
                {
                    foreach (var item in cfrs)
                    {
                        cell.Sheet.SheetConditionalFormatting.AddConditionalFormatting(regions, item);
                    }
                }
    
            }

    NPOI.Extend版本:1.0.3

    ExcelReport版本:2.0.1 

  • 相关阅读:
    总结C# 调用c++ 开源代码使用问题
    nodejs v14使用await async
    一个简单的js文件,在ts中使用的方法
    ts项目+webpack+juuery 插件的引入
    js 立即执行的多种写法
    在webgl2上使用fabric做标记
    comobox 绑定datatable ,无法获取选择值问题
    axios 请求拦截并在 token 过期后自动续订后重调当前请求
    javascript hook 一个函数(不定参数个数)
    java Date 大坑
  • 原文地址:https://www.cnblogs.com/townsend/p/7544174.html
Copyright © 2011-2022 走看看