php tutorial generate excel file source code
class excel {
/ **
* Header excel file (prefix line)
*
* Xml specifications copied from excel.
*
* @ Private access
* @ Reactive string
* /
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 the line)
*
* Xml specifications copied from excel.
*
* @ Private access
* @ Reactive string
* /
var $ footer = "</ workbook>";
/ **
* document lines (rows in an array)
*
* @access private
* @var array
* /
var $ lines = array ();
/ **
Sheet name
*
* Contains a single sheet name
*
* @ Private access
* @ Reactive string
* /
var $ worksheet_title = "table1";
/ **
Add a one-line document string $
*
* @ Private access
* @ Param Kumara array one-dimensional array
* @ To-do list creation should be subtracted -> addarray
* /
function addrow ($ array) {
// initialize all cells for this row
$ cells = "";
// foreach key -> write value into cells
foreach ($ array as $ k => $ v):
// add a string and the number of judgments to avoid the emergence of excel appear as a string of numbers stored warning
if (is_numeric ($ v)) {
// to prevent the first letter is 0 after the generation of excel 0 lost
if (substr ($ v, 0, 1) == 0) {
$ cells. = "<cell> <data ss: type =" string ">". $ v. "</ data> </ cell> n";
} else {
$ cells. = "<cell> <data ss: type =" number ">". $ v. "</ data> </ cell> n";
}
} else {
$ cells. = "<cell> <data ss: type =" string ">". $ v. "</ data> </ cell> n";
}
endforeach
// transform $ cells content into one row
$ this-> lines [] = "<row> n". $ cells. "</ row> n";
}
/ **
* Add an array to the document
*
* This should be the only way to generate an excel
*file.
*
* @ Access is public
* @ Param Kumara array two-dimensional array
* @ To-do can be moved to __construct () later on
* /
function addarray ($ array) {
// run through the array and add them into rows
foreach ($ array as $ k => $ v):
$ this-> addrow ($ v);
endforeach
}
/ **
Set the name of the sheet
*
* Check the string does not allow characters (: /? *),
* Cut its maximum of 31 characters and set the title. Damn it
Why not allow character nowhere? Windows
Help does not help ...
*
* @ Access is public
* @ Param Kumara string $ Title Design Title
* /
function setworksheettitle ($ title) {
// strip out special chars first
$ 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 resulting excel file and use the header () function
* Provided to the browser.
*
* @ Access is public
* @Palm Kumara string $ file name name excel file to generate (... xls) in
* /
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 tutorials lashes 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 =" 110 "/> n";
echo implode ("n", $ this-> lines);
echo "</ table> n </ worksheet> n";
echo $ this-> footer;
}
}
/ **
* Cakephp use method
* Note ** cakephp configuration file define ('debug', 0);
*
* vendor ('excel');
* $ doc = array (
* 0 => array ('China', 'Chinese', 'Chinese people', '123456');
*);
* $ xls = new excel;
* $ xls-> addarray ($ doc);
* $ xls-> generatexml ("mytest");
* /
/ **
* Non-framework to use
*
require_once ('excel.php');
* $ doc = array (
* 0 => array ('China', 'Chinese', 'Chinese people', '123456');
*);
* $ xls = new excel;
* $ xls-> addarray ($ doc);
* $ xls-> generatexml ("mytest");
* /