therealwesfoster Posted May 2, 2008 Share Posted May 2, 2008 I have 2 questions. 1. How would I go about exporting my MySQL data into a Microsoft Excel file so the user can download it? For instance, the user goes to /download_db.php. I want the file to run a MySQL query which saves all the information to an excel file and allows the user to download it. 2. How would I use PHP to read and write to an excel file? So every time someone enters their email address in a form, the PHP should open /file.xls, read it, append the new email address to the file, and save it. That way the user could just go to /file.xls and read/download the information. Please help on both questions if you can. I'm only going to be doing 1 of the 2, whichever is quicker. Thanks, Wes Link to comment https://forums.phpfreaks.com/topic/103884-export-mysql-to-excel-or-creating-excel-with-php/ Share on other sites More sharing options...
benphp Posted May 2, 2008 Share Posted May 2, 2008 <?php $table = $_POST['table']; $selectmain = "SELECT * FROM $table "; $result = mysql_query($selectmain) or trigger_error("SQL", E_USER_ERROR); $count = mysql_num_fields($result); for ($i = 0; $i < $count; $i++){ $header .= mysql_field_name($result, $i)."\t"; } while($row = mysql_fetch_row($result)){ $line = ''; foreach($row as $value){ if(!isset($value) || $value == ""){ $value = "\t"; }else{ $value = str_replace('"', '', $value); $value = '"\'' . $value . '"' . "\t"; } $line .= $value; } $data .= trim($line)."\n"; } # this line is needed because returns embedded in the data have "\r" # and this looks like a "box character" in Excel $data = str_replace("\r", "", $data); # Nice to let someone know that the search came up empty. # Otherwise only the column name headers will be output to Excel. if ($data == "") { $data = "\nno matching records found\n"; } # This line will stream the file to the user rather than spray it across the screen //header("Content-Type: application/vnd.ms-excel; name='excel'"); header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=excelfile.xls"); header("Pragma: no-cache"); header("Expires: 0"); echo $header."\n".$data; ?> Link to comment https://forums.phpfreaks.com/topic/103884-export-mysql-to-excel-or-creating-excel-with-php/#findComment-531829 Share on other sites More sharing options...
soycharliente Posted May 2, 2008 Share Posted May 2, 2008 That script puts a random quote in front of everything for every field on every row. Why? Does it do that for anyone else? Link to comment https://forums.phpfreaks.com/topic/103884-export-mysql-to-excel-or-creating-excel-with-php/#findComment-531865 Share on other sites More sharing options...
benphp Posted May 2, 2008 Share Posted May 2, 2008 This fixes the extra ' <?php $table = "mytablename"; $data = ""; $header = ""; $selectmain = "SELECT * FROM $table "; $result = mysql_query($selectmain) or trigger_error("SQL", E_USER_ERROR); $count = mysql_num_fields($result); for ($i = 0; $i < $count; $i++){ $header .= mysql_field_name($result, $i)."\t"; } while($row = mysql_fetch_row($result)){ $line = ''; foreach($row as $value){ if(!isset($value) || $value == ""){ $value = "\t"; }else{ $value = str_replace('"', '', $value); $value = $value . "\t"; } $line .= $value; } $data .= trim($line)."\n"; } # this line is needed because returns embedded in the data have "\r" # and this looks like a "box character" in Excel $data = str_replace("\r", "", $data); # Nice to let someone know that the search came up empty. # Otherwise only the column name headers will be output to Excel. if ($data == "") { $data = "\nno matching records found\n"; } # This line will stream the file to the user rather than spray it across the screen //header("Content-Type: application/vnd.ms-excel; name='excel'"); header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=excelfile.xls"); header("Pragma: no-cache"); header("Expires: 0"); echo $header."\n".$data; ?> Link to comment https://forums.phpfreaks.com/topic/103884-export-mysql-to-excel-or-creating-excel-with-php/#findComment-531907 Share on other sites More sharing options...
soycharliente Posted May 2, 2008 Share Posted May 2, 2008 Was it supposed to be there? I was just curious. I picked through the code and changed it. I didn't know if it was a formatting issue that I wasn't aware of. I wasn't trying to nitpick your code. Sorry if it came across like that. Link to comment https://forums.phpfreaks.com/topic/103884-export-mysql-to-excel-or-creating-excel-with-php/#findComment-531934 Share on other sites More sharing options...
benphp Posted May 2, 2008 Share Posted May 2, 2008 I copied the code from a project I did, and there was a reason why it was there, but I forget why. I remember that I was importing/exporting data to/from excel and was diddling with quotation errors - probably had something to do with that. Link to comment https://forums.phpfreaks.com/topic/103884-export-mysql-to-excel-or-creating-excel-with-php/#findComment-531936 Share on other sites More sharing options...
rhodesa Posted May 2, 2008 Share Posted May 2, 2008 A single quote in front of the field forces Excel to treat it as text (and not try to convert it to a number/date/etc)... Link to comment https://forums.phpfreaks.com/topic/103884-export-mysql-to-excel-or-creating-excel-with-php/#findComment-531939 Share on other sites More sharing options...
benphp Posted May 2, 2008 Share Posted May 2, 2008 That's it! I didn't want the dates to be converted to excel's shortened version Link to comment https://forums.phpfreaks.com/topic/103884-export-mysql-to-excel-or-creating-excel-with-php/#findComment-531944 Share on other sites More sharing options...
therealwesfoster Posted May 2, 2008 Author Share Posted May 2, 2008 Awesome, thanks everyone! Especially you ben. I'll put that to good use Wes Link to comment https://forums.phpfreaks.com/topic/103884-export-mysql-to-excel-or-creating-excel-with-php/#findComment-531967 Share on other sites More sharing options...
Chupa85 Posted May 7, 2008 Share Posted May 7, 2008 OK So I am trying to use this code but I have a problem. The code is just spitting out the whole file in the web page and isn't generating an excel file. Any ideas? Link to comment https://forums.phpfreaks.com/topic/103884-export-mysql-to-excel-or-creating-excel-with-php/#findComment-535338 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.