欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2024-01-19 20:57:10
...
  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