使用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();
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章