【问题标题】:PHP Using DOM to output XLSPHP 使用 DOM 输出 XLS
【发布时间】:2010-10-13 14:44:07
【问题描述】:

我正在尝试以 XML 格式输出 XLS 文件。

这是 Excel 2003 呈现的数据应该是什么样子(可以保存为 xls 文件):

<? xml version='1.0' ?>
<? mso-application progid='Excel.Sheet' ?>
<Workbook 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' xmlns:html='http://www.w3.org/TR/REC-html40'>
    <Worksheet ss:Name='Connections'>
        <Table>
            <Row>
                <Cell><ss:Data ss:Type='String' xmlns="http://www.w3.org/TR/REC-html40"><B>Test</B></Data></Cell>
                <Cell><ss:Data ss:Type='String' xmlns="http://www.w3.org/TR/REC-html40"><B>Test 2</B></Data></Cell>
            </Row>
            <Row>
                <Cell><ss:Data ss:Type='String'>Data</Data></Cell>
                <Cell><ss:Data ss:Type='String'>More data</Data></Cell>
            </Row>
        </Table>
    </Worksheet>
</Workbook>

我目前得到的是这样的:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:html="http://www.w3.org/TR/REC-html40">
    <Worksheet ss:Name="Test">
        <Table>
            <Row>
                <Cell><ss:Data xmlns="http://www.w3.org/TR/REC-html40" ss:Type="string"><B>Test</B></ss:Data></Cell>
                <Cell><ss:Data xmlns="http://www.w3.org/TR/REC-html40" ss:Type="string"><B>Test 2</B></ss:Data></Cell>
            </Row>
            <Row>
                <Cell><ss:data ss:type="string">Data</ss:Data></Cell>
                <Cell><ss:data ss:type="string">More data</ss:Data></Cell>
            </Row>
        </Table>
    </Worksheet>
</Workbook>

哪些 Excel 无法解析。它在其日志文件中抛出以下四个错误:

XML ERROR in Table
REASON: Bad Value
FILE:   test.xls
GROUP:  Cell
TAG:    Data
ATTRIB: Type
VALUE:  string

我认为主要问题是我用DomDocument::CreateElement('ss:Data') 创建的&lt;ss:Data&gt; 标签应该用&lt;/Data&gt; 关闭,而DomDocument 输出的是&lt;/ss:Data&gt;。我不能使用CreateElementNS(),因为没有命名空间(我认为——除了基础知识之外,我对 XML 并不十分熟悉)并且使用CreateElementNS('','ss:Data') 会导致脚本在安装 XDebug 的情况下完全没有错误,并且error_reporting 设置为E_ALL &amp; E_STRICT

【问题讨论】:

    标签: php xml dom xls


    【解决方案1】:
    ss:Type='String'
    

    不是

    ss:Type='string'
    

    类型区分大小写

    【讨论】:

    • 复杂的问题,简单的答案。太好了!
    猜你喜欢
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 2012-05-30
    • 2011-01-23
    • 1970-01-01
    • 2020-09-12
    • 2017-10-13
    • 1970-01-01
    相关资源
    最近更新 更多