October 16, 2010 by Christoff Truter PHP Microsoft Office
Last month (2010/9) I wrote a post about how to generate an excel document using OpenXML
and C#. In this post we're going to have a look at how to do something similar using
PHP.
However instead of using OpenXML, I am going to demonstrate how to achieve this using another method called Office
XML (not to be confused with OpenXML), which is one of the easiest
methods to generate Office documents.
The basic layout for a spreadsheet using Office XML looks something like this:
<?xml version="1.0" ?> <?mso-application progid="Excel.Sheet"?> <ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"> <ss:Worksheet ss:Name="Sheet1"> <ss:Table> <ss:Row> <ss:Cell> <ss:Data ss:Type="String">ID</ss:Data> </ss:Cell> <ss:Cell> <ss:Data ss:Type="String">Firstname</ss:Data> </ss:Cell> <ss:Cell> <ss:Data ss:Type="String">Lastname</ss:Data> </ss:Cell> </ss:Row> </ss:Table> </ss:Worksheet> </ss:Workbook>
$xmlns = "urn:schemas-microsoft-com:office:spreadsheet"; $xml = '<?mso-application progid="Excel.Sheet"?>'. '<ss:Workbook xmlns:ss="'.$xmlns.'" />'; $Workbook = new SimpleXMLElement($xml); $Worksheet = $Workbook->addChild('ss:Worksheet'); $Worksheet->addAttribute('ss:Name', 'Sheet1', $xmlns); $Table = $Worksheet->addChild('ss:Table'); $Row = $Table->addChild('ss:Row'); $Cell = $Row->addChild('ss:Cell'); $Data = $Cell->addChild('ss:Data', 'ID', $xmlns); $Data->addAttribute('ss:Type', 'String', $xmlns); $Cell = $Row->addChild('ss:Cell'); $Data = $Cell->addChild('ss:Data', 'Firstname', $xmlns); $Data->addAttribute('ss:Type', 'String', $xmlns); $Cell = $Row->addChild('ss:Cell'); $Data = $Cell->addChild('ss:Data', 'Lastname', $xmlns); $Data->addAttribute('ss:Type', 'String', $xmlns); echo $Workbook->asXml();
$Workbook = new Workbook(); $Worksheet = $Workbook->addWorksheet('Sheet1'); $Table = $Worksheet->addTable(); $Row = $Table->addRow(array('ID', 'Firstname', 'Lastname')); $Workbook->Output();
<?xml version="1.0" ?> <?mso-application progid="Excel.Sheet"?> <ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"> <ss:Styles> <ss:Style ss:ID="A"> <ss:Font ss:FontName="Times" ss:Size="16" ss:Color="Red" ss:Bold="1" /> </ss:Style> </ss:Styles> <ss:Worksheet ss:Name="Sheet1"> <ss:Table> <ss:Row ss:StyleID="A"> <ss:Cell> <ss:Data ss:Type="String">ID</ss:Data> </ss:Cell> <ss:Cell> <ss:Data ss:Type="String">Firstname</ss:Data> </ss:Cell> <ss:Cell> <ss:Data ss:Type="String">Lastname</ss:Data> </ss:Cell> </ss:Row> </ss:Table> </ss:Worksheet> </ss:Workbook>
$Workbook = new Workbook(); $Styles = new Styles($Workbook); $Worksheet = $Workbook->addWorksheet('Sheet1'); $Table = $Worksheet->addTable(); $Row = $Table->addRow(array('ID', 'Firstname', 'Lastname')); $Style = $Styles->setStyle('A', $Row); $Style->SetFont('Times', '16', 'Red', true); $Workbook->Output();
January 7, 2016 by Christoff
Hi Frank I've seen a few examples on the web the includes utf8 for "Office XML", I am unsure if Office actually adhere's to the specified encoding though... Will see if I can find an older copy of office somewhere to verify.