因为sham做的小程序主要目的是为了收集员工的反馈信息的,为了方便统计汇总,所以需要将数据库的信息导出来生成excel,方便电脑上登记统计,这样就用到PHP生成excel的功能,以下是一个简单快速生成excel表格的代码,虽然是csv格式的。
直接上代码:
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 |
<?php header("Content-type:text/html;charset=utf-8"); $servername = "localhost"; $username = "username"; $password = "password"; $mysqlname = "dbname"; $con = mysqli_connect($servername, $username, $password, $mysqlname); mysqli_query($con,"set names utf8"); if($month =isset($_GET['months'])?$_GET['months']:''){ $months=$_GET['months']; $filename = date("Y年m月").'维修工单.csv'; $sql = "select * from repairs where months='$months'"; }elseif($_GET['all']=="all"){ $sql = "select * from repairs"; $filename = '全部维修工单.csv'; } $res = $con->query($sql); $str = '宿舍号,维修类别,问题详述,工号,姓名,电话,报修月份,报修日期,工单状态,维修结果,维修费用,维修时间,用户评分,用户反馈'."\n"; foreach ($res as $row) { $str .= $row['dm_no'].','.$row['rep_type'].','.$row['rep_desc'].','.$row['fmcid'].','.$row['name'].','.$row['phone'].','.$row['rep_month'].','.$row['rep_date'].','.$row['rep_status'].','.$row['rep_results'].','.$row['rep_cost'].','.$row['rep_resdate'].','.$row['rep_grade'].','.$row['rep_confirm']."\n"; } export_csv($filename, $str); function export_csv($filename, $data) { header("Content-type:text/csv"); header("Content-Disposition:attachment;filename=".$filename); // Content-Disposition指的是属性名,attachment指的是以附件形式下载,filename为默认保存文件名 header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); // Cache-Control:must-revalidate强制不缓存,post-check=0,pre-check=0是IE5中的防缓存 header('Expires:0'); // 不缓存 header('Pragma:public'); echo chr(0xEF).chr(0xBB).chr(0xBF); // 解决乱码 echo $data; } ?> |
这样只要在需要生成excel的地方加1个链接,比如repairs.php?months=2019年07月,就能生成7月份的汇总excel表了,或者 repairs.php? all=all生成所有数据。
不过这个没有测试过数据量大的时候会不会造成卡死,只能等sham小程序数据多的时候测试了。
最新评论