经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
php导出excel表格的使用
来源:cnblogs  作者:春暖花开94  时间:2018/9/27 16:49:53  对本文有异议

网站后台有很多列表数据,常常都会有导出excel表格的需求,和大家分享一个实用的导出excel表格方法;

不多说,上代码;

  1. 1 /**
  2. 2 * @param array $data 要导出的数据
  3. 3 * @param array $title excel表格的表头
  4. 4 * @param string $filename 文件名
  5. 5 */
  6. 6 public function daochu_excel($data=array(),$title=array(),$filename='报表'){//导出excel表格
  7. 7 //处理中文文件名
  8. 8 ob_end_clean();
  9. 9 Header('content-Type:application/vnd.ms-excel;charset=utf-8');
  10. 10
  11. 11 header("Content-Disposition:attachment;filename=export_data.xls");
  12. 12 //处理中文文件名
  13. 13 $ua = $_SERVER["HTTP_USER_AGENT"];
  14. 14
  15. 15 $encoded_filename = urlencode($filename);
  16. 16 $encoded_filename = str_replace("+", "%20", $encoded_filename);
  17. 17 if (preg_match("/MSIE/", $ua) || preg_match("/LCTE/", $ua) || $ua == 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko') {
  18. 18 header('Content-Disposition: attachment; filename="' . $encoded_filename . '.xls"');
  19. 19 }else {
  20. 20 header('Content-Disposition: attachment; filename="' . $filename . '.xls"');
  21. 21 }
  22. 22 header ( "Content-type:application/vnd.ms-excel" );
  23. 23
  24. 24
  25. 25 $html = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
  26. 26 <html xmlns='http://www.w3.org/1999/xhtml'>
  27. 27 <meta http-equiv='Content-type' content='text/html;charset=UTF-8' />
  28. 28 <head>
  29. 29
  30. 30 <title>".$filename."</title>
  31. 31 <style>
  32. 32 td{
  33. 33 text-align:center;
  34. 34 font-size:12px;
  35. 35 font-family:Arial, Helvetica, sans-serif;
  36. 36 border:#1C7A80 1px solid;
  37. 37 color:#152122;
  38. 38 width:auto;
  39. 39 }
  40. 40 table,tr{
  41. 41 border-style:none;
  42. 42 }
  43. 43 .title{
  44. 44 background:#7DDCF0;
  45. 45 color:#FFFFFF;
  46. 46 font-weight:bold;
  47. 47 }
  48. 48 </style>
  49. 49 </head>
  50. 50 <body>
  51. 51 <table width='100%' border='1'>
  52. 52 <tr>";
  53. 53 foreach($title as $k=>$v){
  54. 54 $html .= " <td class='title' style='text-align:center;'>".$v."</td>";
  55. 55 }
  56. 56
  57. 57 $html .= "</tr>";
  58. 58
  59. 59 foreach ($data as $key => $value) {
  60. 60 $html .= "<tr>";
  61. 61 foreach($value as $aa){
  62. 62 $html .= "<td>".$aa."</td>";
  63. 63 }
  64. 64
  65. 65 $html .= "</tr>";
  66. 66
  67. 67 }
  68. 68 $html .= "</table></body></html>";
  69. 69 echo $html;
  70. 70 exit;
  71. 71 }

    $title参数的数据是一个一维数组,如下:
  1. $data参数是一个二维数组,如下:

 

调用方法:

  1. 1 $daochuData = DB::table('scholarship_to_weixin as s')->leftJoin('users as u','s.uid','=','u.id')
  2. 2 ->leftJoin('admin as a','a.id','=','s.tx_checkid')
  3. 3 ->orderBy('s.times','desc')
  4. 4 ->select('s.*','u.nickname','u.tel','u.id as u_id','a.name as a_name','u.admin_beizhu_name')
  5. 5 ->get();
  6. 6
  7. 7 $title = array('序号','申请时间','申请人','备注名称','申请人手机号','提现金额','操作时间','操作人');
  8. 8
  9. 9 $arr = [];
  10. 10 foreach($daochuData as $k=>$v){
  11. 11 $arr[] = array(
  12. 12 $k+1,
  13. 13 $v->times,
  14. 14 $v->nickname,
  15. 15 $v->admin_beizhu_name,
  16. 16 $v->tel,
  17. 17 $v->money,
  18. 18 $v->s_times,
  19. 19 $v->a_name
  20. 20 );
  21. 21 }
  22. 22
  23. 23 $this->daochu_excel($arr,$title,'红包提现到微信记录');

 

结果:

 

  1. 希望对您有帮助。谢谢!

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号