如何将条件格式添加到XML / Excel文件?

我通过为它编写XML来生成一个Excel文件。 我差不多完成了,但是我无法按照自己想要的方式获得条件格式。

我想对某些单元格应用一个条件。 例如,对于每个数据行(不是页眉或页脚),如果列7-13中的值大于列6中的值,则列7-13应该高亮显示红色。下面的代码仅适用于第一个数据行。 我怎样才能得到它适用于一组行?

</Table> <ConditionalFormatting xmlns="urn:schemas-microsoft-com:office:excel"> <Range>RC7:RC13</Range> <Condition> <Qualifier>Greater</Qualifier> <Value1>RC6</Value1> <Format Style='background-color:#F7A9A5'/> </Condition> </ConditionalFormatting> </Worksheet> </Workbook> 

我宁愿不必指定确切的行号(B7-B13)。 理想情况下,我可以将它应用到我想要的每一行或通用的一组行如何。

更新:我还有一个问题,被比较的列(C6)是一个string。 如果string为空,则不应应用格式。 但是,如果该列包含一个数字,则应将其视为一个数字并进行比较。

更新:

这是更完整的代码:

 <?xml version="1.0"?> <?mso-application progid="Excel.Sheet"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"> <Author>Sodexo Platform</Author> <LastAuthor>@HttpContext.Current.User.Identity.Name</LastAuthor> <Created>@DateTime.Now.ToUniversalTime()</Created> <LastSaved>@DateTime.Now.ToUniversalTime()</LastSaved> <Company>Sodexo</Company> <Version>1</Version> </DocumentProperties> <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office"> <DownloadComponents/> <LocationOfComponents HRef="file:///D:\"/> </OfficeDocumentSettings> <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> <WindowHeight>8700</WindowHeight> <WindowWidth>11355</WindowWidth> <WindowTopX>480</WindowTopX> <WindowTopY>120</WindowTopY> <ProtectStructure>False</ProtectStructure> <ProtectWindows>False</ProtectWindows> </ExcelWorkbook> <Styles> <Style ss:ID="Table"> <Borders> <Border ss:Position="Top" ss:Color="#595959" ss:Weight="1" ss:LineStyle="Continuous"/> <Border ss:Position="Bottom" ss:Color="#595959" ss:Weight="1" ss:LineStyle="Continuous"/> <Border ss:Position="Left" ss:Color="#595959" ss:Weight="1" ss:LineStyle="Continuous"/> <Border ss:Position="Right" ss:Color="#595959" ss:Weight="1" ss:LineStyle="Continuous"/> </Borders> <Font ss:FontName="Arial" ss:Size="8" /> </Style> </Styles> <Worksheet ss:Name="Summary"> <Table> <Column ss:AutoFitWidth="0" ss:Width="200" /> <Column ss:AutoFitWidth="0" ss:Width="80" /> <Column ss:AutoFitWidth="0" ss:Width="130" /> <Column ss:AutoFitWidth="0" ss:Width="75" /> <Column ss:AutoFitWidth="0" ss:Width="75" /> <Column ss:AutoFitWidth="0" ss:Width="75" /> <Row> <Cell ss:StyleID="Table"> <Data ss:Type="String">A</Data> </Cell> <Cell ss:StyleID="Table"> <Data ss:Type="String">B</Data> </Cell> <Cell ss:StyleID="Table"> <Data ss:Type="String">C</Data> </Cell> </Row> </Table> <ConditionalFormatting xmlns="urn:schemas-microsoft-com:office:excel"> <Range>RC7:RC13</Range> <Condition> <Qualifier>Greater</Qualifier> <Value1>RC6</Value1> <Format Style='background-color:#F7A9A5'/> </Condition> </ConditionalFormatting> </Worksheet> </Workbook> 

要将条件格式应用于“通用”行集或执行类似于A:A的操作,该操作将应用于列A中具有值的所有行,或者可以将条件格式指向NamedRange。 如果命名范围发生变化,则不必更新条件格式。

要处理空string,可以使用公式条件格式,并检查单元格不为空,例如=NOT(ISBLANK(A:A))