|
<? Php
/**
* Simple excel generating from PHP5
*
* @ Package Utilities
* @ License http://www.opensource.org/licenses/mit-license.php
* @ Author Oliver Schwarz <oliver.schwarz@gmail.com>
* @ Version 1.0
*/
/**
* Generating excel documents on-the-fly from PHP5
*
* Uses the excel XML-specification to generate a native
* XML document, readable/processable by excel.
*
* @ Package Utilities
* @ Subpackage Excel
* @ Author Oliver Schwarz <oliver.schwarz@vaicon.de>
* @ Version 1.1
*
* @ Todo Issue #4: Internet Explorer 7 does not work well with the given header
* @ Todo Add option to give out first line as header (bold text)
* @ Todo Add option to give out last line as footer (bold text)
* @ Todo Add option to write to file
*/
Class Excel_XML
{
/**
* Header (of document)
* @ Var string
*/
Private $ header = "<? Xml version = "1.0" encoding = "% s"?> N <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 (of document)
* @ Var string
*/
Private $ footer = "</Workbook> ";
/**
* Lines to output in the excel document
* @ Var array
*/
Private $ lines = array ();
/**
* Used encoding
* @ Var string
*/
Private $ sEncoding;
/**
* Convert variable types
* @ Var boolean
*/
Private $ bConvertTypes;
/**
* Worksheet title
* @ Var string
*/
Private $ sWorksheetTitle;
/**
* Constructor
*
* The constructor allows the setting of some additional
* Parameters so that the library may be configured
* One's needs.
*
* On converting types:
* When set to true, the library tries to identify the type
* The variable value and set the field specification for Excel
* Accordingly. Be careful with article numbers or postcodes
* Starting with a '0' (zero )!
*
* @ Param string $ sEncoding Encoding to be used (defaults to UTF-8)
* @ Param boolean $ bConvertTypes Convert variables to field specification
* @ Param string $ sWorksheetTitle Title for the worksheet
*/
Public function _ construct ($ sEncoding = 'utf-8', $ bConvertTypes = false, $ sWorksheetTitle = 'table1 ')
{
$ This-> bConvertTypes = $ bConvertTypes;
$ This-> setEncoding ($ sEncoding );
$ This-> setWorksheetTitle ($ sWorksheetTitle );
}
/**
* Set encoding
* @ Param string Encoding type to set
*/
Public function setEncoding ($ sEncoding)
{
$ This-> sEncoding = $ sEncoding;
}
/**
* Set worksheet title
*
* Strips out not allowed characters and trims
* Title to a maximum length of 31.
*
* @ Param string $ title Title for worksheet
*/
Public function setWorksheetTitle ($ title)
{
$ Title = preg_replace ("/[\ |: |/|? | * | [|]/"," ", $ Title );
$ Title = substr ($ title, 0, 31 );
$ This-> sWorksheetTitle = $ title;
}
/**
* Add row
*
* Adds a single row to the document. If set to true, self: bConvertTypes
* Checks the type of variable and returns the specific field settings
* For the cell.
*
* @ Param array $ array One-dimen1_array with row content
*/
Private function addRow ($ array)
{
$ Cells = "";
Foreach ($ array as $ k => $ v ):
$ Type = 'string ';
If ($ this-> bConvertTypes === true & is_numeric ($ v )):
$ Type = 'number ';
Endif;
$ V = htmlentities ($ v, ENT_COMPAT, $ this-> sEncoding );
$ Cells. = "<Cell> <Data ss: Type =" $ type ">". $ v. "</Data> </Cell> n ";
Endforeach;
$ This-> lines [] = "<Row> n". $ cells. "</Row> n ";
}
/**
* Add an array to the document
* @ Param array 2-dimen1_array
*/
Public function addArray ($ array)
{
Foreach ($ array as $ k => $ v)
$ This-> addRow ($ v );
}
/**
* Generate the excel file
* @ Param string $ filename Name of excel file to generate (...xls)
*/
Public function generateXML ($ filename = 'Excel-export ')
{
// Correct/validate filename
$ Filename = preg_replace ('/[^ aA-zZ0-9 _-]/', '', $ filename );
// Deliver header (as recommended in php manual)
Header ("Content-Type: application/vnd. ms-excel; charset =". $ this-> sEncoding );
Header ("Content-Disposition: inline; filename =" ". $ filename.". xls "");
// Print out document to the browser
// Need to use stripslashes for the damn ">"
Echo stripslashes (sprintf ($ this-> header, $ this-> sEncoding ));
Echo "n <Worksheet ss: Name =" ". $ this-> sWorksheetTitle." "> n <Table> n ";
Foreach ($ this-> lines as $ line)
Echo $ line;
Echo "</Table> n </Worksheet> n ";
Echo $ this-> footer;
}
}
?> |