Guest User

Untitled

a guest
Sep 14th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. <?php
  2.  
  3. // MySQL to XLS (Excel)
  4. // Author : Marvic R. Macalintal
  5. // September 14, 2018
  6.  
  7. // Basic Usage
  8. mysql_to_xls( "reports.xls" );
  9.  
  10. function mysql_to_xls( $filename ) {
  11.  
  12. // Replace with your database credential
  13. $db_host = 'localhost';
  14. $db_username = 'db username';
  15. $db_password = 'db password';
  16. $db_name = 'db name';
  17. $tbl = 'table name';
  18.  
  19. $con = mysqli_connect( "$db_host", "$db_username", "$db_password", "$db_name" );
  20. $sql = mysqli_query( $con, "SELECT * FROM $tbl" );
  21. while( $row = mysqli_fetch_assoc( $sql ) ) {
  22. $result[] = $row;
  23. }
  24.  
  25. // ------------------------------------
  26. // Process starts here... do not modify
  27.  
  28. $headers = '';
  29. $content = '';
  30.  
  31. header("Content-Type: application/xls");
  32. header("Content-Disposition: attachment; filename=$filename");
  33.  
  34. // Headers
  35. foreach( $result[0] as $key => $value ) {
  36. $headers .= $key . "\t";
  37. }
  38.  
  39. // Content
  40. for( $i = 0; $i <= count( $result ) -1; $i++ ) {
  41. $content .= "\n";
  42. foreach( $result[$i] as $key => $value ) {
  43. $content .= $value . "\t";
  44. }
  45. }
  46.  
  47. echo $headers;
  48. echo $content;
  49.  
  50. }
  51.  
  52. ?>
Add Comment
Please, Sign In to add comment