EPPlus条件格式“ AddExpression”问题

时间:2018-09-05 19:42:40

标签: epplus conditional-formatting

我希望比EPPlus拥有更多经验的人可以为我指明更好的方向。

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

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

const

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

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

最后一行当然完全由伪代码组成。

有什么办法吗?

感谢所有人

1 个答案:

答案 0 :(得分:0)

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

我没有找到实际去做自己想要的方法的方法,但是我确实找到了解决我的问题的另一种方法。我刚刚创建了一个模板电子表格,页面上具有所有条件格式(我很幸运,除了行数之外,详细信息与内容无关;因此,我只创建了BUNCH行,任意多超出我的需求

我将该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分钟。我希望它可以帮助其他人。

相关问题