php
/**
* 导出
*/
public function export()
{
$where = [];
$fullname = $this->request->param("fullname");
$status = $this->request->param("status");
$start_time = $this->request->param("start_time");
$end_time = $this->request->param("end_time");
$fullname && $where[] = ['partner.fullname','LIKE',"%{$fullname}%"];
$status && $where[] = ['feedback.status','=',$status];
if ($start_time != '' && $end_time != ''){
$where[] = ['feedback.create_time','between',[$start_time, $end_time]];
}
$fields = [
"subuser.user_name",
"subuser.partner_id",
"feedback.feedback_id",
"feedback.text",
"feedback.files",
"feedback.status",
"feedback.create_time",
"feedback.reply_time",
"feedback.reply_text",
"partner.fullname",
];
$list = $this->model->alias("feedback")
->leftjoin("subuser subuser","feedback.subuser_id = subuser.subuser_id")
->leftjoin("partner partner","partner.partner_id = subuser.partner_id")
->where($where)
->field($fields)
->order('feedback.feedback_id DESC')
->select()
->toArray();
//开始写入EXCEL
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load(root_path() . 'public/tpl/意见反馈导出模板.xls');//读取指定路径下的模板
$worksheet = $spreadsheet->getActiveSheet();//指向激活的工作表
$worksheet->setTitle('Product catalog');
$count=count($list);
$worksheet->insertNewRowBefore(3,$count-1);
$startline=2;
//遍历表单数据
$path = root_path() . 'public/tmpxls/shop_feedback/';
@mkdir($path);
foreach($list as $k=>$v){
$line=$k+$startline;
// 判断是不是图片
$img_url = $v['files_text'][0] ?? '';
$check_img = $this->model->isImageURL($img_url);
if ($check_img == true){
$fileInfo = pathinfo($img_url);
$file = $fileInfo['filename'] .'.'.$fileInfo['extension'];
//下载图片到本地
if (file_exists( $path.$file) === false) {
copy($img_url, $path.$file);
}
if (!file_exists($path.$file)) {
// 跳过无效图片
continue;
}
//下载图片到本地
if(file_exists($path.$file)===true){
$drawing =new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('Image');
$drawing->setDescription('Image');
$drawing->setPath($path.$file); // 设置图片路径
$drawing->setWidth(80); // 设置图片宽度
$drawing->setHeight(80); // 设置图片高度
$drawing->setCoordinates('E'.$line); // 指定单元格
$drawing->setOffsetX(10); // 设置横向偏移
$drawing->setOffsetY(10); // 设置纵向偏移
$drawing->setWorksheet($spreadsheet->getActiveSheet());
}
}
//插入行项目
$worksheet->getCell('A'.$line)->setValue(++$k);
$worksheet->getCell('B'.$line)->setValue($v['fullname']);
$worksheet->getCell('C'.$line)->setValue($v['user_name']);
$worksheet->getCell('D'.$line)->setValue($v['text']);
$worksheet->getCell('F'.$line)->setValue($v['create_time']);
$worksheet->getCell('G'.$line)->setValue($v['status_text']);
$worksheet->getCell('H'.$line)->setValue($v['reply_text']);
}
$filename = date("YmdHis").'意见反馈';
//下载文档
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename='.$filename.'.Xlsx');
header('Cache-Control: max-age=0');
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
return $writer->save('php://output');
}