PhpOffice导出图片到exls 表格

luchao1976
public function index(Request $request){

    $params = $request->all();
    $id = $params['ids'];
    if (strpos($id, ',') !== false) {
         $ids = explode(',',$id);     
    } else{
        $ids =[$id];
    }

    $data = Resources::whereIn('id',$ids)->get();
    if($data){
        $data =  $data->toArray();

        $newExcel = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
        $objSheet = $newExcel->getActiveSheet();
        $newExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
        $newExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
        $newExcel->getActiveSheet()->getColumnDimension('C')->setWidth(80);
        $newExcel->getActiveSheet()->getColumnDimension('D')->setWidth(80);

        $objSheet->setCellValue('A1', 'ID')
                        ->setCellValue('B1', '用户名')
                        ->setCellValue('C1', '图片')
                        ->setCellValue('D1', '时间');
         $k =1;
         $p ="/www/wwwroot/wokerman_qinglv.com/wokerman_qinglv/workerman-admin/plugin/lucQinglv/public/upload/imgs/";
         ///app/lucQinglv/upload/imgs/20231023/653569ec8654.jpg

         foreach($data as $key=>$val) {

                $k = $k + 1;
                $newExcel->getActiveSheet()->getRowDimension($k)->setRowHeight(120);
                $objSheet->setCellValue('A' . $k, $val['user_id'])
                                ->setCellValue('B' . $k, $val['username'])
                                ->setCellValue('D' . $k, $val['created_at']);

                $path = $val['src'];    
                $substring = '/imgs/';
                $result = substr($path, strpos($path, $substring) + strlen($substring));
                $imgsPath =  $p.$result;

                $drawing[$key] = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
                $drawing[$key]->setName('图片');
                $drawing[$key]->setDescription('图片');
                $drawing[$key]->setPath($imgsPath);
                $drawing[$key]->setWidth(80);
                $drawing[$key]->setHeight(80);
                $drawing[$key]->setCoordinates('C' . $k);
                $drawing[$key]->setOffsetX(12);
                $drawing[$key]->setOffsetY(12);
                $drawing[$key]->setWorksheet($newExcel->getActiveSheet());
        }
        //导出

// $filename = 'export';
// $format = 'Xlsx';
// header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
// header("Content-Disposition: attachment;filename=" . $filename . date('Y-m-d') . '.' . strtolower($format));
// header('Cache-Control: max-age=0');
// $objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($newExcel, $format);
// return $objWriter->save('php://output');

        echo public_path();
        $writer = new Xlsx($newExcel);
        $file_path = public_path().'/hello_world.xlsx';
        // 保存文件到 public 下
        $writer->save($file_path);
        // 下载文件
        return response()->download($file_path, '文件名.xlsx');    
    }

}
566 0 0
0个评论

luchao1976

140
积分
0
获赞数
0
粉丝数
2023-06-24 加入
🔝