2011-12-05 85 views
1

我有一個包含示例數據的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文件格式輸出,但我不知道如何去做到這一點。 新文件必須與模板文件具有相同的結構,因此我無法從頭開始創建新文件。

回答

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。例如。

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

感謝您的回覆。 現在我用php取代了數據。但我不知道如何以excel xls格式輸出這個xml。例如用戶點擊鏈接並有一個xls下載 – Karol85

+0

查看更新...... – VolkerK