Home  >  Article  >  Backend Development  >  PHP implements a general method for exporting Excel files

PHP implements a general method for exporting Excel files

WBOY
WBOYOriginal
2016-07-25 08:45:181155browse
  1. /**
  2. * Export data to excel table
  3. *@param $data A two-dimensional array, the structure is the same as the array found from the database
  4. *@param $title The title of the first row of excel, an array, if it is empty, there will be no title
  5. * @param $filename Downloaded file name
  6. *@examlpe
  7. $stu = M ('User');
  8. $arr = $stu -> select();
  9. exportexcel($arr,array('id','account ','Password','Nickname'),'File name!');
  10. */
  11. function exportexcel($data=array(),$title=array(),$filename='report'){
  12. header("Content-type:application/octet-stream");
  13. header("Accept-Ranges:bytes");
  14. header("Content-type:application/vnd.ms-excel");
  15. header("Content-Disposition:attachment;filename=".$filename.".xls");
  16. header("Pragma: no-cache");
  17. header("Expires: 0");
  18. //导出xls 开始
  19. if (!empty($title)){
  20. foreach ($title as $k => $v) {
  21. $title[$k]=iconv("UTF-8", "GB2312",$v);
  22. }
  23. $title= implode("t", $title);
  24. echo "$titlen";
  25. }
  26. if (!empty($data)){
  27. foreach($data as $key=>$val){
  28. foreach ($val as $ck => $cv) {
  29. $data[$key][$ck]=iconv("UTF-8", "GB2312", $cv);
  30. }
  31. $data[$key]=implode("t", $data[$key]);
  32. }
  33. echo implode("n",$data);
  34. }
  35. }
复制代码

PHP, Excel


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php zoom image codeNext article:php zoom image code