php 导出excel 一个单元格 多张图片
public function dumpData()
{
error_reporting(0); // 禁止错误信息输出
ini_set('display_errors', 0); // 不显示错误
$limit = $this->request->post('limit', 20, 'intval');
$offset = $this->request->post('offset', 0, 'intval');
$page = floor($offset / $limit) +1 ;
$where = [];
$name = trim($this->request->param('product_type_name'));
$product_type_id = $this->request->param('product_type_id');
$brand_ids = $this->request->param('brand_ids');
if ($name != ''){
$where[] = ['product_type.l_name_zh',"LIKE","%{$name}%"];
}
$brand_ids && $where[] = ['category.brand_ids',"in",$brand_ids];
if (!empty($product_type_id)){
$product_type_ids = $this->model->getChildIds($product_type_id);
if (empty($product_type_ids)) $product_type_ids = [-9999];
$where[] = ['product_type.product_type_id',"in",$product_type_ids];
}
$field = [
'category.category_carousel_id',
'category.product_type_id',
'category.description',
'category.create_time',
'category.update_time',
'category.create_user_id',
'category.update_user_id',
'category.category_carousel_id',
];
$orderby = 'category_carousel_id desc';
$res = CategoryCarouselService::indexList(formatWhere($where),$field,$orderby,$limit,$page);
$list = $res['rows'];
$img_count = array_column($list, 'img_list');
$img_count_list = [];
foreach ($img_count as $v){
$img_count_list[] = count($v);
}
rsort($img_count_list);
$max_img_count = $img_count_list[0] ?? 100;
//开始写入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);
$worksheet->getColumnDimension('E')->setWidth($max_img_count * 20);
$startline=2;
//遍历表单数据
$path = root_path() . 'public/tmpxls/category/';
@mkdir($path);
$arr=['序号','产品总类','产品大类','产品小类','图片列表','产品小类优势','创建时间','更新时间','创建人','更新人'];
foreach($list as $k=>$v){
// 基础偏移量
$offsetX = 10;
$offsetY = 10;
foreach ($v['img_list'] as $index => $img_list){
$img_url = $img_list['file_path_text'] ?? '';
if ($img_url == '') continue;
$line=$k+$startline;
$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 ' . ($index + 1));
$drawing->setDescription('Image ' . ($index + 1));
$drawing->setPath($path.$file); // 设置图片路径
$drawing->setWidth(80); // 设置图片宽度
$drawing->setHeight(80); // 设置图片高度
$drawing->setCoordinates('E'.$line); // 指定单元格
$drawing->setOffsetX($offsetX); // 设置横向偏移
$drawing->setOffsetY($offsetY); // 设置纵向偏移
$drawing->setWorksheet($spreadsheet->getActiveSheet());
// 调整偏移量,确保图片不重叠
$offsetX += 90; // 每张图片向右偏移 50 像素
}
}
//插入行项目
$worksheet->getCell('A'.$line)->setValue(++$k);
$worksheet->getCell('B'.$line)->setValue($v['first_level_name']);
$worksheet->getCell('C'.$line)->setValue($v['second_level_name']);
$worksheet->getCell('D'.$line)->setValue($v['third_level_name']);
$worksheet->getCell('F'.$line)->setValue($v['description']);
$worksheet->getCell('G'.$line)->setValue($v['create_time']);
$worksheet->getCell('H'.$line)->setValue($v['update_time']);
$worksheet->getCell('I'.$line)->setValue($v['create_user_text']);
$worksheet->getCell('J'.$line)->setValue($v['update_user_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');
}
模板
原文地址:https://blog.csdn.net/qq_19598963/article/details/144030006
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!