首页 >后端开发 >php教程 >将mysql数据转换到excel输出的代码

将mysql数据转换到excel输出的代码

WBOY
WBOY原创
2016-07-25 08:43:25796浏览
  1. $DB_Server = "localhost";
  2. $DB_Username = "mydowns";
  3. $DB_Password = "";
  4. $DB_DBName = "mydowns";
  5. $DB_TBLName = "user";
  6. $Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
  7. or die("Couldn@#t connect.");
  8. $Db = @mysql_select_db($DB_DBName, $Connect)
  9. or die("Couldn@#t select database.");
  10. $file_type = "vnd.ms-excel";
  11. $file_ending = "xls";
  12. header("Content-Type: application/$file_type");
  13. header("Content-Disposition: attachment; filename=mydowns.$file_ending");
  14. header("Pragma: no-cache");
  15. header("Expires: 0");
  16. $now_date = date(@#Y-m-d H:i@#);
  17. $title = "数据库名:$DB_DBName,数据表:$DB_TBLName,备份日期:$now_date";
  18. $sql = "Select * from $DB_TBLName";
  19. $ALT_Db = @mysql_select_db($DB_DBName, $Connect)
  20. or die("Couldn@#t select database");
  21. $result = @mysql_query($sql,$Connect)
  22. or die(mysql_error());
  23. echo("$title\n");
  24. $sep = "\t";
  25. for ($i = 0; $i echo mysql_field_name($result,$i) . "\t";
  26. }
  27. print("\n");
  28. $i = 0;
  29. while($row = mysql_fetch_row($result))
  30. {
  31. $schema_insert = "";
  32. for($j=0; $j{
  33. if(!isset($row[$j]))
  34. $schema_insert .= "NULL".$sep;
  35. elseif ($row[$j] != "")
  36. $schema_insert .= "$row[$j]".$sep;
  37. else
  38. $schema_insert .= "".$sep;
  39. }
  40. $schema_insert = str_replace($sep."$", "", $schema_insert);
  41. $schema_insert .= "\t";
  42. print(trim($schema_insert));
  43. print "\n";
  44. $i++;
  45. }
  46. return (true);
  47. ?>
复制代码

mysql, excel


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn