编程语言
首页 > 编程语言> > php excel导出

php excel导出

作者:互联网

/**
 * 导出节目单
 */
public function programExport_ExcelAction($data,$catid)
{
    require_once dirname(__FILE__) . '/PHPExcel.class.php';
    $sheetname = 'sheet1';
    $catname = $this->category_cache[$catid]['catname'];
    $savefile = $catname . "节目单";
    //设置表格头部
    $title = array('ID', '标题', '推荐','资源状态','栏目','版权上线时间','版权下线时间','版权到期时间','审核状态','发布人','更新时间'); //设置要导出excel的表头
    $objPHPExcel = new PHPExcel();
    //设置列头标题
    for ($i = 0; $i < count($title); $i++) {
        $alpha = chr(65 + $i);
        $objPHPExcel->getActiveSheet()->setCellValue($alpha.'1', $title[$i]);
        $objPHPExcel->getActiveSheet()->getColumnDimension($alpha)->setWidth(20);//设置列宽
        $objPHPExcel->getActiveSheet()->getStyle($alpha)->getAlignment()->setWrapText(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()->getStyle($alpha.'1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);//设置颜色
        $objPHPExcel->getActiveSheet()->getStyle($alpha.'1')->getFill()->getStartColor()->setARGB('FF97BAE5');
        $objPHPExcel->getActiveSheet()->getStyle($alpha)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //水平居中
        $objPHPExcel->getActiveSheet()->getStyle($alpha)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //垂直居中
    }
    for($j = 0;$j<count($data);$j++){
        $z = $j + 2;
        $objPHPExcel->getActiveSheet()->setCellValue('A'.$z, $data[$j]['id']);
        $objPHPExcel->getActiveSheet()->setCellValue('B'.$z, $data[$j]['title']);
        //推荐
        $content_model = get_cache('content_model');
        $content_model = $content_model[$this->category_cache[$catid]['modelid']][fields];
        $tuijianarr = $content_model['shouyetuijian'][setting];//地区的设置
        $tuijianarr = string2array($tuijianarr);//把文本专为数组
        $tuijianarr = explode(chr(13), $tuijianarr[content]);//分割数组 分割条件是一行一个
        //分解数组
        $tuijian = string2array($data[$j]['shouyetuijian']);
        $shouyetuijian = '';
        foreach ($tuijianarr as $qt) {
            list($select_name, $select_value) = explode('|', $qt);
            if (in_array($select_value, $tuijian) && $select_value != -1) {
                $shouyetuijian .= ' '.$select_name;
            }
        }
        $objPHPExcel->getActiveSheet()->setCellValue('C'.$z,$shouyetuijian );
        //资源状态
        if($data[$j]['isonline'] == '0'){
            $isonline = '已上架';
        }else if($data[$j]['isonline'] == '1'){
            $isonline = '已下架';
        }else if($data[$j]['isonline'] == '3'){
            $isonline = '';
        }
        $objPHPExcel->getActiveSheet()->setCellValue('D'.$z, $isonline);
        $objPHPExcel->getActiveSheet()->setCellValue('E'.$z, $catname);
        //版权上线时间
        if($data[$j]['uponlinetime']){
            $uponlinetime = date('Y-m-d', $data[$j]['uponlinetime']);
        }else{
            $uponlinetime = '无';
        }
        $objPHPExcel->getActiveSheet()->setCellValue('F'.$z, $uponlinetime);
        //版权下线时间
        if($data[$j]['downonlinetime']){
            $downonlinetime = date('Y-m-d', $data[$j]['downonlinetime']);
        }else{
            $downonlinetime = '无';
        }
        $objPHPExcel->getActiveSheet()->setCellValue('G'.$z, $downonlinetime);
        //版权到期时间
        if($data[$j]['timeoff']){
            $timeoff = $data[$j]['timeoff'];
        }else{
            $timeoff = '无';
        }
        $objPHPExcel->getActiveSheet()->setCellValue('H'.$z, $timeoff);
        //审核状态
        if(!$data[$j]['aduitstate']){
            $aduitstate = '未审核';
        }else if($data[$j]['aduitstate'] == '1'){
            $aduitstate = '审核成功';
        }else if($data[$j]['aduitstate'] == '2'){
            $aduitstate = '待修改';
        }
        $objPHPExcel->getActiveSheet()->setCellValue('I'.$z, $aduitstate);
        $objPHPExcel->getActiveSheet()->setCellValue('J'.$z, $data[$j]['username']);
        $objPHPExcel->getActiveSheet()->setCellValue('K'.$z, date('Y-m-d', $data[$j]['time']));
    }

    $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
    $objWriter->save('php://output');
}

标签:setCellValue,objPHPExcel,excel,导出,getStyle,alpha,php,data,getActiveSheet
来源: https://blog.csdn.net/weixin_35708475/article/details/88819670