2013-04-25 92 views
1

我正在通过为其编写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)是一个字符串。如果字符串为空,则不应应用格式。但是,如果该列包含一个数字,则应将其视为一个数字并进行比较。

更新:

这里有更完整的代码:

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

回答

0

要应用条件格式“通用”组行或做一些这样答:这将适用于所有值的行在列A中,或者您可以将条件格式指向NamedRange。如果命名范围发生变化,则不必更新条件格式。

为了应对空字符串,你可以使用一个公式条件格式,然后检查没有空白例如=NOT(ISBLANK(A:A))

+0

是的,但细胞如何做我把XML格式我上面显示? – Lifes 2013-04-26 01:09:55

+0

你目前如何生成这个XML?你能发表一些你迄今为止做过的样本吗? – 2013-04-26 01:12:16

+0

我在xml中编写了一个模板并将其保存为cshtml文档,以便我可以使用MVC的剃刀语法并轻松地将其填充数据。我只想知道如何应用条件格式,而不必指定A7:A13,B7:B13。我会发布一些代码。 – Lifes 2013-04-26 01:34:57