How to add Conditional Formatting to an XML / Excel file?

跟風遠走 提交于 2019-12-23 02:33:03

问题


I'm generating an Excel file by writing the XML for it. I am almost done, but I can't get the conditional formatting to work the way I want.

I want to apply a condition to certain cells. E.g. for each data row (not a header or footer), columns 7-13 should highlight red if they are greater than the value in Column 6. The code below works for the first data row only. How can I get it to apply to a set of rows?

    </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>

I'd rather not have to specify the exact row number (B7-B13). Ideally, I'd be able to just apply it to each row I want or a generic set of rows some how.

Update: I have another problem, the column being compared (C6) is a string. If the string is empty, the format should not be applied. However, if the column contains a number, it should be treated as a number and compared.

Update:

Here's more complete code:

<?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>

回答1:


To apply conditional formatting to a "generic" set of rows or do something like A:A which will apply to all the rows with value in column A, or you could have the conditional format point to a NamedRange. If the named range changes, you will not have to update the conditional format.

To deal with the empty string you could use a formula conditional format and check that a cell in not blank for example =NOT(ISBLANK(A:A))



来源:https://stackoverflow.com/questions/16225694/how-to-add-conditional-formatting-to-an-xml-excel-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!