Home  >  Article  >  Backend Development  >  php export data to execl file format

php export data to execl file format

WBOY
WBOYOriginal
2016-07-25 09:08:271119browse
php将数据导出为execl文件格式
  1. ?session_start();
  2. $sql_excel="";
  3. if(isset($_SESSION["excel"]))
  4. {
  5. $sql_excel=$_SESSION["excel"];
  6. }
  7. //unset($_SESSION["excel"]);
  8. $conn=mysql_connect("localhost","root","");
  9. mysql_select_db("test");
  10. mysql_query("set names utf8",$conn);
  11. $result=mysql_query($sql_excel);
  12. header("Content-type:application/vnd.ms-excel");
  13. header("Content-Disposition:attachment;filename=export_data.xls");
  14. //$result=mysql_query($sql_excel);
  15. $result=mysql_query($sql_excel);
  16. while($arr = mysql_fetch_assoc($result)){
  17. $content[]=$arr;
  18. }
  19. // print_r($content);
  20. $table = '';
  21. $table .= '
  22. ';
  23. $table .= '
  24. ';
  25. foreach($content as $key=>$value){
  26. $table .= "
  27. ";
  28. $table .= "
  29. ';
  30. $table .= "
  31. ';
  32. $table .= "
  33. ';
  34. $table .= "
  35. ';
  36. $table .= "
  37. ';
  38. $table .= "
  39. ';
  40. $table .= "
  41. ';
  42. $table .= '
  43. ';
  44. }
  45. $table .= '
  46. ';
  47. $table .= '
  48. id标题电话地址审核状态内容留言时间
    ".$value['id'].' ".$value['title'].' ".$value['phone'].' ".$value['address'].' ".$value['ischeck'].' ".$value['content'].' ".substr($value['datetime'],0,11).'
    ';
  49. echo $table;
  50. ?>
复制代码
php export data to execl file format
  1. session_start();
  2. $link= mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error());
  3. mysql_query("set names utf8",$link);
  4. mysql_select_db("test");
  5. $sqls = "select * from news order by id desc ";
  6. $_SESSION["excel"]=$sqls;
  7. $rs1 = mysql_query($ sqls);
  8. while($arr = mysql_fetch_assoc($rs1)){
  9. $content[]=$arr;
  10. }
  11. echo ' ';
  12. echo '
  13. ';
  14. echo '
  15. ';
  16. echo '
  17. ';
  18. echo '
  19. ';
  20. echo '
  21. ';
  22. echo '
  23. ';
  24. echo '
  25. ';
  26. echo '
  27. ';
  28. echo '
  29. ';
  30. echo '< /thead>';
  31. echo '
  32. ';
  33. foreach ($content as $k => $v){
  34. echo "
  35. ";
  36. echo "
  37. ";
  38. echo "
  39. ";
  40. echo "
  41. ";
  42. echo "
  43. ";
  44. echo "
  45. ";
  46. echo "
  47. ";
  48. echo "
  49. ";
  50. }
  51. echo '';
  52. echo '
  53. ';
  54. echo '
  55. Video list(index: Display on the homepage lanmu: Display on the video page)
    serial number user uid< /th>';
  56. echo '
  57. Display location File name Video name
  58. echo '
  59. Video introduction Upload time
    $k {$v['title']}< ;/td>";
  60. echo "
  61. {$v['phone']} {$v['address']} {$v['ischeck']} {$v['content']} {$v['datetime']}
    ';
  62. //var_dump($content);
  63. mysql_free_result($ rs1);
  64. mysql_close($link);
  65. ?>
Copy code


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:Multi-content pagingNext article:Multi-content paging