Blogger Information
Blog 51
fans 0
comment 1
visits 65063
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PhpSpreadsheet导出Excel
鱼的熊掌
Original
1025 people have browsed it
  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. }
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post