我正在尝试使用XML和XSL模板转换生成.xls文件。
因为生成的.xls文件不包含来自XSL模板的数据而导致事情陷入困境。
我认为将模板数据复制到excelsheet时存在一些问题。请查看代码
<?php
include 'sendit.php';
$outputDocument = 'report.xlsx';//$_REQUEST['output'];
//Declare variables for file names.
if (!isset($outputDocument)){
$outputDocument = "report.xlsx";
}else {
$outputDocument = 'report.xlsx';
}
$uniqueId = $_REQUEST['id'];
$xmlfile = "employee.xml";
$xsltFile = "SortName.xslt";
$sourceTemplate = "SOW.xlsx";
//Load the xml data and xslt and perform the transformation.
$xmlDocument = new DOMDocument();
$xmlDocument->load($xmlfile);
$xsltDocument = new DOMDocument();
$xsltDocument->load($xsltFile);
$xsltProcessor = new XSLTProcessor();
$xsltProcessor->importStylesheet($xsltDocument);
//After the transformation, $newContentNew contains
//the XML data in the Open XML Wordprocessing format.
$newContent = $xsltProcessor->transformToXML($xmlDocument);
//Copy the template document to the output file.
if (copy($sourceTemplate, $outputDocument)) {
//Open XML files are packaged following the Open Packaging
//Conventions and can be treated as zip files when
//accessing their content.
$zipArchive = new ZipArchive();
$zipArchive->open($outputDocument);
//Replace the content with the new content created above.
$zipArchive->addFromString("excel/document.xml", $newContent);
$zipArchive->close();
echo "Success! Download word document " . "<a href='http://localhost/demo/admin/exportDoc/export.xlsx'>HERE </a>";
SendEmail();
} else {
echo "error";
}
?>
这是用于此的XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="SortNames.xsl"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata">
<Employees>
<EmployeeID>1</EmployeeID>
<LastName>Davolio</LastName>
<FirstName>Nancy</FirstName>
<Title>Sales Representative</Title>
<TitleOfCourtesy>Ms.</TitleOfCourtesy>
</Employees>
</dataroot>
这是XSLT文件: -
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- Output as an HTML file. -->
<xsl:output method="html"/>
<xsl:template match="/">
<!-- Create a table with headings, then search for
children of the Employees element. Once processing
is completed, sort the table by the LastName field.
-->
<TABLE>
<TR>
<TH>Name</TH>
<TH>Address</TH>
<TH>Home Phone</TH>
</TR>
<xsl:apply-templates select="dataroot/Employees">
<xsl:sort select="LastName" order="ascending"/>
</xsl:apply-templates>
</TABLE>
</xsl:template>
<!-- Process and output the child elements of
the Employees element.
-->
<xsl:template match="dataroot/Employees">
<TR>
<TD>
<xsl:value-of select="FirstName" />
<xsl:text> </xsl:text>
<xsl:value-of select="LastName" />
</TD>
<TD>
<xsl:value-of select="Address" />
<br/>
<xsl:value-of select="City" />
<xsl:text>, </xsl:text>
<xsl:value-of select="Region"/>
<xsl:text> </xsl:text>
<xsl:value-of select="PostalCode"/>
</TD>
<TD>
<xsl:value-of select="HomePhone" />
<!-- Test whether the phone number
has an extension. If not, skip the
template instruction.
-->
<xsl:if test="string-length(Extension)>0">
<xsl:text> Ext </xsl:text>
<xsl:value-of select="Extension" />
</xsl:if>
</TD>
</TR>
</xsl:template>
</xsl:stylesheet>