【问题标题】:Generating xls file from corresponding xml从对应的xml生成xls文件
【发布时间】:2011-12-05 13:13:48
【问题描述】:

我有一个带有示例数据的 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 文件格式输出,但我不知道该怎么做。 新文件必须与模板文件具有相同的结构,因此我无法从头开始创建新文件。

【问题讨论】:

    标签: php xml symfony1 xls


    【解决方案1】:

    看起来您有可以使用PHPExcel 库处理的openxml 格式的文档。


    如果您只想替换模板中的一些简单值,也可以使用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。例如

    $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
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    相关资源
    最近更新 更多