Posted By

damarev on 03/13/07


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

terminal_fire
vali29


sql to excel


 / Published in: PHP
 

  1. include "db.php";
  2.  
  3. $select = "SELECT * FROM registros_promo ";
  4. $export = mysql_query($select);
  5. $fields = mysql_num_fields($export);
  6.  
  7. for ($i = 0; $i < $fields; $i++) {
  8. $header .= mysql_field_name($export, $i) . "\t";
  9. }
  10.  
  11. while($row = mysql_fetch_row($export)) {
  12. $line = '';
  13. foreach($row as $value) {
  14. if ((!isset($value)) OR ($value == "")) {
  15. $value = "\t";
  16. } else {
  17. $value = str_replace('"', '""', $value);
  18. $value = '"' . $value . '"' . "\t";
  19. }
  20. $line .= $value;
  21. }
  22. $data .= trim($line)."\n";
  23. }
  24. $data = str_replace("\r","",$data);
  25.  
  26. if ($data == "") {
  27. $data = "\n(0) Records Found!\n";
  28. }
  29.  
  30. header("Content-type: application/x-msdownload");
  31. header("Content-Disposition: attachment; filename=registros.xls");
  32. header("Pragma: no-cache");
  33. header("Expires: 0");
  34. print "$header\n$data";

Report this snippet  

You need to login to post a comment.