使用PHPExcel 导出图片范文记录


    /**
     * @param $list
     * @param array $params
     * @param string $imgR 图片插入默认O列
     * @throws PHPExcel_Exception
     * @throws PHPExcel_Reader_Exception
     */
    public function export($list, $params = array(),$imgR='O')
    {
        if (PHP_SAPI == 'cli') {
            exit('This example should only be run from a Web Browser');
        }
        require_once IA_ROOT . '/framework/library/phpexcel/PHPExcel.php';
        $data = m('common')->getSysset('shop');
        $excel = new PHPExcel();
        $excel->getProperties()->setCreator((empty($data['name']) ? '人人商城' : $data['name']))->setLastModifiedBy((empty($data['name']) ? '人人商城' : $data['name']))->setTitle('Office 2007 XLSX Test Document')->setSubject('Office 2007 XLSX Test Document')->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')->setKeywords('office 2007 openxml php')->setCategory('report file');
        //设置居中
        $excel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $excel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $sheet = $excel->setActiveSheetIndex(0);
        $rownum = 1;
        foreach ($params['columns'] as $key => $column) {
            $sheet->setCellValue($this->column($key, $rownum), $column['title']);
            if (!(empty($column['width']))) {
                $sheet->getColumnDimension($this->column_str($key))->setWidth($column['width']);
            }
        }
        ++$rownum;
        $len = count($params['columns']);
        foreach ($list as $k => $row) {
            $i = 0;
            while ($i < $len) {
                $value = ((isset($row[$params['columns'][$i]['field']]) ? $row[$params['columns'][$i]['field']] : ''));
                if ($params['columns'][$i]['field'] == 'img') {
                    //处理图片
                    /* 实例化插入图片类 */
                    $objDrawing = new PHPExcel_Worksheet_Drawing();
                    /* 设置图片路径 切记:只能是本地图片 */
                    $objDrawing->setPath($_SERVER['DOCUMENT_ROOT'] . '/attachment/' . $value);
                    /* 设置图片高度 */
                    $objDrawing->setHeight(50);
                    /* 设置图片要插入的单元格位置 */
                    $objDrawing->setCoordinates($imgR . $rownum); // 这里第二行C列
                    // 写入图片在指定格中的X坐标值
                    $objDrawing->setOffsetX(10);
                    // 写入图片在指定格中的Y坐标值
                    $objDrawing->setOffsetY(10);
                    // 设置旋转角度
                    // $objDrawing->setRotation(20);
                    $objDrawing->getShadow()->setVisible(true);
                    $objDrawing->getShadow()->setDirection(50);
                    $objDrawing->setWorksheet($excel->getActiveSheet());
                } else {
                    $sheet->setCellValue($this->column($i, $rownum), $value);
                }
                ++$i;
            }
            $excel->getActiveSheet()->getRowDimension($rownum)->setRowHeight(50);#设置单元格行高
            $excel->getActiveSheet()->getStyle('A1:F1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            ++$rownum;
        }


        $excel->getActiveSheet()->setTitle($params['title']);
        $filename = urlencode($params['title'] . '-' . date('Y-m-d H:i', time()));
        ob_end_clean();
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment;filename="' . $filename . '.xlsx"');
        header('Cache-Control: max-age=0');
        $writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
        $this->SaveViaTempFile($writer);
        exit();
    }

 

你可能感兴趣的:(PHP,PHPExcel)