Connecting Tech Pros Worldwide Forums | Help | Site Map

export data to xls format

Member
 
Join Date: Dec 2007
Posts: 56
#1: Dec 12 '07
Dear Sir,
Can you tell me how to export data from MySQL database to a xls file using php code?I have to export data from different tables of mysql database to a single xls file.Please help me!!!

Regards,
Sejal Mandaliya

zabsmarty's Avatar
Newbie
 
Join Date: Feb 2007
Location: I am from Pakistan
Posts: 25
#2: Dec 12 '07

re: export data to xls format


Hello Sejal Mandaliya,
I send you the script that make your work easy....

Reply me hows its worked.


Regards
zabsmarty


Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. //include db connection file
  4.  
  5. include 'connection.php';
  6.  
  7. $query = "SELECT first_name, last_name, preferred_name, nick_name, ethnicity, gender, email, mobile, phone, line_1,line_2, subrub, city,country, graduating_university, year_graduation, level_training, level_training_year, hospital_main, vocational_interest_1 FROM members ";
  8.  
  9. $result = mysql_query($query) or die('Error, query failed');
  10.  
  11. $tsv = array();
  12. $html = array();
  13. while($row = mysql_fetch_array($result, MYSQL_NUM))
  14. {
  15.     $tsv[]  = implode("\t", $row);
  16.     $html[] = "<tr><td>" .implode("</td><td>", $row) . "</td></tr>";
  17. }
  18.  
  19. $tsv  = implode("\r\n", $tsv);
  20. $html = "<table>" . implode("\r\n", $html) . "</table>";
  21.  
  22. $fileName = date("d-m-Y").'_mysql.xls';
  23. header("Content-type: application/vnd.ms-excel"); 
  24. header("Content-Disposition: attachment; filename=$fileName");
  25.  
  26. //echo $tsv;
  27. echo $html;
  28.  
  29. //include 'library/closedb.php';
  30. header('location:thanks.php');
  31. ?>
  32.  
  33.  
  34.  


Quote:

Originally Posted by sejal17

Dear Sir,
Can you tell me how to export data from MySQL database to a xls file using php code?I have to export data from different tables of mysql database to a single xls file.Please help me!!!

Regards,
Sejal Mandaliya

Member
 
Join Date: Dec 2007
Posts: 56
#3: Dec 13 '07

re: export data to xls format


Thanks,
Thank you very very much.Your code is working properly.But the lines are not enalbed in xls file.Data is displayed properly but the lines are not enabled in xls file.
zabsmarty's Avatar
Newbie
 
Join Date: Feb 2007
Location: I am from Pakistan
Posts: 25
#4: Dec 15 '07

re: export data to xls format


Thanks to reply, and no problem for thanks, Sejal i does not understand this problem please clarify me so i make to solve this problem for you. reply me soon.

Ragards
zabmsarty



Quote:

Originally Posted by sejal17

Thanks,
Thank you very very much.Your code is working properly.But the lines are not enalbed in xls file.Data is displayed properly but the lines are not enabled in xls file.

pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#5: Dec 23 '07

re: export data to xls format


Heya, Sejal.

Check out the Spreadsheet_Excel_Writer class.
Reply


Similar PHP bytes