从相应的xml生成xls文件
问题描述:
我有一个包含示例数据的Excel(xls)模板。我需要使用动态生成的数据准备Excel文件。从相应的xml生成xls文件
首先,我从相应的xls创建了一个XML文件。 XML文件看起来是这样的:
<?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">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>Op</Author>
<LastAuthor>ufoq</LastAuthor>
<Created>2011-11-18T06:54:25Z</Created>
<LastSaved>2011-12-05T11:43:26Z</LastSaved>
<Version>11.9999</Version>
</DocumentProperties>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>9750</WindowHeight>
<WindowWidth>23310</WindowWidth>
<WindowTopX>480</WindowTopX>
<WindowTopY>405</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font x:CharSet="238" x:Family="Swiss" ss:Size="8" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
......
现在我放置在symfony中浏览该内容,并与我所生成的数据填充它。 我需要以XML文件格式输出,但我不知道如何去做到这一点。 新文件必须与模板文件具有相同的结构,因此我无法从头开始创建新文件。
答
看起来您有openxml格式的文档,您可以使用PHPExcel库进行处理。
如果你只是想更换一些简单的值在模板中,你也可以使用DOMDocument和DOMXPath处理XML文档,例如
<?php
$doc = new DOMDocument;
$doc->loadxml(getData());
$xpath = new DOMXPath($doc);
$xpath->registerNamespace('o', "urn:schemas-microsoft-com:office:office");
$xpath->registerNamespace('x', "urn:schemas-microsoft-com:office:excel");
$xpath->registerNamespace('ss', "urn:schemas-microsoft-com:office:spreadsheet");
$xpath->registerNamespace('html', "http://www.w3.org/TR/REC-html40");
foreach($xpath->query('/ss:Workbook/o:DocumentProperties/o:LastAuthor') as $n) {
//echo '.';
$text = $doc->createTextnode('SO Test');
$n->replaceChild($text, $n->firstChild);
}
echo $doc->savexml();
function getData() {
return <<< eox
<?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">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>Op</Author>
<LastAuthor>ufoq</LastAuthor>
<Created>2011-11-18T06:54:25Z</Created>
<LastSaved>2011-12-05T11:43:26Z</LastSaved>
<Version>11.9999</Version>
</DocumentProperties>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>9750</WindowHeight>
<WindowWidth>23310</WindowWidth>
<WindowTopX>480</WindowTopX>
<WindowTopY>405</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font x:CharSet="238" x:Family="Swiss" ss:Size="8" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
[...]
</Style>
</Styles>
</Workbook>
eox;
}
更新:如果你想在客户承认你要设置的内容(MIME)类型来选择恰当的值的文件类型,见Office 2007 File Format MIME Types for HTTP Content Streaming。例如。
$spreadsheet = new DOMDocument;
$spreadsheet->loadxml(getData());
someProcessing($spreadsheet);
if (headers_sent()) {
die('error: headers already sent');
}
header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
echo $spreadsheet->save('php://output');
感谢您的回复。 现在我用php取代了数据。但我不知道如何以excel xls格式输出这个xml。例如用户点击链接并有一个xls下载 – Karol85
查看更新...... – VolkerK