PHP之Excel表格之导入数据

2020-05-07  本文已影响0人  Cute_小肥鸡

说明:由于项目需要,今天用Ajax+PHPExcel进行文件下载(后台获取数据,将数据生成Excel表格,最后通过浏览器进行下载)

参考:https://blog.csdn.net/wyj880220/article/details/48439153

1、页面


2、JQuery

window.open(window.contextPath + "eHome/PrivateLetter/getClassMembers?p_a="+thisParam+"&o_i="+rcm_classList_1[0]['orgID']+"&c_i="+rcm_classList_1[0]['classID']);


3、后台

public function getClassMembers(){

    $param = $this ->input ->get("p_a");

    $orgID = $this ->input ->get("o_i");

    $classID = $this ->input ->get("c_i");

    //获取不同的班级成员列表

    if($param == "stuM"){

        $filename = "学生私信模板";

        $listArr = json_decode( ((new biz\common\EduAPI()) ->getDataByGetPost("/api/users/Stu/getUserInfoByOCId?o_i=".$orgID."&c_i=".$classID,"GET")),true );

        if($listArr["code"] == 200){

            $list = $listArr["data"];

        }else{

            $list = [];

        }

    }else if($param == "parM"){

        $filename = "家长私信模板";

        $listArr = json_decode( ((new biz\common\EduAPI()) ->getDataByGetPost("/api/users/Par/getUserInfoByOCId?o_i=".$orgID."&c_i=".$classID,"GET")),true );

        if($listArr["code"] == 200){

            $list = $listArr["data"];

        }else{

            $list = [];

        }

    }

    //开始将某班级的学生列表写入excel表格

    $CI = & get_instance();

    $CI ->load ->library('PHPExcel');

    $objPHPExcel = new \PHPExcel();

    $objPHPExcel ->getProperties() ->setCreator('http://www.phpernote.com') ->setLastModifiedBy('http://www.phpernote.com') ->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 ->setActiveSheetIndex(0) ->setCellValue('A1','姓名') ->setCellValue('B1','账号(不能修改,不能删除)');

    //开始从数据库提取信息插入Excel表中

    $i = 2;

    foreach ($list as $k =>$v){

        $objPHPExcel ->getActiveSheet() ->setCellValue('A' . $i, $v['FName']);         if($v['FTelephone']) $objPHPExcel ->getActiveSheet() ->setCellValue('B' . $i, $v['FTelephone']);

        if($v['FMail']) $objPHPExcel ->getActiveSheet() ->setCellValue('B' . $i, $v['FMail']); $i++; }

        //下面是设置其他信息

        $objPHPExcel ->getActiveSheet() ->getColumnDimension('B')->setWidth(50);//设置列宽

        $objPHPExcel ->setActiveSheetIndex(0); //设置sheet的起始位置 //开始下载         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');         $objWriter ->save('php://output'); //文件通过浏览器下载

}

上一篇 下一篇

猜你喜欢

热点阅读