hicksjt Posted March 31, 2008 Share Posted March 31, 2008 Hello, Is there a way using PHP to have the fields of a HTML form be written out to a Microsoft Excel CSV file on the server (as part of the form processing) and then have that Excel/CSV file emailed to someone. Thanks for any feedback. This may be a start: <?php $cr = "\n"; $data = "Name" . ',' . "Email" . ',' . "Age" . ',' "Location". $cr; $data .= "$name" . ',' . "$email" . ',' . "$age" . ',' "$location". $cr; $fp = fopen($filename,"a"); // $fp is now the file pointer to file $filename if($fp) { fwrite($fp,$data); // write information to the file fclose($fp); //close the file echo "File Saved Successfully" } else { echo "Error Saving File"; } header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=test.csv"); header("Pragma: no-cache"); header("Expires: 0"); echo $data; ?> Link to comment https://forums.phpfreaks.com/topic/98844-convert-form-information-to-microsoft-excel-or-csv-file/ Share on other sites More sharing options...
hicksjt Posted April 4, 2008 Author Share Posted April 4, 2008 Anyone? Link to comment https://forums.phpfreaks.com/topic/98844-convert-form-information-to-microsoft-excel-or-csv-file/#findComment-509234 Share on other sites More sharing options...
slpctrl Posted April 4, 2008 Share Posted April 4, 2008 <?php //Written by Dan Zarrella. Some additional tweaks provided by JP Honeywell //pear excel package has support for fonts and formulas etc.. more complicated //this is good for quick table dumps (deliverables) include('DB_connection.php'); $result = mysql_query('select * from excel_test', $linkID); $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{ # important to escape any quotes to preserve them in the data. $value = str_replace('"', '""', $value); # needed to encapsulate data in quotes because some data might be multi line. # the good news is that numbers remain numbers in Excel even though quoted. $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/octet-stream"); # replace excelfile.xls with whatever you want the filename to default to header("Content-Disposition: attachment; filename=excelfile.xls"); header("Pragma: no-cache"); header("Expires: 0"); echo $header."\n".$data; ?> http://www.stargeek.com/php_scripts.php?script=2 <-----source Link to comment https://forums.phpfreaks.com/topic/98844-convert-form-information-to-microsoft-excel-or-csv-file/#findComment-509238 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.