将数据导出到excel当中
智慧车行小程序,是一个专门为洗车/4S/车辆维修行业打造的小程序,前后端完整代码包括车行动态,养车常识,保养预约,维修预约,洗车美容预约,汽车检测预约等功能。采用腾讯提供的小程序云开发解决方案,无须服务器和域名预约管理:开始/截止时间/人数均可灵活设置,可以自定义客户预约填写的数据项预约凭证:支持线下到场后校验签到/核销/二维码自助签到等多种方式详尽的预约数据:支持预约名单数据导出Excel,打印
0
<?php
/**
* 导出到excel文件(一般导出中文的都会乱码,需要进行编码转换)
* 使用方法如下
* $excel = new Excel();
* $excel->addHeader(array('列1','列2','列3','列4'));
* $excel->addBody(
array(
array('数据1','数据2','数据3','数据4'),
array('数据1','数据2','数据3','数据4'),
array('数据1','数据2','数据3','数据4'),
array('数据1','数据2','数据3','数据4')
)
);
* $excel->downLoad();
*/
class Excel{
private $head;
private $body;
/**
*
* @param type $arr 一维数组
*/
public function addHeader($arr){
foreach($arr as $headVal){
$headVal = $this->charset($headVal);
$this->head .= "{$headVal} ";
}
$this->head .= "
";
}
/**
*
* @param type $arr 二维数组
*/
public function addBody($arr){
foreach($arr as $arrBody){
foreach($arrBody as $bodyVal){
$bodyVal = $this->charset($bodyVal);
// 过滤特殊字符
$bodyVal = str_replace(array('\n','\t','<br>','<br />', '<br>','</br>'), "", $bodyVal);
$bodyVal = str_replace(array("rn", "r", "n"), "", $bodyVal);
$bodyVal =preg_replace("{ }","",$bodyVal);
$bodyVal=preg_replace("{
}","",$bodyVal);
$bodyVal=preg_replace("{
}","",$bodyVal);
$bodyVal=preg_replace("{
}","",$bodyVal);
$bodyVal = str_replace(",", " ",$bodyVal);
$this->body .= "{$bodyVal} ";
}
$this->body .= "
";
}
}
/**
* 下载excel文件
*/
public function downLoad($filename=''){
if(!$filename)
$filename = date('YmdHis',time()).'.xls';
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=$filename");
header("Content-Type:charset=gb2312");
if($this->head)
echo $this->head;
echo $this->body;
}
/**
* 编码转换
* @param type $string
* @return string
*/
public function charset($string){
return iconv("utf-8", "gb2312", $string);
}
}
?>
全网最新最细最实用WPS零基础入门到精通全套教程!带你真正掌握WPS办公! 内含Excel基础操作、函数设计、数据透视表等
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号