tp6框架 导出excel表格附带图片

			$spreadsheet = new Spreadsheet();
			$objSheet = $spreadsheet->getActiveSheet();  //获取当前操作sheet的对象

			// 获取本地文件夹路径
            $dir = $_SERVER['DOCUMENT_ROOT'];

            // Excel body 部分
            foreach ($body as $key => $val) {
                $row = $key + 2;
                $col = 0;
                foreach ($val as $v) {
                    if (strpos($v, 'jpg') === false && strpos($v, 'png') === false && strpos($v, 'jpeg') === false) {
                        $sheet->setCellValue($char_index[$col] . $row, $v);
                    }
                    $col++;
                }
                if ($val['content']) {
                    $this->download($val['content'], $dir, $row, $objSheet, 'H');
                }
                if ($val['budget_content']) {
                    $this->download($val['budget_content'], $dir, $row, $objSheet, 'L');
                }
                if ($val['stock_content']) {
                    $this->download($val['stock_content'], $dir, $row, $objSheet, 'P');
                }
                if ($val['check_content']) {
                    $this->download($val['check_content'], $dir, $row, $objSheet, 'W');
                }

                $objSheet->getRowDimension($row)->setRowHeight(80);
            }

            $objSheet->setTitle($title);

            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="' . $name . '.xlsx"');
            header('Cache-Control: max-age=0');

            $objWriter = IOFactory::createWriter($spreadsheet, 'Xlsx');

            $objWriter->save('php://output');
            exit();
/**
     * 获取图片
     * @param $url
     * @param $path
     * @return array|string
     */
    private function download($val, $dir, $row, $objSheet, $key)
    {
        $content = explode(',', $val);
        foreach ($content as $k => $value) {
            $file_info = pathinfo($value);
            $basename = $file_info['basename'];
            $dirname = $file_info['dirname'];
            is_dir($dir) or mkdir($dir, 0777, true);
            file_put_contents($dir . $dirname . '/' . $basename, file_get_contents($dir . $dirname . '/' . $basename));
            // 引入操作图片类
            $drawing[$row] = new Drawing();
            $drawing[$row]->setName('GoodsCode');
            $drawing[$row]->setDescription('GoodsCode');
            $drawing[$row]->setPath($dir . $value);
            $drawing[$row]->setWidth(30);
            $drawing[$row]->setHeight(80);
            $drawing[$row]->setCoordinates($key . $row);
            $drawing[$row]->setOffsetX(0);
            $drawing[$row]->setOffsetY(0);
            $drawing[$row]->setWorksheet($objSheet);
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值