【问题标题】:Error in MS excel when generate xls with xslt使用 xslt 生成 xls 时 MS excel 出错
【发布时间】:2013-03-14 12:04:24
【问题描述】:

我想用 xls 生成一个 excel .xls 文件。 我已经想出了如何生成文件。只有在 MS Excel 中打开文件时,才会出现以下错误。

文件损坏,无法打开。

这是输出;

    enterFile: Order_2013.xls

<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"><Styles><Style xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:ID="Default" ss:Name="Normal"><Alignment ss:Vertical="Bottom"/><Borders/><Font/><Interior/><NumberFormat/><Protection/></Style><Style xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:ID="s21"><Font ss:Size="22" ss:Bold="1"/></Style><Style xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:ID="s22"><Font ss:Size="14" ss:Bold="1"/></Style><Style xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:ID="s23"><Font ss:Size="12" ss:Bold="1"/></Style><Style xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:ID="s24"><Font ss:Size="10" ss:Bold="1"/></Style></Styles><Worksheet xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:Name="order"><Table><Column ss:AutoFitWidth="0" ss:Width="85"/><Column ss:AutoFitWidth="0" ss:Width="115"/><Column ss:AutoFitWidth="0" ss:Width="115"/><Column ss:AutoFitWidth="0" ss:Width="160"/><Column ss:AutoFitWidth="0" ss:Width="115"/><Column ss:AutoFitWidth="0" ss:Width="85"/><Column ss:AutoFitWidth="0" ss:Width="85"/><Column ss:AutoFitWidth="0" ss:Width="160"/><Row ss:AutoFitHeight="0" ss:Height="27.75"><Cell ss:StyleID="s21"><Data ss:Type="String">Example Spreadsheet</Data></Cell></Row><Row ss:AutoFitHeight="0" ss:Height="18"><Cell ss:StyleID="s22"><Data ss:Type="String">Vuurvlinderronde 15</Data></Cell></Row><Row><Cell><Data ss:Type="String">
                            test
                            </Data></Cell></Row><Row xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" ss:AutoFitHeight="0" ss:Height="18"><Cell ss:StyleID="s23"><Data ss:Type="String">
                Collumn 1
                </Data></Cell></Row><Row xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"><Cell ss:StyleID="s24"><Data ss:Type="String">
                    Collumn 2
                </Data></Cell><Cell ss:StyleID="s24"><Data ss:Type="String">
                    Collumn 3
                </Data></Cell><Cell ss:StyleID="s24"><Data ss:Type="String">
                    Collumn 4
                </Data></Cell><Cell ss:StyleID="s24"><Data ss:Type="String">
                    Collumn 5
                </Data></Cell></Row></Table></Worksheet></Workbook> code here

这是 xsl 样式表

<?xml version="1.0" encoding="ISO-8859-1"?>
<?mso-application progid="Excel.Sheet"?>

<files>
<file filename="Order_%Y%.xls">

<xsl:stylesheet version="1.0" 
xmlns:html="http://www.w3.org/TR/REC-html40"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    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">

    <xsl:template match="/">

<xsl:for-each select="orders/order">

        <Workbook>
            <Styles>
                <Style ss:ID="Default" ss:Name="Normal">
                    <Alignment ss:Vertical="Bottom" />
                    <Borders />
                    <Font />
                    <Interior />
                    <NumberFormat />
                    <Protection />
                </Style>
                <Style ss:ID="s21">
                    <Font ss:Size="22" ss:Bold="1" />
                </Style>
                <Style ss:ID="s22">
                    <Font ss:Size="14" ss:Bold="1" />
                </Style>
                <Style ss:ID="s23">
                    <Font ss:Size="12" ss:Bold="1" />
                </Style>
                <Style ss:ID="s24">
                    <Font ss:Size="10" ss:Bold="1" />
                </Style>
            </Styles>

            <Worksheet ss:Name="order">
                <Table>
                    <Column ss:AutoFitWidth="0" ss:Width="85" />
                    <Column ss:AutoFitWidth="0" ss:Width="115" />
                    <Column ss:AutoFitWidth="0" ss:Width="115" />
                    <Column ss:AutoFitWidth="0" ss:Width="160" />
                    <Column ss:AutoFitWidth="0" ss:Width="115" />
                    <Column ss:AutoFitWidth="0" ss:Width="85" />
                    <Column ss:AutoFitWidth="0" ss:Width="85" />
                    <Column ss:AutoFitWidth="0" ss:Width="160" />

                    <Row ss:AutoFitHeight="0" ss:Height="27.75">
                        <Cell ss:StyleID="s21">
                            <Data ss:Type="String">Voorbeeld</Data>
                        </Cell>
                    </Row>
                    <Row ss:AutoFitHeight="0" ss:Height="18">
                        <Cell ss:StyleID="s22">
                            <Data ss:Type="String">
                                <xsl:value-of select="shipping/street" />
                            </Data>
                        </Cell>
                    </Row>
                    <Row>
                        <Cell>
                            <Data ss:Type="String">
                            test
                            </Data>
                        </Cell>
                    </Row>

                    <xsl:call-template name="orders" />


                </Table>
            </Worksheet>


        </Workbook>
</xsl:for-each>
    </xsl:template>



    <xsl:template name="orders">

        <Row ss:AutoFitHeight="0" ss:Height="18">
            <Cell ss:StyleID="s23">
                <Data ss:Type="String">
                Collumn 1
                </Data>
            </Cell>
        </Row>
        <Row>
            <Cell ss:StyleID="s24">
                <Data ss:Type="String">
                    Collumn 2
                </Data>
            </Cell>
            <Cell ss:StyleID="s24">
                <Data ss:Type="String">
                    Collumn 3
                </Data>
            </Cell>
            <Cell ss:StyleID="s24">
                <Data ss:Type="String">
                    Collumn 4
                </Data>
            </Cell>
            <Cell ss:StyleID="s24">
                <Data ss:Type="String">
                    Collumn 5
                </Data>
            </Cell>
        </Row>

        <xsl:for-each
            select="orders/order">

            <Row>
                <Cell>
                    <Data ss:Type="String">
                        <xsl:value-of select="order_id" />
                    </Data>
                </Cell>
                <Cell>
                    <Data ss:Type="String">
                        <xsl:value-of select="order_id" />
                    </Data>
                </Cell>
                <Cell>
                    <Data ss:Type="String">
                        <xsl:value-of select="order_id" />
                    </Data>
                </Cell>
                <Cell>
                    <Data ss:Type="String">
                        <xsl:value-of select="order_id" />
                    </Data>
                </Cell>
            </Row>

        </xsl:for-each>
    </xsl:template>


</xsl:stylesheet>

</file>
</files>

我希望有人能指出正确的解决方案。

提前致谢!

J

【问题讨论】:

  • 您能否展示您正在使用的输入 XML 示例?谢谢!

标签: xml excel xslt xls


【解决方案1】:

XML 格式的 Excel 文件具有扩展名 .xlsx 否则 Excel 需要 BIFF 格式。

【讨论】:

  • 如果您保存为 .xml,Excel 2007 在打开时会生成一个错误文件,这可能会有所帮助。当我查看 Excel 创建的 .xml 文件时,列在行内。您的文件在列中有行。
【解决方案2】:

在您的 XSLT 示例中,您将 xsl:stylesheet 元素包含在 file 元素中。

<files>
   <file filename="Order_%Y%.xls">
      <xsl:stylesheet version="1.0" ....
         <!-- XSLT Code.... -->
      </xsl:stylesheet>
   </file>
</files>

也许您不是有意将它们放在您的问题中,但 filesfile 元素绝对不应该存在。我认为 files 根本不是有效的 Excel XML。我希望 Workbook 元素是 Excel XML 文件中最顶层的元素。

您当前的输出还在顶部包含一行文本enterFile: Order_2013.xls。同样,也许这只是您的问题的一个问题,但它不应该出现在输出中。输出应该是格式良好的 XML 文档。

还有一点需要注意的是处理指令的使用

<?mso-application progid="Excel.Sheet"?>

虽然这对于 Excel XML 是正确的,但它不需要放在 XSLT 文件中。就目前而言,这将是 XSLT 处理器的处理指令,它将忽略它!您需要 XSLT 在输出中写出处理指令。为此,请将此命令添加到第一个模板中

<xsl:template match="/">
   <xsl:processing-instruction name="mso-application">
      <xsl:text>progid="Excel.Sheet"</xsl:text>
   </xsl:processing-instruction>
   <xsl:for-each select="orders/order">

当我在一个空的 XML 文件上尝试 XSLT 时,我能够生成一个成功打开的 Excel XML 文件。

【讨论】:

    猜你喜欢
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 2017-06-27
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多