html - 使用 xsl 为 excel 生成 html 需要更多格式

标签 html css xml excel xslt

我想为一些标签设置背景颜色。 CSS background-color 或属性 bgcolor 似乎都不起作用。

是否有可以给我背景色的 mso- CSS?

仅限 Perl、XSL,无 Microsoft 产品。它应该适用于 OS X 和 Windows,以及当前版本的 Excel。

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

如果有人关心,完整的xsl是

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

最佳答案

您可能需要使用单值类属性并相应地调整您的 CSS。

像这样:

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

Excel 似乎无法处理多个 class 属性值,似乎也无法为复合应用 CSS 选择器类值。

background-color 将在 Excel 中工作,如果 CSS 选择器应用于只有一个类属性值的元素的单个类属性。

在 Excel 中查看这个示例 HTML 文档,了解我的意思:

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

关于html - 使用 xsl 为 excel 生成 html 需要更多格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3272517/

相关文章:

html - 使用 CSS 网格在行中留出 1px 的间距

javascript - 不要打开OGG文件

javascript - 将图像库 block 滚动到当前图像的位置

javascript - Dash 中的多页面应用程序 - 其他页面无法识别的下拉值

xml - 选择 xml 属性

HTML - XML 数据孤岛

javascript - 使用 javascript 链接显示更多文本

html - 如何使用 CSS 根据任意数字倍数选择特定类之后的第 n 个兄弟?

html - 使用 HTML 和 CSS 创建 L 形边框,可能吗?

.net - C# 中是否有 Sizzle/jQuery 选择器实现?