TP5引用PHPExcel实现导入导出功能

荆轲刺秦王

方法很简单,废话不多说,直接上代码:

common.php 里面:

/*
*导出EXCEL
*/
function phpExcelList($field, $list, $title='文件')
{
    $exts=array('jpg','png','jpeg','gif','bmp');
    vendor('phpExcel.PHPExcel');
    $objPHPExcel = new \PHPExcel();
    $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel); //设置保存版本格式
    /*实例化excel图片处理类*/
    $objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
    

    $objDrawing = new \PHPExcel_Worksheet_Drawing();
    foreach ($list as $key => $value) {
        /*设置表格高度*/
        foreach ($field as $k => $v) {
            
            if ($key == 0) {
                $objPHPExcel->getActiveSheet()->setCellValue($k . '1', $v[1]);
                 /*列宽度自适应*/

                //$objPHPExcel->getActiveSheet()->getColumnDimension($k)->setAutoSize(true);
            }
            /*列宽度*/
                $objPHPExcel->getActiveSheet()->getColumnDimension($k)->setWidth(20);
            $i = $key + 2; //表格是从2开始的
            $array=explode('.', $value[$v[0]]);
            $ext=$array[count($array)-1];
            if(in_array($ext,$exts) && file_exists($value[$v[0]])){
                 
                $objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(100);
                /*实例化插入图片类*/
                $objDrawing = new \PHPExcel_Worksheet_Drawing();
                /*设置图片路径 切记:只能是本地图片*/
                $objDrawing->setPath($value[$v[0]]);
                /*设置图片高度*/
                $objDrawing->setHeight(80);
                //$objDrawing->setWidth(80);
                /*设置图片要插入的单元格*/
                $objDrawing->setCoordinates($k . $i);
                /*设置图片所在单元格的格式*/
                $objDrawing->setOffsetX(10);
                $objDrawing->setOffsetY(10);
                //$objDrawing->setRotation(20);

                $objDrawing->getShadow()->setVisible(true);

                $objDrawing->getShadow()->setDirection(50);

                $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
                
                // $objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            }
            else
            {

                $objPHPExcel->getActiveSheet()->setCellValue($k . $i, $value[$v[0]]);
            }
         
        }

    }
    ob_end_clean();
	header("Content-type:text/html;charset=utf-8");
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
    header("Content-Type:application/force-download");
    header("Content-Type:application/vnd.ms-execl");
    header("Content-Type:application/octet-stream");
    header("Content-Type:application/download");;
    header('Content-Disposition:attachment;filename='.$title.'.xls');
    header("Content-Transfer-Encoding:binary");
//        $objWriter->save($title.'.xls');
    $objWriter->save('php://output');
}

然后在 控制器中直接调用即可:

        /*导出EXCEL*/
	    $field = array(
	    	'A' => array('date', '日期'),
	    	'B' => array('', ''),
	    	'C' => array('', ''),
	    	'D' => array('', ''),
	    	'E' => array('', ''),
	    	'F' => array('', ''),
	    	'G' => array('', ''),
	    	'H' => array('', ''),
	    	'I' => array('', '')
	    );
        //$arr 是二维数组,是数据源
	    phpExcelList($field, $arr, 'Excel名字' . date('Y-m-d H:i:s'));

在我的资源中,我已经上传了 PHPExcel 解压后放在 vendor 目录下即可。

你可能感兴趣的:(thinkphp,php,ThinkPHP5.0)