You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在XSLT中使用变量设置Excel工作表名称?

解决方法:用XSLT参数动态设置Excel工作表名称

首先确认全局参数已正确定义(需放在<xsl:stylesheet>根节点下):

<xsl:param name="rootProductID" />

1. 修改Workbook中的name属性

找到定义工作表入口的代码段(通常在Workbook的节点下),把硬编码的"Sheet1"替换为参数值:

错误写法(你可能试过的):

<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, '/\:*?&quot;&lt;&gt;|', '')" />
<Sheet name="{$cleanSheetName}" ... />
<Name><xsl:value-of select="$cleanSheetName" /></Name>

内容的提问来源于stack exchange,提问作者Ian W

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 06:32:39