doufen3563 2010-10-13 14:44
浏览 68
已采纳

PHP使用DOM输出XLS

I'm trying to output an XLS file in the XML format.

This is what the data should look like for Excel 2003 to render (can be saved as an xls file):

<? 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>

What I'm currently getting is this:

<?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>

Which Excel can't parse. It throws four of these errors in its log file:

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

I think that the main problem is that the <ss:Data> tags which I'm creating with DomDocument::CreateElement('ss:Data') should be closed with </Data>, whereas DomDocument is outputting </ss:Data>. I can't use CreateElementNS(), because there isn't a namespace (I think — I'm not really familiar with XML past the basics) and using CreateElementNS('','ss:Data') causes the script to die with absolutely no errors with XDebug installed and error_reporting set to E_ALL & E_STRICT.

  • 写回答

1条回答 默认 最新

  • duanlipeng4136 2010-10-13 15:18
    关注
    ss:Type='String'
    

    not

    ss:Type='string'
    

    the type is case-sensitive

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?