jnvnsn Posted July 28, 2011 Share Posted July 28, 2011 how to append to an excel file? any idea?hehe please help! Quote Link to comment https://forums.phpfreaks.com/topic/243113-append-to-an-excel-file/ Share on other sites More sharing options...
requinix Posted July 28, 2011 Share Posted July 28, 2011 The same way you'd append to any file: open it up with mode=a and write to it. If I had to guess I would say that's not the answer you're looking for, but given the question asked I think I gave the correct response. Quote Link to comment https://forums.phpfreaks.com/topic/243113-append-to-an-excel-file/#findComment-1248650 Share on other sites More sharing options...
jnvnsn Posted July 28, 2011 Author Share Posted July 28, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/243113-append-to-an-excel-file/#findComment-1248656 Share on other sites More sharing options...
xyph Posted July 28, 2011 Share Posted July 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243113-append-to-an-excel-file/#findComment-1248668 Share on other sites More sharing options...
jnvnsn Posted July 28, 2011 Author Share Posted July 28, 2011 yep XLS format. 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. what do you mean by this? Quote Link to comment https://forums.phpfreaks.com/topic/243113-append-to-an-excel-file/#findComment-1248669 Share on other sites More sharing options...
xyph Posted July 28, 2011 Share Posted July 28, 2011 Never mind, there's probably tons of binary data for you to decode. Why not format your files in CSV rather than using Excel formats? Excel can read CSV files without issue. Otherwise, you could try http://phpexcel.codeplex.com/ - This is for Excel 2007 though. Quote Link to comment https://forums.phpfreaks.com/topic/243113-append-to-an-excel-file/#findComment-1248672 Share on other sites More sharing options...
jnvnsn Posted July 28, 2011 Author Share Posted July 28, 2011 My boss wanted it in an excel file. I've tried that one, i think it's more on customizing the cells. Quote Link to comment https://forums.phpfreaks.com/topic/243113-append-to-an-excel-file/#findComment-1248674 Share on other sites More sharing options...
xyph Posted July 28, 2011 Share Posted July 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243113-append-to-an-excel-file/#findComment-1248686 Share on other sites More sharing options...
jnvnsn Posted July 28, 2011 Author Share Posted July 28, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/243113-append-to-an-excel-file/#findComment-1248691 Share on other sites More sharing options...
requinix Posted July 28, 2011 Share Posted July 28, 2011 You're still doing it wrong if you're writing text TSV data into a binary .XLS file. Protip: I know the guy who maintains the PHPExcel project. Give it a try. Quote Link to comment https://forums.phpfreaks.com/topic/243113-append-to-an-excel-file/#findComment-1248692 Share on other sites More sharing options...
jnvnsn Posted July 28, 2011 Author Share Posted July 28, 2011 I am actually studying it right now. Anyway, thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/243113-append-to-an-excel-file/#findComment-1248694 Share on other sites More sharing options...
xyph Posted July 28, 2011 Share Posted July 28, 2011 Are you using Apache, IIS, litehttpd? Try $path = 'C:/some/path/excelreport.xls' Quote Link to comment https://forums.phpfreaks.com/topic/243113-append-to-an-excel-file/#findComment-1248740 Share on other sites More sharing options...
jnvnsn Posted July 29, 2011 Author Share Posted July 29, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/243113-append-to-an-excel-file/#findComment-1249191 Share on other sites More sharing options...
requinix Posted July 29, 2011 Share Posted July 29, 2011 ...You're running it on a Linux/Unix machine. There is no C: drive. The path will be more like /var/www/html/test-1/File/excelreport.xls Quote Link to comment https://forums.phpfreaks.com/topic/243113-append-to-an-excel-file/#findComment-1249198 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.