/**
* Excel Export Class
*
* How to use
$excel =new Excel ();
*//Set Code:
* $excel->setencode ("Utf-8", "gb2312"); If you do not transcode, the parameters can be written, such as $excel->setencode ("Utf-8", "utf-8");
*//Set title bar
* $titlearr =array ("A", "B", "C", "D");
*//Set Content Bar
* $contentarr =array (
* 1=>array ("ab", "AC", "ad", "AE"),
* 2=>array ("abc", "ACC", "ADC", "AEC"),
* 3=>array ("abd", "ACD", "Add", "AED"),
* 4=>array ("Abe", "Ace", "Ade", "AEE"),
* );
* $excel->getexcel ($titlearr, $contentarr, "abc");
*/
Class Excel {
var $inEncode; Page encoding is generally
var $outEncode; Encoding of Excel files is generally
Public Function __construct () {
}
/**
* Set the encoding
*/
Public Function Setencode ($incode, $outcode) {
$this->inencode= $incode;
$this->outencode= $outcode;
}
/**
* Set the title bar of Excel
*/
Public Function Settitle ($titlearr) {
$title = "";
foreach ($titlearr as $v) {
if ($this->inencode!= $this->outencode) {
$title. =iconv ($this->inencode, $this->outencode, $v). " \ t ";
}
else{
$title. = $v. " \ t ";
}
}
$title. = "\ n";
return $title;
}
/**
* Set Excel Content
*/
Public Function Setrow ($array) {
$content = "";
foreach ($array as $k = = $v) {
foreach ($v as $vs) {
if ($this->inencode!= $this->outencode) {
$content. =iconv ($this->inencode, $this->outencode, $vs). " \ t ";
}
else{
$content. = $vs. "\ t";
}
}
$content. = "\ n";
}
return $content;
}
/**
* Generate and download Excel automatically
* $titlearr title bar Array
* $array Content Array
* $filename file name (empty, the current date is the name)
*/
Public Function Getexcel ($titlearr, $array, $filename = ") {
if ($filename = = ") {
$filename =date ("y-m-d");
}
$title = $this->settitle ($titlearr);
$content = $this->setrow ($array);
Header ("Content-type:application/vnd.ms-excel");
Header ("Content-disposition:filename=". $filename. ". XLS ");
echo $title;
Echo $content;
}
}
PHP Export Excel Class