Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Php Data to Excel

Status
Not open for further replies.

DarkOne72

Technical User
Jun 14, 2002
210
US
Hello all,

I have a problem I cant figure out; maybe all of you smarter than I peeps can help!!!

I have a simple online signup page that when you hit submit it emails the information that was typed in the fields to a certain email address; this works.
My problem is, is that i also want it to open an existing excel file and input the same information submitted into the next available row in the excel file (not deleting any prior data).
Can someone please help, I have searched the faqs best i could and have been all over the net but they al are talking about a sqldb which i dont need or want.

Here is my code:
Code:
<?php

$where_form_is="[URL unfurl="true"]http://".$_SERVER[/URL]['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));

mail("emailname@domain.com","Signup Form","Form Submission:

Name: " . $_POST['field_1'] . " 
Shift: " . $_POST['field_2'] . " 
Room Selection: " . $_POST['field_3'] . " 
Shirt Size: " . $_POST['field_4'] . " 
Contact Number: " . $_POST['field_5'] . " 
.");


$name = $_POST['field_1'];  
$shift = $_POST['field_2']; 
$room = $_POST['field_3'];  
$shirt = $_POST['field_4'];  
$contact = $_POST['field_5']; 


$filename = "cares.xls"; 
$handle = fopen($filename, 'a'); 


$string_to_add = "$name\n$shift\n$room\n$shirt\n$contact\n";
fwrite($handle, $string_to_add); 

fclose($handle); 

include("Thanks.html");
?>

Thanks in advance
 
I don't think XLS files can be written that way by PHP. You could write new lines to a plain text CSV file, which can be read by Excel.

Google for 'PHP write to CSV' for plenty of examples.
 
I think this tool might be capable of doing what you want. Using a database is almost certainly a better choice, I'm not sure why you wouldn't want to use one.

-----------------------------------------
I cannot be bought. Find leasing information at
 
if you want to output csv information then use the fputcsv

Code:
$fName = 'myfile.csv';
$fields = array('field_1', 'field_2', 'field_3', 'field_4', 'field_5');
foreach ($fields as $field){
  $array[$field] = isset($_POST[$field]) ? trim($_POST[$field]) : '';
}
$fh = fopen ($fName, 'a');
fputcsv($fh, $array);
fclose ($fh);

this should be easily importable to excel. just open the file and excel does the rest.

an alternative is to save the file as if it were an html table (with correct <td></td> tags etc). Excel reads these without a problem. You cannot include formulae using either method however.

 
You can also use the Excel COM object and interact with the Excel program directly to insert your data. This does require that you have Excel installed on the server that is running the script, though.

Here is some code that I wrote when learning VBScript, then modified to work with PHP.

Code:
<?php
//start excel
$excel = new COM("excel.application") or die("Could not launch Excel.");
echo "Loaded Excel, version {$excel->Version}\n";

//add a workbook
$workbook = $excel->Workbooks->Add();

//set the active worksheet
$worksheet = $workbook->Worksheets(1);

//fill in the colors in the cells
for ($i=1; $i<=14; $i++) {
	$excel->Cells($i, 1)->Value = $i;
  $excel->Cells($i, 2)->Interior->ColorIndex = $i;
}

for ($i=15; $i<=28; $i++) {
	$excel->Cells($i - 14, 3)->Value = $i;
  $excel->Cells($i - 14, 4)->Interior->ColorIndex = $i;
}

for ($i=29; $i<=42; $i++) {
	$excel->Cells($i - 28, 5)->Value = $i;
  $excel->Cells($i - 28, 6)->Interior->ColorIndex = $i;
}

for ($i=43; $i<=56; $i++) {
	$excel->Cells($i - 42, 7)->Value = $i;
  $excel->Cells($i - 42, 8)->Interior->ColorIndex = $i;
}

//make it visible
$excel->Visible = true;
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top