||
/**
* 导出到excel文件
* @author rookie <wanggeng123@vip.qq.com>
* @authorLink www.xiaocainiao.net
*/
class Excel{
/**
* 头部
*/
private $head;
/**
* 内容体
*/
private $body;
/**
* 设置头部
* @param array $arr 一维索引数组
* @author rookie Wanggeng123@vip.qq.com
* @authorlink www.xiaocainiao.net
*/
public function addHeader($arr){
foreach($arr as $headVal){
$headVal = $this->charset($headVal);
$this->head .= "{$headVal}\t ";
}
$this->head .= "\n";
}
/**
* 设置内容
* @param array $arr 二维索引数组
* @author rookie Wanggeng123@vip.qq.com
* @authorlink www.xiaocainiao.net
*/
public function addBody($arr){
foreach($arr as $arrBody){
foreach($arrBody as $bodyVal){
$bodyVal = $this->charset($bodyVal);
$this->body .= "{$bodyVal}\t ";
}
$this->body .= "\n";
}
}
/**
* 下载excel文件
*/
public function downLoad($filename=''){
if(empty($filename)){
$filename = date('YmdHis',time()).'.xls';
} else {
$filename = $filename. '.xls';
}
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header('Content-Type: charset=gbk');
header('Expires: 0');
header('Content-type:application/vnd.ms-excel; charset=gbk');
header("Content-Disposition:attachment; filename=$filename");
$body = '';
if($this->head){
$body .= $this->head;
}
$body .= $this->body;
echo $body;
}
/**
* 编码转换 (由于win下一般是gbk的所以转下码)
* @param type $string
* @return string
*/
public function charset($string){
return iconv("utf-8", "gbk", $string);
}
}接下来是简单的应用
$excel = new Excel();
$head = array('姓名','性别','班级','年龄');$body = array(
array('张三','男','1','12'), array('李四','女','2','12'), array('王五','男','3','12'), array('赵六','女','4','12'), array('王五','男','5','12'), array('李四','女','6','12'), array('哈哈','男','7','12') );
$excel->addHeader($head);
$excel->addBody($body);
$excel->downLoad('班级信息');Archiver|手机版|小黑屋|Discuz!扩展中心 - 杭州富迪文化艺术策划有限公司 版权所有 ( 浙ICP备14042422号-1 )|网站地图QQ机器人
GMT+8, 2025-12-8 12:42 , Processed in 0.669772 second(s), 20 queries , Gzip On.
Powered by Discuz! X5.0
© 2001-2025 Discuz! Team.|IP地址位置数据由 纯真CZ88 提供支持