Changing the address of a Conditional Formatting rule can in some cases construct invalid XML.
Consider the following:
```
FileInfo file = new FileInfo("original.xlsx");
using (ExcelPackage excelPackage = new ExcelPackage(file))
{
var worksheet = excelPackage.Workbook.Worksheets.Add("Sheet 1");
var equalsRule = worksheet.ConditionalFormatting.AddEqual(new ExcelAddress(2, 3, 6, 3));
equalsRule.Formula = "0";
equalsRule.Style.Fill.BackgroundColor.Color = Color.Blue;
worksheet.ConditionalFormatting.AddDatabar(new ExcelAddress(4, 4, 4, 4), Color.Red);
excelPackage.Save();
}
using (ExcelPackage excelPackage = new ExcelPackage(file))
{
var worksheet = excelPackage.Workbook.Worksheets["Sheet 1"];
int i = 0;
foreach (var conditionalFormat in worksheet.ConditionalFormatting)
{
conditionalFormat.Address = new ExcelAddress(5 + i++, 5, 6, 6);
}
excelPackage.SaveAs(new FileInfo("error.xlsx"));
}
```
After running the example provided, you end up with two files.
I will only show the salient differences for this issue (after the <sheetData /> node).
__original.xlsx:__
<conditionalFormatting sqref="C2:C6">
<cfRule priority="1" type="cellIs" operator="equal" dxfId="0">
<formula>0</formula>
</cfRule>
</conditionalFormatting>
<conditionalFormatting sqref="D4">
<cfRule priority="2" type="dataBar">
<dataBar>
<d:cfvo type="min" val="" xmlns:d="http://schemas.openxmlformats.org/spreadsheetml/2006/main" />
<d:cfvo type="max" val="" xmlns:d="http://schemas.openxmlformats.org/spreadsheetml/2006/main" />
<color rgb="FFFF0000" />
</dataBar>
</cfRule>
</conditionalFormatting>
<headerFooter />
__error.xlsx:__
<conditionalFormatting sqref="E5:F6">
<cfRule priority="1" type="cellIs" operator="equal" dxfId="0">
<formula>0</formula>
</cfRule>
</conditionalFormatting>
<headerFooter />
<conditionalFormatting sqref="E6:F6">
<cfRule priority="2" type="dataBar">
<dataBar>
<d:cfvo type="min" val="" xmlns:d="http://schemas.openxmlformats.org/spreadsheetml/2006/main" />
<d:cfvo type="max" val="" xmlns:d="http://schemas.openxmlformats.org/spreadsheetml/2006/main" />
<color rgb="FFFF0000" />
</dataBar>
</cfRule>
</conditionalFormatting>
Note that after manipulating the conditional formatting addresses one of the nodes is below the <headerFooter /> node, yielding a corrupt document.
I've tested every conditional formatting rule that EP Plus allows you to create, and DataBars are the only ones that seem to exhibit this behavior.
Consider the following:
```
FileInfo file = new FileInfo("original.xlsx");
using (ExcelPackage excelPackage = new ExcelPackage(file))
{
var worksheet = excelPackage.Workbook.Worksheets.Add("Sheet 1");
var equalsRule = worksheet.ConditionalFormatting.AddEqual(new ExcelAddress(2, 3, 6, 3));
equalsRule.Formula = "0";
equalsRule.Style.Fill.BackgroundColor.Color = Color.Blue;
worksheet.ConditionalFormatting.AddDatabar(new ExcelAddress(4, 4, 4, 4), Color.Red);
excelPackage.Save();
}
using (ExcelPackage excelPackage = new ExcelPackage(file))
{
var worksheet = excelPackage.Workbook.Worksheets["Sheet 1"];
int i = 0;
foreach (var conditionalFormat in worksheet.ConditionalFormatting)
{
conditionalFormat.Address = new ExcelAddress(5 + i++, 5, 6, 6);
}
excelPackage.SaveAs(new FileInfo("error.xlsx"));
}
```
After running the example provided, you end up with two files.
I will only show the salient differences for this issue (after the <sheetData /> node).
__original.xlsx:__
<conditionalFormatting sqref="C2:C6">
<cfRule priority="1" type="cellIs" operator="equal" dxfId="0">
<formula>0</formula>
</cfRule>
</conditionalFormatting>
<conditionalFormatting sqref="D4">
<cfRule priority="2" type="dataBar">
<dataBar>
<d:cfvo type="min" val="" xmlns:d="http://schemas.openxmlformats.org/spreadsheetml/2006/main" />
<d:cfvo type="max" val="" xmlns:d="http://schemas.openxmlformats.org/spreadsheetml/2006/main" />
<color rgb="FFFF0000" />
</dataBar>
</cfRule>
</conditionalFormatting>
<headerFooter />
__error.xlsx:__
<conditionalFormatting sqref="E5:F6">
<cfRule priority="1" type="cellIs" operator="equal" dxfId="0">
<formula>0</formula>
</cfRule>
</conditionalFormatting>
<headerFooter />
<conditionalFormatting sqref="E6:F6">
<cfRule priority="2" type="dataBar">
<dataBar>
<d:cfvo type="min" val="" xmlns:d="http://schemas.openxmlformats.org/spreadsheetml/2006/main" />
<d:cfvo type="max" val="" xmlns:d="http://schemas.openxmlformats.org/spreadsheetml/2006/main" />
<color rgb="FFFF0000" />
</dataBar>
</cfRule>
</conditionalFormatting>
Note that after manipulating the conditional formatting addresses one of the nodes is below the <headerFooter /> node, yielding a corrupt document.
I've tested every conditional formatting rule that EP Plus allows you to create, and DataBars are the only ones that seem to exhibit this behavior.