Home  >  Article  >  Backend Development  >  Code to convert mysql data to excel output

Code to convert mysql data to excel output

WBOY
WBOYOriginal
2016-07-25 08:43:25762browse
  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("$titlen");
  24. $sep = "t";
  25. for ($i = 0; $i < mysql_num_fields($result); $i++) {
  26. echo mysql_field_name($result,$i) . "t";
  27. }
  28. print("n");
  29. $i = 0;
  30. while($row = mysql_fetch_row($result))
  31. {
  32. $schema_insert = "";
  33. for($j=0; $j{
  34. if(!isset($row[$j]))
  35. $schema_insert .= "NULL".$sep;
  36. elseif ($row[$j] != "")
  37. $schema_insert .= "$row[$j]".$sep;
  38. else
  39. $schema_insert .= "".$sep;
  40. }
  41. $schema_insert = str_replace($sep."$", "", $schema_insert);
  42. $schema_insert .= "t";
  43. print(trim($schema_insert));
  44. print "n";
  45. $i++;
  46. }
  47. return (true);
  48. ?>
复制代码

mysql, 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