phpExcel导出表格

Posted 泪滴

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了phpExcel导出表格相关的知识,希望对你有一定的参考价值。

/**
     * excel下载。单个职位下的求职者
     */
    public function test()
    {

        $deliveryIds = trim($this->_param[‘deliveryIds‘], ‘,‘);

        if(empty($deliveryIds)){
            return json($this->_format_return_data([], FAILED, ‘参数不对‘));
        }
        //取该职位下的用户
        $res = $this->delivery_logic->getExcelList($deliveryIds);
        if (empty($res)) {
            return json($this->_format_return_data([], FAILED, ‘参数不对‘));
        }

        foreach ($res as &$val) {
            //计算年龄
            $val[‘age‘] = $this->getAge(strtotime($val[‘birthDay‘]));
            $val[‘sex‘] = !empty($val[‘sex‘])?config(‘status.sex‘)[$val[‘sex‘]]:‘‘;
            $val[‘education‘] = !empty($val[‘education‘])?config(‘status.education‘)[$val[‘education‘]]:‘‘;
            $val[‘jobType‘] = !empty($val[‘jobType‘])?config(‘status.‘)[$val[‘jobType‘]]:‘‘;
            $val[‘trace‘] = !empty($val[‘trace‘])?config(‘status.trace‘)[$val[‘trace‘]]:‘‘;
            $val[‘yearSalary‘] = !empty($val[‘yearSalary‘])?config(‘status.hopeSalary‘)[$val[‘yearSalary‘]]:‘‘;
        }

        //引入核心文件
        vendor("Util.phpExcel");
        $objPHPExcel = new PHPExcel();

        $objPHPExcel->getProperties()->setCreator(‘http://www.jb51.net‘)
                ->setLastModifiedBy(‘http://www.jb51.net‘)
                ->setTitle(‘Office 2007 XLSX Document‘)
                ->setSubject(‘Office 2007 XLSX Document‘)
                ->setDescription(‘Document for Office 2007 XLSX, generated using PHP classes.‘)
                ->setKeywords(‘office 2007 openxml php‘)
                ->setCategory(‘Result file‘);
        $objPHPExcel->getActiveSheet()->getStyle(‘A1:S1‘)->getFont()->setBold(true);
        $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue(‘A1‘, ‘姓名‘)
                ->setCellValue(‘B1‘, ‘性别‘)
                ->setCellValue(‘C1‘, ‘年龄‘)
                ->setCellValue(‘D1‘, ‘学历‘)
                ->setCellValue(‘E1‘, ‘申请时间‘)
                ->setCellValue(‘F1‘, ‘简历进度‘)
                ->setCellValue(‘G1‘, ‘工作年限‘)
                ->setCellValue(‘H1‘, ‘工作类型‘)
                ->setCellValue(‘I1‘, ‘期望薪资‘);


        $i = 2;

        foreach ($res as $v) {
            $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue(‘A‘ . $i, $v[‘name‘])
                    ->setCellValue(‘B‘ . $i, $v[‘sex‘])
                    ->setCellValue(‘C‘ . $i, $v[‘age‘])
                    ->setCellValue(‘D‘ . $i, $v[‘education‘])
                    ->setCellValue(‘E‘ . $i, $v[‘deliveryTime‘])
                    ->setCellValue(‘F‘ . $i, $v[‘trace‘])
                    ->setCellValue(‘G‘ . $i, $v[‘workExp‘])
                    ->setCellValue(‘H‘ . $i, $v[‘jobType‘])
                    ->setCellValue(‘I‘ . $i, $v[‘yearSalary‘]);
            $i++;
        }

        $objPHPExcel->getActiveSheet()->setTitle(‘商城会员信息‘);
        $objPHPExcel->setActiveSheetIndex(0);
        $filename = urlencode(‘申请者列表‘) . ‘_‘ . date(‘Y-m-dHis‘);
        //*生成xlsx文件
        /* ob_end_clean();//清除缓冲区,避免乱码
          header(‘Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet‘);
          header(‘Content-Disposition: attachment;filename="‘.$filename.‘.xlsx"‘);
          header(‘Cache-Control: max-age=0‘);
          $objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,‘Excel2007‘); */
        /* 生成xls文件 */
        ob_end_clean(); //清除缓冲区,避免乱码
        header(‘Content-Type: application/vnd.ms-excel;charset=utf-8‘);
        header("Content-Disposition:attachment;filename=test_data.xls");
        header(‘Content-Disposition: attachment;filename="‘ . $filename . ‘.xls"‘);
        header(‘Cache-Control: max-age=0‘);
        vendor("Util.PHPExcel");
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘);

        //$objWriter->save($filename);
        $objWriter->save(‘php://output‘);
    }

  

以上是关于phpExcel导出表格的主要内容,如果未能解决你的问题,请参考以下文章

phpexcel导出数据表格

phpexcel--导入excel表格

PHPExcel 导出表格 不知道好不好用

php导出表格两种方法 ——PhpExcel的列子

使用PHPExcel实现数据批量导出为excel表格

phpExcel导出表格