登录  /  注册
博主信息
博文 51
粉丝 0
评论 1
访问量 81338
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
PhpSpreadsheet导出Excel
鱼的熊掌
原创
1214人浏览过
  1. namespace eq;
  2. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  3. class Excel{
  4. /**
  5. * @description: 使用例子
  6. * @return {*}
  7. */
  8. public static function demo()
  9. {
  10. $fields=[
  11. ['name'=>'名称','field'=>'name'],
  12. ['name'=>'性别','field'=>'sex']
  13. ];
  14. $list=[
  15. ['name'=>'张三','sex'=>'男'],
  16. ['name'=>'李四','sex'=>'女']
  17. ];
  18. $name='测试demo下载'.date('YmdHis');
  19. self::downXlsx($name,$fields,$list);
  20. }
  21. /**
  22. * @description: 下载Excel文档
  23. * @param {*} $name 生成文件名称
  24. * @param {*} $list 数据列表
  25. * @param {*} $fields 字段列表 [['name'=>'名称','field'=>'name'],['name'=>'手机','field'=>'phone']]
  26. * @return {*}
  27. */
  28. public static function downXlsx($name, $fields = [], $list = [])
  29. {
  30. $spreadsheet = new Spreadsheet();
  31. $sheet = $spreadsheet->getActiveSheet();
  32. $letter_all=[];
  33. $letter=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
  34. // 通过fields数量生成Excel A-Z
  35. $item=0;
  36. foreach($fields as $k => $v){
  37. if($item==26){
  38. $item=0;
  39. }
  40. $n=ceil(($k+1)/26);
  41. if($n==1){
  42. array_push($letter_all,$letter[$item]);
  43. }else{
  44. $n=$n-2;
  45. array_push($letter_all,$letter[$n].$letter[$item]);
  46. }
  47. $item++;
  48. }
  49. # 生成首行标题
  50. foreach($fields as $k => $v){
  51. $sheet->setCellValue($letter_all[$k].'1', $v['name']);
  52. }
  53. // 通过传入数据生成Excel
  54. foreach($list as $k => $v){
  55. foreach($fields as $kk => $vv){
  56. switch($vv['field']){
  57. default:$value=$v[$vv['field']];;
  58. }
  59. $sheet->setCellValue($letter_all[$kk].($k+2), $value);
  60. }
  61. }
  62. $names = $name;
  63. //utf-8转unicode格式
  64. $name = iconv('UTF-8', 'UCS-2BE', $name);
  65. $len = strlen($name);
  66. $str = '';
  67. for ($i = 0; $i < $len - 1; $i = $i + 2) {
  68. $c = $name[$i];
  69. $c2 = $name[$i + 1];
  70. if (ord($c) > 0) {
  71. $str .= '\u' . base_convert(ord($c), 10, 16) . str_pad(base_convert(ord($c2), 10, 16), 2, 0, STR_PAD_LEFT);
  72. } else {
  73. $str .= '\u' . str_pad(base_convert(ord($c2), 10, 16), 4, 0, STR_PAD_LEFT);
  74. }
  75. }
  76. // header('Content-Type: application/vnd.ms-excel');
  77. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  78. header('Data-Type: binary');
  79. //前端导出数据根据这个unicode格式解析为中文
  80. header('Data-Filename: ' . $str);
  81. header('Content-Disposition: attachment;filename="' . $names . '.xlsx"');
  82. header('Cache-Control: max-age=0');
  83. header('Access-Control-Expose-Headers:Data-Type,Data-Filename');
  84. //header('Content-Type: application/vnd.ms-excel');
  85. //header('Content-Disposition: attachment;filename="' . $name . '.xlsx"');
  86. //header('Cache-Control: max-age=0');
  87. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
  88. $writer->save('php://output');
  89. exit;
  90. }
  91. }
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学