PHP 利用PHPExcel类库批量导出数据

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

之前做健康档案系统时,有一个将统计的数据批量导出到Excel的需求,后来实现后就没怎么做记录,最近一个小伙伴问我这块的问题,我觉得有必要将这块的东西写下来和大家分享和学习下,这段时间真的是太忙了,所以博客这块也没能及时更新,望见谅。

下面进入正题,在需求这块,有一个统计展示,统计出按条件搜索出来的所有信息,现在是要将这些数据都导入到Excel表格中,方便给领导看需要的数据。我这块的思路是首先获取表头的数据,获取数据库中对应的数据信息,注意这块的数据要和表头的信息是对应的,然后根据表头信息数量来确定插入的数据量,设置部分Excel表格样式,最后是通过浏览器输出给用户。

下面是部分代码段,由于时间关系,我只解释部分代码段

    

//生成excle文件
function generateExcel($param){
    $objExcel = new PHPExcel();
    $i=0;
    //表头
    $k1="民警编号";
    $k2="民警姓名";
    $k3="性别";
    $k4="出生日期";
    $k5="部门";
    $k6="体检时间";
    $k7="住院时间";
    $k8="出院时间";
    $k9="病种";
    $k10="残疾状况";
    $k11="受伤时间";
    $k12="培训时间";
    $k13="培训结果";
    $k14="资质级别";

    $objExcel->getActiveSheet()->setCellValue('a1', "$k1");
    $objExcel->getActiveSheet()->setCellValue('b1', "$k2");
    $objExcel->getActiveSheet()->setCellValue('c1', "$k3");
    $objExcel->getActiveSheet()->setCellValue('d1', "$k4");
    $objExcel->getActiveSheet()->setCellValue('e1', "$k5");
    $objExcel->getActiveSheet()->setCellValue('f1', "$k6");
    $objExcel->getActiveSheet()->setCellValue('g1', "$k7");
    $objExcel->getActiveSheet()->setCellValue('h1', "$k8");
    $objExcel->getActiveSheet()->setCellValue('i1', "$k9");
    $objExcel->getActiveSheet()->setCellValue('j1', "$k10");
    $objExcel->getActiveSheet()->setCellValue('k1', "$k11");
    $objExcel->getActiveSheet()->setCellValue('l1', "$k12");
    $objExcel->getActiveSheet()->setCellValue('m1', "$k13");
    $objExcel->getActiveSheet()->setCellValue('n1', "$k14");

    $policehealthstatistics = new Policehealth_statistics();
    $links_list = $policehealthstatistics->getConditionInformationExcle($param);
 
    foreach($links_list as $k=>$v) {
        $u1=$i+2;
        /*----------写入内容-------------*/
        $objExcel->getActiveSheet()->setCellValue('a'.$u1, $v["policenum"]);
        $objExcel->getActiveSheet()->setCellValue('b'.$u1, $v["realname"]);
        $objExcel->getActiveSheet()->setCellValue('c'.$u1, $v["sex"]);
        $objExcel->getActiveSheet()->setCellValue('d'.$u1, $v["csrq"]);
        $objExcel->getActiveSheet()->setCellValue('e'.$u1, $v["department"]);
        $objExcel->getActiveSheet()->setCellValue('f'.$u1, $v["tjsj"]);
        $objExcel->getActiveSheet()->setCellValue('g'.$u1, $v["zysj"]);
        $objExcel->getActiveSheet()->setCellValue('h'.$u1, $v["cysj"]);
        $objExcel->getActiveSheet()->setCellValue('i'.$u1, $v["bz"]);
        $objExcel->getActiveSheet()->setCellValue('j'.$u1, $v["cjjb"]);
        $objExcel->getActiveSheet()->setCellValue('k'.$u1, $v["sssj"]);
        $objExcel->getActiveSheet()->setCellValue('l'.$u1, $v["pxsj"]);
        $objExcel->getActiveSheet()->setCellValue('m'.$u1, $v["pxjg"]);
        $objExcel->getActiveSheet()->setCellValue('n'.$u1, $v["zzjb"]);
        $i++;
    }

// 高置列的宽度
    $objExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
    $objExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
    $objExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
    $objExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
    $objExcel->getActiveSheet()->getColumnDimension('E')->setWidth(35);
    $objExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
    $objExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
    $objExcel->getActiveSheet()->getColumnDimension('H')->setWidth(15);
    $objExcel->getActiveSheet()->getColumnDimension('I')->setWidth(15);
    $objExcel->getActiveSheet()->getColumnDimension('J')->setWidth(15);
    $objExcel->getActiveSheet()->getColumnDimension('K')->setWidth(15);
    $objExcel->getActiveSheet()->getColumnDimension('L')->setWidth(15);
    $objExcel->getActiveSheet()->getColumnDimension('M')->setWidth(15);
    $objExcel->getActiveSheet()->getColumnDimension('N')->setWidth(35);

    $objExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BPersonal cash register&RPrinted on &D');
    $objExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objExcel->getProperties()->getTitle() . '&RPage &P of &N');

// 设置页方向和规模
    $objExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
    $objExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
    $objExcel->setActiveSheetIndex(0);
    $timestamp = time();
//导出excel2007文档
//    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
//    header('Pragma:public');
    /*header("Content-Type: application/force-download");
    header('Content-Disposition: attachment;filename="links_out'.$timestamp.'.xlsx"');
    header('Cache-Control: max-age=0');*/

    ob_end_clean();//清除缓冲区,避免乱码
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="详情表(' . date('Ymd-His') . ').xls"');
    header('Cache-Control: max-age=0');
    $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
//    $fileName = "111.xlsx";
    $objWriter->save('php://output');
    exit;

由于表头这块之前是固定的格式,所以我没有用动态获取的方式来处理,通过$objExcel->getActiveSheet()->setCellValue('a'.$u1, $v["policenum"]);将从数据库中提取出的信息写入到Excel中,这块有个注意点在setCellValue中的第一个参数不能同名,因为这块是给表格做标识,同名后会导致后面的数据覆盖前面的数据。后面的设置表格样式大家可以去体会,就是对象调用方法的问题,主要说下最后的那个输出方式。$fileName = "111.xlsx";是将生成好的excle文件命名为111.xlsx并输出到当前文件目录中,如果用$objWriter->save('php://output');则是在浏览器窗口中输出。之前我卡在没法在浏览器中进行输出excle文件,由于时间有点久了,具体的原因现在记得不太清楚了,好像是传给前端是json数据,导致后面文件一直没有出来。

如有说的错误或不准确的地方,希望大家指正

转载于:https://my.oschina.net/zhaoqun/blog/744534

你可能感兴趣的:(PHP 利用PHPExcel类库批量导出数据)