* excel导出类
*
* 使用方法
$excel=new excel();
* //设置编码:
*$excel->setencode(utf-8,gb2312); //如果不转码,参数写一样即可,例如$excel->setencode(utf-8,utf-8);
* //设置标题栏
* $titlearr=array(a,b,c,d);
* //设置内容栏
* $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; //一般是页面编码
var $outencode; //一般是excel文件的编码
public function __construct(){
}
/**
*设置编码
*/
public function setencode($incode,$outcode){
$this->inencode=$incode;
$this->outencode=$outcode;
}
/**
*设置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;
}
/**
*设置excel内容
*/
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;
}
/**
*生成并自动下载excel
* $titlearr 标题栏数组
* $array 内容数组
* $filename 文件名称 (为空,已当前日期为名称)
*/
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;
}
}
