PHP execel Export XML Program
<?php
Class Excel_xml
{
Private $header = "<?xml version=" 1.0 "encoding=" UTF-8 "?>"
<workbook xmlns= "Urn:schemas-microsoft-com:office:spreadsheet"
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" > ";
Private $footer = "</Workbook>";
Private $lines = Array ();
Private $worksheet _title = "Table1";
Private Function AddRow ($array)
{
Initialize all cells to this row
$cells = "";
foreach key-> write value into cells
foreach ($array as $k => $v):
$cells. = "<cell><data ss:type=" String ">". Utf8_encode ($v). "</data></cell>n";
Endforeach;
Transform $cells content into one row
$this->lines[] = "<row>n". $cells. "</row>n";
}
Public Function AddArray ($array)
{
Run through the array and add them into rows
foreach ($array as $k => $v):
$this->addrow ($v);
Endforeach;
}
Public Function Setworksheettitle ($title)
{
Strip out Special chars
$title = Preg_replace ("/[|:| /|?| *| [|]] /"," ", $title);
Now cut it to the allowed length
$title = substr ($title, 0, 31);
Set Title
$this->worksheet_title = $title;
}
function Generatexml ($filename)
{
Deliver header (as recommended in PHP manual)
Header ("Content-type:application/vnd.ms-excel; Charset=utf-8 ");
Header ("Content-disposition:inline; Filename= "". $filename. ". xls" ");
Print out document to the browser
Need to use stripslashes for the damn ">"
Echo stripslashes ($this->header);
echo "N<worksheet ss:name=" ". $this->worksheet_title. "" >n<table>n ";
echo "<column ss:index=" 1 "ss:autofitwidth=" 0 "ss:width=" "/>n";
echo Implode ("n", $this->lines);
echo "</table>n</worksheet>n";
Echo $this->footer;
}
}
?>
Instance
<?php
Include the Php-excel class
Require (DirName (__file__). "/class-excel-xml.inc.php");
Create a dummy array
$doc = Array (
1 => Array ("Oliver", "Peter", "Paul"),
Array ("Marlene", "Lucy", "Lina")
);
Generate Excel File
$xls = new Excel_xml;
$xls->addarray ($doc);
$xls->generatexml ("mytest");
?>