joshblue Posted June 14, 2008 Share Posted June 14, 2008 Could someone give me an idea on how to export data from PHP (output from a result of an query) to an Excel file Quote Link to comment https://forums.phpfreaks.com/topic/110168-solved-exporting-to-excel/ Share on other sites More sharing options...
winmastergames Posted June 14, 2008 Share Posted June 14, 2008 Explain A Bit More, Are you talking about Form Data or what? Quote Link to comment https://forums.phpfreaks.com/topic/110168-solved-exporting-to-excel/#findComment-565380 Share on other sites More sharing options...
joshblue Posted June 14, 2008 Author Share Posted June 14, 2008 Explain A Bit More, Are you talking about Form Data or what? Ah, ok.. well, basically, I'll be getting the data from a database (MySQL database for example) Quote Link to comment https://forums.phpfreaks.com/topic/110168-solved-exporting-to-excel/#findComment-565383 Share on other sites More sharing options...
winmastergames Posted June 14, 2008 Share Posted June 14, 2008 Hows this <?php define(db_host, “YOUR DATABASE HOST"); define(db_user, “USERNAME"); define(db_pass, “PASSWORD"); define(db_link, mysql_connect(db_host,db_user,db_pass)); define(db_name, “DB_NAME"); mysql_select_db(db_name); $select = “SELECT * FROM TABLE_NAME"; $export = mysql_query($select); $count = mysql_num_fields($export); for ($i = 0; $i < $count; $i++) { $header .= mysql_field_name($export, $i)."t"; } while($row = mysql_fetch_row($export)) { $line = ‘’; foreach($row as $value) { if ((!isset($value)) OR ($value == “")) { $value = “t"; } else { $value = str_replace(’"‘, ‘""‘, $value); $value = ‘"‘ . $value . ‘"‘ . “t"; } $line .= $value; } $data .= trim($line)."n"; } $data = str_replace("r", “", $data); if ($data == “") { $data = “n(0) Records Found!n"; } header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=spreadsheet.xls"); header("Pragma: no-cache"); header("Expires: 0″); print “$headern$data"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/110168-solved-exporting-to-excel/#findComment-565388 Share on other sites More sharing options...
joshblue Posted June 14, 2008 Author Share Posted June 14, 2008 Very thanks for the idea. I really appreciate it... Quote Link to comment https://forums.phpfreaks.com/topic/110168-solved-exporting-to-excel/#findComment-565415 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.