如何在XSLT中使用变量设置Excel工作表名称?
解决方法:用XSLT参数动态设置Excel工作表名称
首先确认全局参数已正确定义(需放在<xsl:stylesheet>根节点下):
<xsl:param name="rootProductID" />
1. 修改Workbook中的name属性
找到定义工作表入口的代码段(通常在Workbook的
错误写法(你可能试过的):
<Sheet name="$rootProductID" SheetId="1" r:id="rId1" />
正确写法(使用XSLT属性值模板):
<Sheet name="{$rootProductID}" SheetId="1" r:id="rId1" />
或者用<xsl:attribute>的方式:
<xsl:element name="Sheet"> <xsl:attribute name="name"><xsl:value-of select="$rootProductID" /></xsl:attribute> <xsl:attribute name="SheetId">1</xsl:attribute> <xsl:attribute name="r:id">rId1</xsl:attribute> </xsl:element>
2. 同步修改Worksheet内部的元素
如果你的样式表在Worksheet节点下有<Name>元素(用于显示工作表标签名称),同样替换:
错误写法:
<Name>Sheet1</Name>
正确写法:
<Name><xsl:value-of select="$rootProductID" /></Name>
3. 可选:处理非法字符
Excel不允许工作表名称包含\/:*?"<>|这些字符,若参数值可能包含,可添加过滤逻辑:
<xsl:variable name="cleanSheetName" select="translate($rootProductID, '/\:*?"<>|', '')" /> <Sheet name="{$cleanSheetName}" ... /> <Name><xsl:value-of select="$cleanSheetName" /></Name>
内容的提问来源于stack exchange,提问作者Ian W
相关产品推荐
相关产品推荐

