Class excel{ /** * Header Excel file (line with prefix) * * XML specifications copied from Excel. * * @ Access Private * @ var series */ var $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" > "; /** * Footer Excel file (attached to line) * * XML specifications copied from Excel. * * @ Access Private * @ var series */ var $footer = "</workbook>"; /** * Document lines (rows in an array) * * @access Private * @var Array */ var $lines = array (); /** Sheet name * * contains a single sheet name * * @ Access Private * @ var series */ var $worksheet _title = "Table1"; /** Add a single line of document string $ * * @ Access Private * @ Palamkumara Array one-dimensional array @ To-do line creation should be done to reduce the-> AddArray */ function AddRow ($array) { Initialize all cells to this row $cells = "";
foreach key-> write value into cells foreach ($array as $k => $v):
Add a string and a number to avoid the generated Excel appears with a warning that the number is stored as a string if (Is_numeric ($v)) { Prevent 0 loss After Excel is generated from the first letter 0 o'clock if (substr ($v, 0, 1) = 0) { $cells. = "<cell><data ss:type=" string ">". $v. "</data></cell>"; } else { $cells. = "<cell><data ss:type=" number ">". $v. "</data></cell>"; } } else { $cells. = "<cell><data ss:type=" string ">". $v. "</data></cell>"; } Endforeach; Transform $cells content into one row $this->lines[] = "<row>". $cells. "</row>"; } /** * Add an array to the document * * This should be the only way to generate an Excel File * * @ Access Public * @ Palamkumara Array of two dimensions @ To-do can be transferred to __construct () later */ function AddArray ($array) { Run through the array and add them into rows foreach ($array as $k => $v): $this->addrow ($v); Endforeach; } /** Set sheet name * * Check the string does not allow characters (:/?*), * Cut it to a maximum of 31 characters and set the title. Damn it * Why characters are not allowed to be found anywhere? Window * Help doesn't help ... * * @ Access Public * @ Palamkumara string $ title Design title */ 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; } /** * Generate Excel File * * The last generated Excel file and use the header () function * provided to the browser. * * @ Access Public * @ Palamkumara string $ file name Excel file to generate (... xls) */ 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 strips tutorial lashes for the damn ">" Echo stripslashes ($this->header); echo "<worksheet ss:name=" ". $this->worksheet_title. "> <table>"; echo "<column ss:index=" 1 "ss:autofitwidth=" 0 "ss:width=" "/>"; echo Implode ("", $this->lines); echo "</table> </worksheet>"; Echo $this->footer; } } |