Jump to content

Convert Form Information To Microsoft Excel or CSV File


hicksjt

Recommended Posts

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
Share on other sites

<?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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.