Advertisement
GWibisono

ekspor data ke excel di php

Apr 8th, 2012
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.33 KB | None | 0 0
  1. <?php
  2. $dbhost = "localhost"; // Usually localhost.
  3. $dbname = "tes"; // Database name.
  4. $dbuser = "root"; // Database username.
  5. $dbpass = ""; // Database password.
  6. $dataname = "web-data"; // FILENAME PREFIX THAT WILL BE ADDED TO XL FILE.
  7. $tablename = "tabel_a"; // name of table to extract data from.
  8. $fields = "id_a, nama, jenkel, pekerjaan"; // the fields in the table you want. seperated with comma. Example ($fields = "field1, field2, field3";)
  9. /*
  10. //////////////////////////////////////////////////
  11. /////////// DO NOT EDIT THIS PART OF FILE ////////
  12. //////////////////////////////////////////////////
  13. */
  14. $datestamp = date("d-m-Y");
  15. $dbh=mysql_connect ($dbhost, $dbuser, $dbpass) or die ('I cannot connect to the database because: ' . mysql_error());
  16. mysql_select_db ($dbname);
  17. $query = ("SELECT $fields FROM $tablename");
  18. $result=mysql_query($query) or die('Error, unsuccesseful when querying database');
  19. $myarray = array();
  20.  
  21. $myarray[] ="<table>";
  22. while($row = mysql_fetch_array($result, MYSQL_NUM))
  23. {
  24.     $myarray[] ="<tr>";
  25.     foreach($row as $v) $myarray[] ="<td>$v</td>";
  26.     $myarray[] ="</tr>";
  27. }
  28. $myarray[] ="</table>";
  29.  
  30.  foreach($myarray as $v)
  31. {
  32.     $s.=$v;
  33. }
  34. $filename = "$dataname-$datestamp.xls";
  35.  
  36. header("Content-Disposition: attachment; filename=$filename");
  37. header("Content-Type: application/vnd.ms-excel");
  38.  
  39. echo $s;
  40. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement