从相应的xml生成xls文件

发布于 2024-12-19 19:40:34 字数 1574 浏览 4 评论 0原文

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

I have an Excel (xls) template with example data. I need to prepare an Excel file with dynamically- generated data.

First, I made an XML file from the corresponding xls.
The XML file looks like 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: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/>
......

Now I placed this content in symfony view and filled it with my generated data.
I need to output in XML file format but I don't know how to to this.
The new file must have the same structure as the template file so I cannot make a new file from scratch.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝影如岚 2024-12-26 19:40:34

看起来您有 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 文件格式 MIME 类型用于 HTTP 内容流。例如

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

Looks like you have documents in the openxml format which you can process with the PHPExcel library.


If you just want to replace some simple values in your template you could also use DOMDocument and DOMXPath to process the xml document, e.g.

<?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;
}

update: If you want the client to recognize the file type you have to set the content (mime) type to the approriate value, see 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');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文