Jump to content

append to an excel file


jnvnsn

Recommended Posts

I tried it but no luck.

 

here is the code if it can help:

 

$filename ="excelreport.xls";
$path = 'C:\some\path\here\excelreport.xls';

if(file_exists($path)):	
	$fh = fopen($filename, 'a') or die("can't open file");
	$contents = "name \t companyName \t address \t num \t email \t url \t \n";
	fwrite($fh, $contents);
	fclose($fh);
	echo 'file exist';
else:

	echo 'does not exist';
endif;

Link to comment
Share on other sites

Well, excel doesn't do tab delimited formats in an XLS file. Are you using the XLS format? XLSX? What version of Excel would you like to write to?

 

I suggest opening the Excel file with a text editor to see how it's formatted, or use a standard that excel supports like CSV.

Link to comment
Share on other sites

Unless you're ready to learn the Excel document format and it's intricacies, you're kinda S.O.L. :(

 

You can't just 'append' to the end of an excel file... you have to find out where the last cell of information is so you can start creating new cells below it.

 

Again, I think you should just use the CSV format. Excel can open those fine. Here's an example of a CSV file

http://seepeoplesoftware.com/downloads/older-versions/11-sample-csv-file-of-us-presidents.html

 

Very easy to append information on the end, and very easy to manipulate.

Link to comment
Share on other sites

i got it actually. :)

using this:

$filename ='C:\some\path\excelreport.xls';
$path = 'C:\some\path\excelreport.xls';

if(file_exists($path)):
	$fh = fopen($filename, "a") or die("can't open file");
	$contents = "name \t companyName \t address \t num \t email \t url \t \n";
	fwrite($fh, $contents);
	fclose($fh);
	echo 'file exist';
else:

	echo 'does not exist';
endif;

 

but i'm having trouble on this one:

$filename ="excelreport.xls";
$path =  'C:\some\path\excelreport.xls';

if(file_exists($path)) {

	$filepath = 'C:\some\path\excelreport.xls';
	$fh = fopen($filepath, "a") or die("can't open file");
	$write = "$name \t $companyName \t $address \t $num \t $email \t $url \t \n";
	fwrite($fh, $write);
	fclose($fh);
	exit;

} else {

	$contents = "NAME \t COMPANY NAME \t ADDRESS \t PHONE NUMBER \t EMAIL \t URL \t \n";
	$contents .= "$name \t $companyName \t $address \t $num \t $email \t $url \t \n";
	header("Pragma: public");
	header("Expires: 0");
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header("Content-Type: application/force-download");
	header("Content-Type: application/octet-stream");
	header("Content-Type: application/download");
	header('Content-type: application/vnd.ms-excel');
	header("Content-Disposition: attachment;filename=".$filename);
	header("Content-Transfer-Encoding: binary ");
	echo $contents;

}

 

 

it executes the else statement even though the file exist.

any ideas?

Link to comment
Share on other sites

still no luck, xyph.having this error:

 

Warning: fopen(C:/some/path/excelreport.xls) [function.fopen]: failed to open stream: No such file or directory in /var/www/html/test-1/File/export.php on line 54

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.