Thinkphp3.2.3整合PHPexcel进行导入导出操作

3045
前端按钮
<a class="button excel" _href="{:U(CONTROLLER_NAME.'/excel')}">导出Excel</a>


AJAX提交
$(function () {
    $(".excel").on("click",function () {
      var data = $(".inputcheckbox:checked").map(function (i,e){return $(e).val()}).get().join(','),
    _url = $(this).attr("_href");
$.post(_url,{data:data},function(data){
      if (data.status) {
          window.location.href = "{:U(CONTROLLER_NAME.'/excel')}/excid/"+data.excid;
     }
      },'json');
  });
})


控制器方法:
//导出excel
 public function excel () {
       //处理勾选内容
        if (IS_AJAX) {
            $this->ajaxReturn(array('excid'=>I('post.data',0),'status'=>1));
        }
        $excid = I('get.excid');
        if (empty($excid)) {
            $data = $this->feedback->select(); //整表查询
        } else {
            $data = $this->feedback->where(array('id'=>array('IN',explode(",", $excid))))->select(); //条件查询
        }
        $fields = $this->feedback->getDbFields(); //获取数据表 字段
        $name = C('name') ? C('name') : 'Excel'.date('Y-m-d'); //导出Excel名称
        $this->exportExcel($data, C('name'), $fields); //开始导出
    }
//导出方法
 public function exportExcel($data, $savefile = null, $title = null, $sheetname = 'sheet1') { 
        Vendor('Excel.PHPExcel');
        //若没有指定文件名则为当前时间戳 
        if (is_null($savefile)) { 
            $savefile = time(); 
        } 
        //若指字了excel表头,则把表单追加到正文内容前面去 
        if (is_array($title)) { 
            array_unshift($data, $title); 
        } 
        $objPHPExcel = new \PHPExcel(); 
        //Excel内容 
        $head_num = count($data); 
     
        foreach ($data as $k => $v) { 
            $obj = $objPHPExcel->setActiveSheetIndex(0); 
            $row = $k + 1; //行 
            $nn = 0; 
     
            foreach ($v as $vv) { 
                $col = chr(65 + $nn); //列 
                $obj->setCellValue($col . $row, $vv); //列,行,值 
                $nn++; 
            } 
        } 
        //设置列头标题 
        for ($i = 0; $i < $head_num - 1; $i++) { 
            $alpha = chr(65 + $i); 
            $objPHPExcel->getActiveSheet()->getColumnDimension($alpha)->setAutoSize(true); //单元宽度自适应  
            $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getFont()->setName("Candara");  //设置字体 
            $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getFont()->setSize(12);  //设置大小 
            $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getFont()->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_BLACK); //设置颜色 
            $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //水平居中 
            $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER); //垂直居中 
            $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getFont()->setBold(true); //加粗 
        } 
     
        $objPHPExcel->getActiveSheet()->setTitle($sheetname); //题目 
        $objPHPExcel->setActiveSheetIndex(0); //设置当前的sheet   
        header('Content-Type: application/vnd.ms-excel'); 
        header('Content-Disposition: attachment;filename="' . $savefile . '.xls"'); //文件名称 
        header('Cache-Control: max-age=0'); 
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); //Excel5 Excel2007 
        $objWriter->save('php://output');
        $this->success("导出Excel成功",U('Feedback/index')); 
    }