tags:

views:

32

answers:

1

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'&gt;
    <Worksheet ss:Name='Connections'>
        <Table>
            <Row>
                <Cell><ss:Data ss:Type='String' xmlns="http://www.w3.org/TR/REC-html40"&gt;&lt;B&gt;Test&lt;/B&gt;&lt;/Data&gt;&lt;/Cell&gt;
                <Cell><ss:Data ss:Type='String' xmlns="http://www.w3.org/TR/REC-html40"&gt;&lt;B&gt;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"&gt;
    <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  A: 
ss:Type='String'

not

ss:Type='string'

the type is case-sensitive

Mark Baker
Complex question, simple answer. Great!
Alan