PHP导出excel,导出图片,设置行高,合并单元格,设置单元格宽度,设置超链接

PHP导出excel,导出图片,设置行高,合并单元格,设置单元格宽度,设置超链接

public function export_contrast()
{
    $search_text = input('goods_name', "");
    $condition = [];
    $condition[] = ['gb.s_goods_id', '<>', 'null' ];
    if (!empty($search_text))
        $condition[] = [ 'gs.goods_name', 'like', '%' . $search_text . '%' ];

    $field = 'mp.*, ABS(cast(gs.cost_price - mp.min_price as decimal(10,2))) diff, gs.goods_image,gs.sku_id,gs.category_id,gs.goods_name,gs.market_price,gs.cost_price,gs.price,gb.s_price,gb.s_bili,gb.s_goods_state,gb.s_skulist';//,gs.mer_id
    $alias = 'mp';
    $goods_table='goods_'.$this->site_id;
    $join = [
        [ 'goods_chi gs', 'gs.mer_id = mp.spu', 'left' ],
        [ "$goods_table gb", 'gs.goods_id = gb.s_goods_id', 'left' ],
    ];
    $order = 'diff desc, mp.id desc';
    $goods_model = new GoodsModel();
    $list = $goods_model->getGoodsContrastAll($condition,$order ,$field, $this->site_id, $alias);
    $list = $list['data'];
    if(!$list)
        return;

    // 实例化excel
    $phpExcel = new \PHPExcel();
    $phpExcel->getProperties()->setTitle("商品价格洞悉");
    $phpExcel->getProperties()->setSubject("商品价格洞悉");

    //设置第一个sheet为工作的sheet
    $phpExcel->setActiveSheetIndex(0);
    $objActSheet = $phpExcel->getActiveSheet();

    // 全局上下左右居中
    $objActSheet->getDefaultStyle()
        ->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT) //水平样式
        ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_TOP); //内容垂直样式

    //单独添加列名称
    $objActSheet->setCellValue('A1', '图片');
    $objActSheet->setCellValue('B1', '商品名称');
    $objActSheet->setCellValue('C1', '价格(元)');
    $objActSheet->setCellValue('D1', '市场价(元)');
    $objActSheet->setCellValue('E1', '成本价(元)');
    $objActSheet->setCellValue('F1', '其他');
    //循环添加数据(根据自己的逻辑)
    $i = 2;
    foreach ($list as $k => $v) {
        //合并单元格
        $market_price_data = json_decode($v[ 'market_price_data' ], true);
        if($market_price_data){
            $num = $i - 1 + count($market_price_data);
            $objActSheet->mergeCells("A{$i}:A{$num}");
            $objActSheet->mergeCells("B{$i}:B{$num}");
            $objActSheet->mergeCells("C{$i}:C{$num}");
            $objActSheet->mergeCells("D{$i}:D{$num}");
            $objActSheet->mergeCells("E{$i}:E{$num}");
        }

        //设置表格的宽度
        $objActSheet->getColumnDimension('A')->setWidth(20);
        $objActSheet->getColumnDimension('B')->setWidth(30);
        $objActSheet->getColumnDimension('C')->setWidth(10);
        $objActSheet->getColumnDimension('D')->setWidth(10);
        $objActSheet->getColumnDimension('E')->setWidth(10);
        $objActSheet->getColumnDimension('F')->setWidth(100);
        $objActSheet->getColumnDimension('G')->setWidth(12);

        //图片设置为超链接
        /*if($v[ 'goods_image' ]) {
            $s = 'A';
            $objActSheet->setCellValue($s . $i, "图片");
            $objActSheet->getCell($s . $i)->getHyperlink()->setUrl($v['goods_image']);
            $objActSheet->getStyle($s . $i)->getFont()->getColor()->setRGB('#0000FF');
        }*/

        //图片导出 会导致文件过大 不实用
        if ($v['goods_image']){
            //$img = substr($v['goods_image'],'-3','3');
            $img1 =  @imagecreatefromjpeg($v['goods_image']);
            if($img1)
            {
                //实例化图片操作类
                $objDrawing  = new \PHPExcel_Worksheet_MemoryDrawing();

                //设置图片存放在表格的位置
                $objDrawing ->setCoordinates('A' . $i);
                $objDrawing -> setImageResource($img1);

                //设置X方向偏移量每一张图片的后面追加一个偏移量
                $objDrawing ->setOffsetX(10);
                //设置Y方向偏移量
                $objDrawing ->setOffsetY(10);
                $objDrawing ->setWorksheet($objActSheet);
                //渲染方法
                $objDrawing->setRenderingFunction(\PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT);
                $objDrawing->setMimeType(\PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
                //设置图片高
                $objDrawing ->setHeight(80);
                //设置图片宽
                $objDrawing ->setWidth(80);
                //设置表格的高度
                $objActSheet->getRowDimension($i)->setRowHeight(90);
            }else{
                $objActSheet->setCellValue('A' . $i, '');
            }

        }

        //数据
        //$objActSheet->setCellValue('A' . $i, $v[ 'goods_image' ]);
        $objActSheet->setCellValue('B' . $i, $v[ 'goods_name' ]);
        $objActSheet->setCellValue('C' . $i, $v[ 'price' ]);
        $objActSheet->setCellValue('D' . $i, $v[ 'market_price' ]);
        $objActSheet->setCellValue('E' . $i, $v[ 'cost_price' ]);
        //$objActSheet->setCellValue('F' . $i, $v[ 'market_price_data' ]);
        if($market_price_data){//处理合并单元格的数据
            foreach ($market_price_data as $value){
                $row = '名称:' . $value['goods_name'] . "\r\n";
                $row .= '价格:' . $value['price'] . "\r\n";
                $row .= '评论数:' . $value['commit'] . "\r\n";
                $row .= '产品链接:' . $value['goods_detail_url'];
                $objActSheet->setCellValue('F' . $i, $row);
                $objActSheet->getStyle('F' . $i)->getAlignment()->setWrapText(true);

                //超链接
                $objActSheet->setCellValue('G' . $i, '产品链接');
                $objActSheet->getCell('G' . $i)->getHyperlink()->setUrl($value['goods_detail_url']);

                // 设置字体大小、加粗、颜色、对齐方式
                $font_style_array = array(
                    'font'  =>  array(
                        //'size'  => 14,
                        //'bold'  => true,
                        'color' => array('rgb' => '409eff'),
                        //'alignment' => array('horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER)
                    )
                );
                $objActSheet->getStyle('G' . $i)->applyFromArray($font_style_array);
                $objActSheet->getRowDimension($i)->setRowHeight(80);#设置单元格行高
                $i++;
            }
        }
    }

    // 重命名工作sheet
    $objActSheet->setTitle('商品价格洞悉');
    // 保存Excel 2007格式文件,保存路径为当前路径,名字为export.xlsx
    $objWriter = \PHPExcel_IOFactory::createWriter($phpExcel, 'Excel2007');
    $file = date('商品价格洞悉表', time()) . '.xlsx';
    $objWriter->save($file);

    // 清空输出缓冲区
    ob_end_clean();

    header("Content-type:application/octet-stream");
    $filename = basename($file);
    header("Content-Disposition:attachment;filename = " . $filename);
    header("Accept-ranges:bytes");
    header("Accept-length:" . filesize($file));
    readfile($file);
    unlink($file);
    exit;
}

你可能感兴趣的:(php,excel,开发语言)