简体   繁体   English

如何使用Epplus从工作表模板复制或扩展条件格式

[英]How to Copy or Extend Condition Formating from Worksheet Template using Epplus

I have .xmls template file, where is in the first column (Column A), where I have some example data and specified conditional formatting (Rules for A1, and for A3). 我有.xmls模板文件,在第一列(A列)中,这里有一些示例数据和指定的条件格式(A1和A3的规则)。

I am using this template and dynamically I am adding new columns( maybe hundreds of columns). 我正在使用此模板,并且动态地添加了新列(也许有数百列)。

Every time I copy the range of the first column and inserting it into a new column. 每次我复制第一列的范围并将其插入新列中时。 Formatting and styling are copied automatically, that is okay. 格式和样式会自动复制,没关系。

However, what I want to do is also copy CONDITIONAL FORMATING specify on the first column. 但是,我还想复制第一列上指定的CONDITIONAL FORMATING。 (Make a copy of rules for B1 and B3 or extend existing rule for these Columns) (为B1和B3制作规则副本,或为这些列扩展现有规则)

Is there any way, how to: 有什么办法,如何:

  1. Copy conditional formatting rule from the first column and paste it on the n-th column? 从第一列复制条件格式设置规则并将其粘贴到第n列?

  2. Or is there way, how to extend Address of existing conditional formatting rule? 还是有办法,如何扩展现有条件格式规则的地址?

foreach (var condition in ws.ConditionFormating)
{
   condition.Address.Adress += " B1 B3";
}

You can set the range of the Conditional Formatting: 您可以设置条件格式的范围:

condition.Address = new ExcelAddress("C4:C8,G1:G3");

Separate the Ranges with Commas. 用逗号分隔范围。 As you have more than one formatting, you have to distinguish between formatting rules - the easiest way is by their priority: 由于您有多种格式,因此必须区分格式规则-最简单的方法是按其优先级排列:

if(condition.Priority == 2)
    condition.Address = new ExcelAddress("C4:C8,G1:G3");

It would be more logical of course to tell them apart by their ranges, but I am afraid, there is no EPPlus function to test if two ranges intersect. 当然,将它们按范围分开是比较合乎逻辑的,但是恐怕没有EPPlus函数可以测试两个范围是否相交。 But you could test if the Address.Address begins with A1 or A3 ... 但是您可以测试Address.Address是否以A1A3开头...

Attention: If you read the Adress.Address as set above, you get back "C4:C8 G1:G3" (Space, not Comma for addresses with more than one range). 注意:如果阅读上面设置的Adress.Address ,则会返回"C4:C8 G1:G3" (空格,而不是逗号,表示一个以上范围的地址)。 So if you want to work with this you have to do condition.Address.Address.Replace(' ',',') . 因此,如果要使用此方法,则必须执行condition.Address.Address.Replace(' ',',') This seems to be inconsistent within EPPlus. 这在EPPlus中似乎不一致。

API Documentation API文档

Adressing Sample 地址样本

Sample cs file for conditional formatting 用于条件格式的示例CS文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM