Xml 转换为 Excel xls

Xml transform as Excel xls

好的,所以我一直在通过 VB.Net 对某些 XML 数据进行 XSL 转换。我一切正常,数据保存为可读的 .xls 文件。问题是我在尝试打开文档时收到以下信息。

The file you are trying to open, 'Test_Xls.xls', is in a different format than specified by the file extension. Verify that the file is not currupted and is from a trusted source before opening the file. Do you want to open the file now?

我一直在做一些阅读,但我无法想出任何办法来解决这个问题。我已经在 Notepad++ 中获取了 .xls 文件,并在仍然产生错误的同时剥离了它的所有内容。我将其作为转换的输出:

<?xml version="1.0" encoding="utf-8"?>
<Workbook 
    xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    xmlns:user="urn:my-scripts" 
    xmlns:o="urn:schemas-microsoft-com:office:office" 
    xmlns:x="urn:schemas-microsoft-com:office:excel" 
    xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">

    <Worksheet ss:Name="Testing Data">
        <Table>
            <Row>
                <Cell>
                    <Data ss:Type="String">Tests</Data>
                </Cell>
            </Row>
            <Row>
                <Cell>
                    <Data ss:Type="Number">1</Data>
                </Cell>
            </Row>
        </Table>
    </Worksheet>

</Workbook>

这引出了问题。有什么方法可以通过 Xml 转换生成可以在 Excel 中看到的文档,而不会收到此错误消息?我无法更改注册表,也无法使用任何第 3 方实用程序。

(既然问题似乎解决了,我把我的评论扩展为答案。)

据我们所知,您的方法的问题很简单(我们没有看到任何代码,除了输出文件):您的 XSLT 样式表生成一个 XML 文档,您将其保存为*.xls 扩展。这会破坏文件,因为内容在解释为 *.xls 文件时无效。

目前讨论的文件扩展名:

  • *.xsl:一种二进制格式,旧版本的Excel默认存储工作表。 不是 XML.
  • *.xslx:Excel 使用的较新版本的格式。在内部,它是一个包含多个 XML 组件的压缩文件。
  • *.xml:在这种特定情况下,所谓的"Excel 2004 XML Spreadsheet"(对于Mac;"Excel 2003 XML Spreadsheet" on Windows)格式,它存储了一个文件为普通文件,解压缩 XML。顺便说一句,其他 Office 产品也知道 *.xml 格式,例如办公字)

将转换后的文档另存为 *.xml 让 Excel 清楚内容既不是旧的二进制格式,也不是压缩的 XML 标准。


更多详细信息:令人困惑的是,Microsoft Office 调色板的压缩 XML 格式集称为 "Office Open XML"("OOXML"、ECMA-376). The plain XML formats are sometimes called "Microsoft Office XML" formats. The specific format for newer versions of Excel is "SpreadsheetML".

为了使这个更复杂,还有 "Open Office",它具有一系列其他含义,所有这些都与 Microsoft 无关。

也许您想看一看history of Office XML formats