其他分享
首页 > 其他分享> > tp5 execl导出线上图片

tp5 execl导出线上图片

作者:互联网

 public function excel(){
$list = Db::table('news')->select();
//导出的文件名称
$file_name = date('Y-m-d_H:i:s') . '表格' . '.xls';
$PHPExcel=new \PHPExcel();
$PHPSheet = $PHPExcel->getActiveSheet();

$PHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$PHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$PHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$PHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

// 设置个表格标题
$PHPSheet->setTitle("代理商");
$PHPSheet->setCellValue("A1", "ID");
$PHPSheet->setCellValue("B1", "标题");
$PHPSheet->setCellValue("C1", "分类");
$PHPSheet->setCellValue("D1", "图片地址");

//设置表格宽度
$PHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$PHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$PHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
$PHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(80);

// 设置垂直居中
$PHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$PHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$PHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$PHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

$i = 2;
foreach ($list as $k => $v) {
$PHPSheet->setCellValue('A' . $i, '' . $v['id']);
$PHPSheet->setCellValue('B' . $i, '' . $v['title']);
$PHPSheet->setCellValue('C' . $i, '' . $v['category']);

//获取到图片信息
if ($v['thumbnail_pic_s']) {
// 图片生成
$objDrawing[$k] = new \PHPExcel_Worksheet_MemoryDrawing();
// 截取图片的格式,用不同的方法
$end[$k] = substr($v['thumbnail_pic_s'], -3);

if ($end[$k] == 'jpg' || $end[$k] == 'peg') {
$img[$k] = @imagecreatefromjpeg($v['thumbnail_pic_s']);
} else if ($end[$k] == 'png') {
$img[$k] = @imagecreatefrompng($v['thumbnail_pic_s']);
} else if ($end[$k] == 'gif') {
$img[$k] = @imagecreatefromgif($v['thumbnail_pic_s']);
}

$objDrawing[$k]->setImageResource($img[$k]);
$objDrawing[$k]->setRenderingFunction(\PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT);//渲染方法

$objDrawing[$k]->setMimeType(\PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
}

$objDrawing[$k]->setHeight(20);//照片高度
$objDrawing[$k]->setWidth(70); //照片宽度

$objDrawing[$k]->setCoordinates('D' . $i);

// 图片偏移距离
$objDrawing[$k]->setOffsetX(12);
$objDrawing[$k]->setOffsetY(12);
$objDrawing[$k]->setWorksheet($PHPExcel->getActiveSheet());
// $PHPSheet->setCellValue('D'.$i,''.$value['image_input']);
$i++;
}
$PHPExcel->setActiveSheetIndex(0);
$objWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel2007");
header('Content-Disposition: attachment;filename=' . $file_name);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Cache-Control: max-age=0');
$objWriter->save("php://output"); //文件通过浏览器下载
}

标签:execl,CENTER,objDrawing,PHPSheet,tp5,线上,PHPExcel,setActiveSheetIndex,Alignment
来源: https://www.cnblogs.com/shaoxiong12/p/15632463.html