从相应的xml生成xls文件

时间:2011-12-05 13:13:48

标签: php xml symfony1 xls

我有一个包含示例数据的Excel(xls)模板。我需要准备一个包含动态生成数据的Excel文件。

首先,我从相应的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文件格式输出,但我不知道如何对此。 新文件必须与模板文件具有相同的结构,因此我无法从头开始创建新文件。

1 个答案:

答案 0 :(得分:1)

您似乎拥有openxml格式的文档,您可以使用PHPExcel库处理这些文档。


如果您只想替换模板中的一些简单值,您还可以使用DOMDocumentDOMXPath来处理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。 E.g。

$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');