用到工具:PHPEXCEL类,百度可搜索下载
下面是部分代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
<?php date_default_timezone_set('PRC'); function unicodeDecode($unicode_str){ $json = '{"str":"'.$unicode_str.'"}'; $arr = json_decode($json,true); if(empty($arr)) return ''; return $arr['str']; } function do_post($url, $param,$token,$cookie) { $ch = curl_init();//初始化curl //$this_header = array("Content-Type: multipart/form-data"); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($param),$token) ); curl_setopt($ch, CURLOPT_URL,$url);//抓取指定网页 //curl_setopt($ch, CURLOPT_HEADER,$this_header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//执行后直接打印出来,方便调试 curl_setopt($ch, CURLOPT_POST, 1);//post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $param); //curl_setopt($ch, CURLOPT_TIMEOUT,2);//请求超时10s curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 跳过证书检查 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);// 不从证书中检查SSL加密算法是否存在 curl_setopt($ch, CURLOPT_COOKIE, $cookie); $data = curl_exec($ch);//运行curl curl_close($ch); return $data; } function ArrayToExcel($arr,$name) { require_once 'PHPExcel.php'; //实例化 $objPHPExcel = new PHPExcel(); /*右键属性所显示的信息*/ $objPHPExcel->GETProperties()->setCreator("xuan") //作者 ->setLastModifiedBy("xuan") //最后一次保存者 ->setTitle('数据EXCEL导出') //标题 ->setSubject('数据EXCEL导出') //主题 ->setDescription('导出数据') //描述 ->setKeywords("excel") //标记 ->setCategory("result file"); //类别 //设置当前的表格 $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);#设置单元格宽度 $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(8); // 设置表格第一行显示内容 $objPHPExcel->GETActiveSheet() ->setCellValue('A1','所在党支部') ->setCellValue('B1','姓名') ->setCellValue('C1','性别') ->setCellValue('D1','身份证号') ->setCellValue('E1','联系方式'); $key = 1; /*以下就是对处理Excel里的数据,横着取数据*/ foreach($arr as $v){ //设置循环从第二行开始 $key++; //Excel的第A列,name是你查出数组的键值字段,下面以此类推->setCellValue('F'.$key,$v['d1Name']) if($v['a01004']=='1') { $sex='男'; } else { $sex='女'; } $objPHPExcel->GETActiveSheet() ->setCellValue('A'.$key,$v['orgName']) ->setCellValue('B'.$key,$v['a01001']) ->setCellValue('C'.$key,$v['a01004']) ->setCellValue('D'.$key,$v['a01085']) ->setCellValue('E'.$key,$v['a37017']); } //设置当前的表格 $objPHPExcel->GETDefaultStyle()->GETAlignment() //设置居中显示 ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)//垂直居中 ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//水平居中 $objPHPExcel->setActiveSheetIndex(0); ob_end_clean(); //清除缓冲区,避免乱码 header('Content-Type: application/vnd.ms-excel'); //文件类型 header('Content-Disposition: attachment;filename="'.$name.'.xls"'); //文件名 header('Cache-Control: max-age=0'); header('Content-Type: text/html; charset=utf-8'); //编码 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //excel 2003 $objWriter->save('php://output'); exit; } if(isset($_GET['name']) and $_GET['name']!=null) { $name=$_GET['name']; $datas=array(); $token=$_GET['token']; $ss=$_GET['token']; $cookie=$_GET['cookie']; $headers=array($ss); $param='{"pagesize":2000,"pagenum":0,"orgCode":"000002000008000008000009000005","conditions":[],"orgId":"16e53e4f-b521-413a-ba07-37745245bd55","orgType":"611"}'; $url='http://10.243.19.81:7710/api/imis/mainQuery'; $jsondata=do_post($url, $param,$token,$cookie); //echo $jsondata; $datas=json_decode($jsondata,true)['datalist']; ArrayToExcel($datas,$name); } ?> <style> #cookie{ width: 30%; border: 1px solid #e2e2e2; height: 40px; padding:0 0 0 10px; } #header{ width: 69%; border: 1px solid #e2e2e2; height: 40px; padding:0 0 0 10px; } #keywords{ width: 15%; border: 1px solid #e2e2e2; height: 40px; padding:0 0 0 10px; } #search{ width: 10%; border: 1px solid #e2e2e2; height: 42px; text-align: center; line-height: 42px; cursor: pointer; } </style> <form method="GET" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> <input id="keywords" type="search" name="name" placeholder="输入文件名" spellcheck="false" value="<?php if(isset($_GET['name']))echo $_GET['name'];?>" autocomplete="off"/> <input id="cookie" type="cookie" name="cookie" placeholder="cookie" spellcheck="false" value="<?php if(isset($_GET['cookie']))echo $_GET['cookie'];?>" autocomplete="off"/> <input id="token" type="cookie" name="token" placeholder="Authorization" spellcheck="false" value="<?php if(isset($_GET['token']))echo $_GET['token'];?>" autocomplete="off"/> <input id="search" type="submit" value="导出" name="导出"/> </form> |
近期评论