phpexcel插件:https://pan.baidu.com/s/1rNTs9BWMeA5mzcUepy9RDg
提取码:putk
将phpexcel插件存放到/extend目录中
import('PHPExcel/PHPExcel',EXTEND_PATH);
class MessageControllers extends BaseAdmin{
// 导出
public function exportExcel(){
$objPHPExcel = new \PHPExcel();
$objPHPExcel->createSheet(0);
$objPHPExcel->setActiveSheetIndex(0);
$currentSheet = $objPHPExcel->getActiveSheet();
/**设置部分单元格居中,宽度 */
$letter = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I');
for($i=0;$i<count($letter); $i++){
// 水平居中(位置很重要,建议在最初始位置,这里我举例,ABCDEFG,可以按照自己的需求定义!~)
$objPHPExcel->setActiveSheetIndex(0)->getStyle($letter[$i])->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// 垂直居中
$objPHPExcel->setActiveSheetIndex(0)->getStyle($letter[$i])->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
// 设置个表格宽度(这个一定要设置哦,不然图片会大厨单元格的)
$objPHPExcel->getActiveSheet()->getColumnDimension($letter[$i])->setWidth(20);
//自动换行
$objPHPExcel->getActiveSheet()->getStyle($letter[$i])->getAlignment()->setWrapText(TRUE);
}
//设置第一行的高度
$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(20);
// 将g,h,i单元格合并
$objPHPExcel->getActiveSheet()->mergeCells('G1:I1');
// 设置字体
$objPHPExcel->getActiveSheet()->getStyle('A1:G1')->getFont()->setSize(12); //字体大小
$objPHPExcel->getActiveSheet()->getStyle('A1:G1')->getFont()->setBold(true); //字体加粗
//设置第一行标题
$currentSheet->setCellValue('A1', "序列");
$currentSheet->setCellValue('B1', "用户头像");
$currentSheet->setCellValue('C1', "用户名称");
$currentSheet->setCellValue('D1', "反馈问题");
$currentSheet->setCellValue('E1', "意见建议");
$currentSheet->setCellValue('F1', "发布时间");
$currentSheet->setCellValue('G1', "印证资质");
//定义变量初始化为2,从第二行开始
$idx = 2;
// 数据
$model = new MessageModel();
$list = $model->getAll();
// 循环遍历将数据插入excel中
foreach($list as $key=>$val){
//设置单元格的高度
$objPHPExcel->getActiveSheet()->getRowDimension($idx)->setRowHeight(100);
// $objPHPExcel->getActiveSheet()->getStyle(''.$letter[$key].$idx.':'.$letter[$key].'100'.'')->getAlignment()->setWrapText(TRUE);
//设置数据所在单元格,我这里把图片放在E里
$currentSheet->setCellValue('A' . $idx, $key+1);
$currentSheet->setCellValue('C' . $idx, $val['nickname']);
$currentSheet->setCellValue('D' . $idx, $val['question']);
$currentSheet->setCellValue('E' . $idx, $val['idea']);
$currentSheet->setCellValue('F' . $idx, date('Y-m-d H:i:s',$val['senddate']));
// 实例化图片处理类
$objDrawing = new \PHPExcel_Worksheet_Drawing();
// 处理微信头像
if($val['local_avatarurl'] != ''){
if(file_exists(ROOT_PATH.$val['local_avatarurl'])){
// 图片生成
$objDrawing->setPath(ROOT_PATH.$val['local_avatarurl']);
/*设置图片高度*/
$objDrawing->setHeight(80);
$objDrawing->setWidth(80); //照片宽度
/*图片偏移距离*/
$objDrawing->setOffsetX(10);
$objDrawing->setOffsetY(10);
/*设置图片要插入的单元格*/
$objDrawing->setCoordinates('B'.$idx);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
}
}
//循环添加多个印证资质图片,从g开始到i最多3张图
if($val['imgs'] != null){
$imgs = unserialize($val['imgs']);
$val['imgs'] = $imgs;
for($i=0; $i<count($imgs); $i++){
if(file_exists(ROOT_PATH.$imgs[$i])){
// $objDrawing[$i] = new \PHPExcel_Worksheet_Drawing();
$objDrawing = new \PHPExcel_Worksheet_Drawing();
$objDrawing->setPath(ROOT_PATH.$imgs[$i]); //图片路径
$objDrawing->setCoordinates($letter[$i+6].$idx); //设置图片要插入的单元格
// $objDrawing->setHeight(80); //设置图片高度
$objDrawing->setWidth(80); //设置图片宽度
/*设置图片所在单元格的偏移量*/
$objDrawing->setOffsetX(10);
$objDrawing->setOffsetY(10);
// $objDrawing->setResizeProportional(false);
$objDrawing->setResizeProportional(false);
// $objDrawing->setRotation(1); //设置图片旋转
// $objDrawing->getShadow()->setVisible(true); //设置图片可见
// $objDrawing->getShadow()->setDirection(50); //设置图片方向
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
}else{
break;
}
}
}
$idx++;
}
// dump($list);
// die;
$filename = "./".date('Y-m-d',time())."-".rand(1111,9999).".xls";
$write = new \PHPExcel_Writer_Excel5($objPHPExcel);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl;charset=UTF-8");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");
header('Content-Disposition:attachment;filename='.$filename);
header("Content-Transfer-Encoding:binary");
$write->save('php://output');
// echo $filename;
}
}
<a href="{:url('exportExcel')}" class="layui-btn">全部导出a>
将微信头像下载至本地
/**
* 添加用户头像昵称等信息
* id
* nickname
* avatarurl
* @return array id,nickname,avatarurl
*/
public function addUserinfo(){
$post = $this->request->post();
if(empty($post['id'])){
return output_error(1, '缺少参数id');
}
$model = new UserModel;
$where['id'] = $post['id'];
if(empty($post['avatarurl'])){
return output_error(500, '缺少头像昵称参数');
}
$avatar = substr($post['avatarurl'],strripos($post['avatarurl'],"//")+2); //截取微信头像中的https://
$post['local_avatarurl'] = $this->wx_image($avatar); //图片本地路径
// 检测该id之前是否有数据
$user = $model->getUserWechat(['id'=>$post['id']]);
if($user){
if($user['avatarurl'] != ''){
$res = $user;
}else{
$res = $model->editUserWechat($where, $post);
}
}else{
return output_error(500, '获取用户失败');
}
if($res){
return output_data($res);
}else{
return output_error(1, '数据更新失败');
}
}
//微信头像转换成正常图片格式(如:jpg、gif、jpeg等)
/**
* @param $url 微信图片地址
* @return string 正常图片地址
*/
public function wx_image($url){
$header = array(
'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding: gzip, deflate',);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$data = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
// dump($code);
// die;
if ($code == 200) {//把URL格式的图片转成base64_encode格式的!
$imgBase64Code = "data:image/jpeg;base64," . base64_encode($data);
}
$img_content=$imgBase64Code;//图片内容
if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img_content, $result)) {
$type = $result[2];//得到图片类型png?jpg?gif?
$fileData = base64_decode(str_replace($result[1], '', $img_content));
$image_name = uniqid() .".{$type}";
// $upload_dir = ENV::get('root_path') . 'public/upload/img/';
$upload_dir = ROOT_PATH."uploads/upload/img/";
if (!file_exists($upload_dir)){
mkdir( $upload_dir, 0755, true );
}
$filedir = $upload_dir . '/' . $image_name;
$src = '/uploads/upload/img/' . $image_name;
// $src = config( 'website' ) . 'upload/img/' . $image_name;
if (file_put_contents($filedir,$fileData)) {
return $src;
}
}
}