2010-07-17 4 views
1

Je voudrais définir la couleur d'arrière-plan pour certains tags. Ni la couleur de fond CSS ni l'attribut bgcolor ne semblent fonctionner.en utilisant xsl pour générer html pour Excel a besoin de plus de mise en forme

Y a-t-il un mso-CSS qui me donnera la couleur de fond?

Perl, XSL uniquement, Pas de produits Microsoft. Cela devrait fonctionner pour OS X et Windows, avec les versions actuelles d'Excel.

xml: <d>Y</d> 
html: <td class="Status Y" bgcolor="yellow">Y</td> 
css: .Status.Y { background-color: yellow; } 

si quelqu'un se soucie, la pleine xsl est

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="html" 
     encoding="UTF-8" 
     indent="yes" 
     omit-xml-declaration="yes" 
    /> 
    <xsl:template match="project"> 
     <html> 
      <head> 
       <title> 
        <xsl:text>pqs - </xsl:text> 
        <xsl:value-of select="table[@id='Head']/caption/row[1]/d[1]"/> 
       </title> 
       <style type="text/css"> 
       table,th,td { 
        border-width: thin; 
        border-style: outset; 
        border-color: gray; 
        border-collapse: collapse; 
        background-color: white; 
        mso-width-source:auto; 
       } 
       th,td { 
        padding: 0 1em 0 1em; 
        border-style: inset; 
       } 
       .Status { text-align: center; } 
       .Status.G { background-color: #0F0; } 
       .Status.R { background-color: red; } 
       .Status.Y { background-color: yellow; } 
       .Status.I { background-color: #6F00FF; } 
       </style> 
      </head> 
      <body> 
       <xsl:apply-templates select="node()"/> 
      </body> 
     </html> 
    </xsl:template> 
    <xsl:template match="table|caption"> 
     <xsl:if test="@id"> 
      <h1><xsl:value-of select="@id"/></h1> 
     </xsl:if> 
     <xsl:apply-templates select="caption"/> 
     <xsl:if test="count(row)&gt;0" > 
      <table id="{@id}" class="{local-name(.)}"> 
       <xsl:apply-templates select="row"/> 
      </table> 
     </xsl:if> 
    </xsl:template> 
    <xsl:template match="row"> 
     <tr><xsl:apply-templates select="node()"/></tr> 
    </xsl:template> 
    <xsl:template match="h"> 
     <th><xsl:value-of select="@name"/></th> 
    </xsl:template> 
    <xsl:template match="d"> 
     <td> 
      <xsl:variable name="text" select="text()"/> 
      <xsl:variable name="p" select="position()"/> 
      <xsl:variable name="class" select="../../row[1]/h[$p]/@name"/> 
      <xsl:choose> 
       <xsl:when test="$class='Status'"> 
        <xsl:attribute name="class"> 
         <xsl:value-of select="concat($class,' ',$text)"/> 
        </xsl:attribute> 
        <xsl:attribute name="bgcolor"> 
         <xsl:choose> 
          <xsl:when test="$text='G'"><xsl:value-of select="'green'"/></xsl:when> 
          <xsl:when test="$text='R'"><xsl:value-of select="'red'"/></xsl:when> 
          <xsl:when test="$text='Y'"><xsl:value-of select="'yellow'"/></xsl:when> 
          <xsl:when test="$text='I'"><xsl:value-of select="'#6F00FF'"/></xsl:when> 
          <xsl:otherwise><xsl:value-of select="white"/></xsl:otherwise> 
         </xsl:choose> 
        </xsl:attribute> 
       </xsl:when> 
      </xsl:choose> 
      <xsl:value-of select="text()"/> 
     </td> 
    </xsl:template> 
</xsl:stylesheet> 
+1

Postez le code que vous pensez ne pas travailler. Où ce XML est-il visualisé? Un navigateur? À quoi ressemble la transformation? – Pointy

+0

le xml n'est pas visualisé. il est converti en html pour une utilisation en Excel. –

Répondre

2

Vous devrez peut-être utiliser des attributs de classe de valeur unique et d'ajuster votre CSS en conséquence.

Quelque chose comme ceci:

html: <td class="StatusY" bgcolor="yellow">Y</td> 
css: .StatusY { background-color: yellow; } 

Excel ne semble pouvoir gérer plusieurs class valeurs d'attribut et ne semble être en mesure d'appliquer des sélecteurs CSS pour les valeurs de la classe composite.

background-color fonctionnera dans Excel, si le sélecteur CSS est appliqué à un seul attribut de classe pour un élément qui n'a qu'une valeur d'attribut de classe.

Jetez un oeil à cet exemple HTML document Excel pour voir ce que je veux dire:

<html xmlns:o="urn:schemas-microsoft-com:office:office" 
xmlns:x="urn:schemas-microsoft-com:office:excel" 
xmlns="http://www.w3.org/TR/REC-html40"> 
    <head> 
     <meta http-equiv=Content-Type content="text/html; charset=windows-1252"> 
     <meta name="ProgId content=Excel.Sheet"> 
     <meta name="Generator" content="Microsoft Excel 10"> 
     <style> 
      <!-- 
table{ border:1px solid black;} 
td{ border:1px solid black;} 
.Y {background-color:#0000FF;} 
.Status{background-color:#00FF00;} 
.Status.Y {background-color:#FF0000;} 
.StatusY {background-color:#CCCCCC;} 
--> 
     </style> 
    </head> 
    <body> 
     <table> 
      <tr> 
       <td class="Y">class="Y"</td> 
      </tr> 
      <tr> 
       <td class="Status">class="Status"</td> 
      </tr> 
      <tr> 
       <td class="Status Y" >class="Status Y"</td> 
      </tr> 
      <tr> 
       <td class="StatusY" >class="StatusY"</td> 
      </tr> 
      <tr> 
       <td>no class</td> 
      </tr> 
     </table> 
    </body> 
</html> 
+0

Vous avez frappé le clou sur la tête! MERCI! Il est facile d'oublier que Microsoft a d'abord implémenté CSS avec une seule classe. Cela fait un mauvais sens. –

Questions connexes