Posted By

psradich111 on 09/13/11


Tagged

mysql php download excel


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

guillermoj88gmailcom


PHP MySql to Excel download


 / Published in: PHP
 

  1. <?
  2. // Connect database.
  3. $dbhost = ''; //Host Name
  4. $dbuser = ''; //DB User
  5. $dbpass = ''; //DB password
  6.  
  7. $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
  8.  
  9. $dbname = ''; // DB Name
  10. mysql_select_db($dbname);
  11. // Get data records from table.
  12. $filter = $_GET['ev'];
  13. $result=mysql_query("SELECT * FROM "); //DB Query
  14.  
  15. $titer = str_replace(" ","_",$filter);
  16. // Functions for export to excel.
  17. function xlsBOF() {
  18. echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
  19. return;
  20. }
  21. function xlsEOF() {
  22. echo pack("ss", 0x0A, 0x00);
  23. return;
  24. }
  25. function xlsWriteNumber($Row, $Col, $Value) {
  26. echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
  27. echo pack("d", $Value);
  28. return;
  29. }
  30. function xlsWriteLabel($Row, $Col, $Value ) {
  31. $L = strlen($Value);
  32. echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
  33. echo $Value;
  34. return;
  35. }
  36. header("Pragma: public");
  37. header("Expires: 0");
  38. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  39. header("Content-Type: application/force-download");
  40. header("Content-Type: application/octet-stream");
  41. header("Content-Type: application/download");;
  42. header("Content-Disposition: attachment;filename=Excel.xls "); //File Name
  43. header("Content-Transfer-Encoding: binary ");
  44.  
  45. xlsBOF();
  46.  
  47. /*
  48. Make a top line on your excel sheet at line 1 (starting at 0).
  49. The first number is the row number and the second number is the column, both are start at '0'
  50. */
  51.  
  52. xlsWriteLabel(0,0,"Title"); //Top Title
  53.  
  54. // Make column labels. (at line 3)
  55. xlsWriteLabel(2,0,"Row Name"); // Row Name
  56.  
  57.  
  58.  
  59.  
  60. $xlsRow = 3;
  61.  
  62. // Put data records from mysql by while loop.
  63. while($row=mysql_fetch_array($result)){
  64.  
  65.  
  66. xlsWriteLabel($xlsRow,0,$row['']); //Each DB Row
  67.  
  68.  
  69.  
  70.  
  71. $xlsRow++;
  72. }
  73. xlsEOF();
  74. exit();
  75. ?>

Report this snippet  

You need to login to post a comment.