I am trying to generate a .xls file using an XML transform and an XSL template.
Things get stuck just because the generated .xls file does not contain data from the XSL template.
I think there is some problem when copying template data to excelsheet. Look at the code
<?php
include 'sendit.php';
$outputDocument = 'report.xlsx';
if (!isset($outputDocument)){
$outputDocument = "report.xlsx";
}else {
$outputDocument = 'report.xlsx';
}
$uniqueId = $_REQUEST['id'];
$xmlfile = "employee.xml";
$xsltFile = "SortName.xslt";
$sourceTemplate = "SOW.xlsx";
$xmlDocument = new DOMDocument();
$xmlDocument->load($xmlfile);
$xsltDocument = new DOMDocument();
$xsltDocument->load($xsltFile);
$xsltProcessor = new XSLTProcessor();
$xsltProcessor->importStylesheet($xsltDocument);
$newContent = $xsltProcessor->transformToXML($xmlDocument);
if (copy($sourceTemplate, $outputDocument)) {
$zipArchive = new ZipArchive();
$zipArchive->open($outputDocument);
$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";
}
?>
here is the XML for this:
<?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>
Here is the XSLT file: -
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<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>
<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" />
<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>
source
share