EPPlus 条件格式 'AddExpression' 问题

EPPlus Conditional Format 'AddExpression' problem

我希望比我有更多 EPPlus 经验的人能给我指明更好的方向。

我在一个 sheet 中有 table 个值(40 列,大约 600 行),在另一个 sheet 中有一个值列表。对于每一行,table 中与列表(同一行)中的相应值不匹配的任何值都应通过将背景设置为红色来突出显示。

我找到了实现这一目标的方法;但它很慢。它正在为大约 24000 个单元格中的每一个创建一个条件格式规则,这在任何情况下都会花费很长时间,但是规则列表越大,添加每个单元格所需的时间就越长;总而言之, 运行 这个循环需要半个小时:

    private static bool TryCreateConditionalFormatting(List<HVISettingSection> properties, int numberOfColumns, ExcelWorksheet ranges, ref ExcelWorksheet sxs)
    {
        bool success = true;


        int row = 4;
        foreach (HVISettingSection settings in properties)
        {
            row++; // to move beyond the section label
            foreach (var x in settings.SettingsList)
            {
                for (int col = 3; col < 3 + numberOfColumns; col++)
                {
                    var exacta = ranges.Cells[row, 3];
                    //ExcelFormulaAddress exactaAddr = new ExcelFormulaAddress(exacta.Address);
                    var neqRule = sxs.ConditionalFormatting.AddExpression(sxs.Cells[row, col]);
                    string _statement = string.Format(
                                         "AND(LEN(Ranges!{0})<>0, Ranges!{0}<>{1})",
                                         exacta.Address,
                                         new OfficeOpenXml.ExcelCellAddress(row, col).Address);

                    neqRule.Style.Fill.BackgroundColor.Color = Color.Red;
                    neqRule.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
                    neqRule.Formula = _statement;
                }

            }
        }

        return success;
    }

我希望有一些方法可以为整行定义规则,但只更改单个单元格的格式;所以我的公式不是

string _statement = string.Format(
                                         "AND(LEN(Ranges!{0})<>0, Ranges!{0}<>{1})",
                                         exacta.Address,
                                         new OfficeOpenXml.ExcelCellAddress(row, col).Address);

我想要这样的东西:

string _statement = string.Format(
                                         "AND(LEN(Ranges!{0})<>0, Ranges!{0}<>{1})",
                                         exacta.Address,
                                         <process each address in a range, separately>;

最后一行当然是完全编造的,如果你喜欢伪代码。

有什么办法吗?

感谢大家

这不是我希望找到的答案,但这可能是我能做的最好的答案。

我没有找到真正做我想做的事的方法,但我确实找到了另一种方法来解决我的问题。我刚刚创建了一个模板电子表格,页面上包含所有条件格式(我很幸运,除了行数之外,细节不依赖于内容;所以我只是创建了一堆行,任意更多比我需要的)

我将该 XSLX 文件保存为 .Net 文件资源。然后,当我启动时,我会这样做:

using (BinaryWriter bw = new BinaryWriter(File.Open(filename, FileMode.Create)))
{
    // this is the xslx file resource; just write it to the file
    bw.Write(Properties.Resources.SampleStatus);  
}

//  Now, turn around and read that file into EPP ExcelPackage
FileInfo myFile = new FileInfo(filename);
using (ExcelPackage ExcelFile = new ExcelPackage(myFile))
{
    // Access the worksheets in the excel file
    ExcelWorksheet sxsWorksheet = ExcelFile.Workbook.Worksheets["Side-By-Side"];
    ExcelWorksheet rangesWorksheet = ExcelFile.Workbook.Worksheets["Ranges"];

    // add all of the data content here

    // add any additional worksheets you want

    // ... and save it!
    ExcelFile.Save();
}

速度很快 -- 可能需要 5 秒,而不是 20-30 分钟。我希望它能帮助别人。