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();
$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');
}
文件模板