excel导出导入 带图片

 excel导出导入 不带图片

    //导入
    public function addExcel()
    {

        $request=Request::instance();

        $excel = request()->file('excel')->getInfo();//excel为file中的name
        vendor("PHPExcel.Class.PHPExcel.IOFactory");
        $objPHPExcel = \PHPExcel_IOFactory::load($excel['tmp_name']);//读取上传的文件
        $arrExcel = $objPHPExcel->getSheet(0)->toArray();//获取其中的数据

        $word = $arrExcel[0];
        array_shift($arrExcel);

        $data = array();
        $num = count($arrExcel) -1;
        foreach ($arrExcel as $key => $value) {
            $data[] = array(

                'content'=>$arrExcel[$key][0]
            );
        }
        $result = Db::table('exceltable')->insertAll($data);
        if($result){
            $this->success('success');
        }else{
            $this->error('error');
        }
    }


    //导出
    public function excel(){
        $list=Db::table('eb_essas_message')->select();
        $file_name=date('Y-m-d_H:i:s').'.xls';
        dirname(__FILE__);
        Loader::import('PHPExcel.Classes.PHPExcel');
        Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
        $PHPExcel = new \PHPExcel();
        $PHPSheet = $PHPExcel->getActiveSheet();
        $PHPSheet->setTitle("代理商");
        $PHPSheet->setCellValue("A1","ID");
        $PHPSheet->setCellValue("B1","名字");
        $PHPSheet->setCellValue("C1","电话");
        $PHPSheet->setCellValue("D1","编号");
        $i = 2;
        foreach($list as $key => $value){
            $PHPSheet->setCellValue('A'.$i,''.$value['id']);
            $PHPSheet->setCellValue('B'.$i,''.$value['name']);
            $PHPSheet->setCellValue('C'.$i,''.$value['cont']);
            $PHPSheet->setCellValue('D'.$i,''.$value['imag']);

            $i++;
        }
        $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,"Excel2007");
        header('Content-Disposition: attachment;filename='.$file_name);
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        $PHPWriter->save("php://output");


    }

 

 

带图片导出 居中垂直对齐(tp框架 )

 //导出excel
    public function excel()
    {

        //查找要导出的数据表
        $list = Db::table('eb_brand')->select();
        //导出的文件名称
        $file_name = date('Y-m-d_H:i:s') . '表格' . '.xls';
        dirname(__FILE__);
        Loader::import('PHPExcel.Classes.PHPExcel');
        Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
        $PHPExcel = new \PHPExcel();

        $objWriter = new \PHPExcel_Writer_Excel2007($PHPExcel);
        $PHPSheet = $PHPExcel->getActiveSheet();

        //设置水平居中
        $PHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $PHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $PHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $PHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        // 设置个表格标题
        $PHPSheet->setTitle("代理商");
        $PHPSheet->setCellValue("A1", "ID");
        $PHPSheet->setCellValue("B1", "品牌名称");
        $PHPSheet->setCellValue("C1", "排序");
        $PHPSheet->setCellValue("D1", "图片地址");

        //设置表格宽度
        $PHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
        $PHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
        $PHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
        $PHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(80);

        // 设置垂直居中
        $PHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $PHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $PHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $PHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);


        $i = 2;
        foreach ($list as $key => $value) {
            $PHPSheet->setCellValue('A' . $i, '' . $value['id']);
            $PHPSheet->setCellValue('B' . $i, '' . $value['bname']);
            $PHPSheet->setCellValue('C' . $i, '' . $value['sort']);

            //获取到图片信息
            $img = DB::table('eb_brand')->where('id', $value['id'])->field('image_input')->find();

            $objDrawing[$key] = new \PHPExcel_Worksheet_Drawing();
            $objDrawing[$key]->setPath('.' . $img['image_input']);   //这里拼接 . 是因为要在根目录下获取

            $objDrawing[$key]->setHeight(20);//照片高度
            $objDrawing[$key]->setWidth(70); //照片宽度

            $objDrawing[$key]->setCoordinates('D' . $key);

            // 图片偏移距离
            $objDrawing[$key]->setOffsetX(12);
            $objDrawing[$key]->setOffsetY(12);
            $objDrawing[$key]->setWorksheet($PHPExcel->getActiveSheet());
//            $PHPSheet->setCellValue('D'.$i,''.$value['image_input']);
            $i++;
        }


        $PHPExcel->setActiveSheetIndex(0);
        $objWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel2007");
        header('Content-Disposition: attachment;filename=' . $file_name);
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Cache-Control: max-age=0');
        $objWriter->save("php://output");  //文件通过浏览器下载
    }

 

posted @ 2021-02-26 23:28  Conqueror·  阅读(178)  评论(0编辑  收藏  举报