Php to Excel

This is one sweet code to export your variables and pass it on to a downloadable excel file. As of now i am working on an resume maintenance project and that needs this aspect. I googled for the simplest and efficient code.

I use a wamp localhost and faced warnings with all classes and codes that use the fopen(xls://FILENAME) format. This particular code was so cool enough to not read and open any existing file but just provide a  new file that you’ll name at runtime and download.

Its  more like writing the file on flyby!!

If you were one among those who googled for excel to php/php to excel/php write excel files/write excel files in php  and came here, trust me its a standalone simple code below. Just copy paste and it will start working.

<?php
$line1=”ID\tProduct\tColor\tSales\t”;
$line2=”1\tPrinter\tGrey\t13\t”;
$line3=”2\tCD\tBlue\t15\t”;
$line4=”3\tDVD\tRed\t7\t”;
$line5=”4\tMonitor\tGreen\t4\t”;
$line6=”5\tTelephone\tBlack\t2\t”;
$data=”$line1\n$line2\n$line3\n$line4\n$line5\n$line6\n”;

header(“Content-type: application/x-msdownload”);
header(“Content-Disposition: attachment; filename=extraction.xls”);
header(“Pragma: no-cache”);
header(“Expires: 0”);
print “$header\n$data”;

?>

The code is pretty self explanatory with /t for column break and \n for line break.

Cya
Madan

Advertisements

One thought on “Php to Excel

  1. hi madan….!!

    from the past two days i am visiting your blog…..too good dear…nice postings…!!

    the fact is i have just developed a interest in robotics after attending a workshop…m planning to make some bots….and regarding the same found ur blog worthy..!!

    i hav added ur blog to my blog site.. http://anksworld.wordpress.com

    will bother you dear, when i get some probs…..!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s