Saturday, 3 August 2013

Save php file as word and excel

In this article lets see how a simple application form which is designed using PHP can be save as word and excel. Onclick the save as word and save as excel ,this php file can be converted into word as well as excel.

Application form

Name
Father name
Occuption
State
Gender MaleFemale




app_form.php :



<form name="form" action="result.php" method="get">
<table border="0" width="400px" cellpadding="10" cellspacing="0" align="center" id="tbl">
<tr>
<td colspan="2" align="center"><h1>Application form</h1></td>
</tr>
<tr>
<td>Name</td>
<td>
<input type="text" name="name" />
</td>
</tr>
<tr>
<td>Father name</td>
<td><input type="text" name="fname" />
</td></tr>
<tr>
<td>Occuption</td>
<td><input type="text" name="occupation"/>
</td></tr>
<tr>
<td>State</td>
<td>

<select id="on" name="country">
<option id="count">-------select-------</option>
<option id="count">Tamilnadu</option>
<option id="count">Kerala</option>
<option id="count">Andhra Pradesh</option>
<option id="count">Maharastra</option>
<option id="count">Delhi</option>
</select>

</td>
</tr>
<tr>
<td>Gender</td>
<td><input name="gender" type="radio" value="male" checked="checked" />Male<input name="gender" type="radio" value="Female"/>Female</td>
</tr>
<tr> 
<td colspan="3" align="center"><input name="submit" type="submit" value="Save as word" class="submit"/></td>
</tr>
<tr>

<td colspan="3" align="center"><input type="submit" name="submit1" value="Save as excel" class="submit"/></td>

</tr>     
</table>
</form>


style :

<style type="text/css">
#tbl
{
        margin:10% auto;
        color:#666666;
        background: rgb(237,237,237);
        background: -moz-linear-gradient(top, rgba(237,237,237,1) 0%, rgba(222,222,222,1) 100%);
        background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(237,237,237,1)), color-stop(100%,rgba(222,222,222,1)));
        background: -webkit-linear-gradient(top, rgba(237,237,237,1) 0%,rgba(222,222,222,1) 100%);
        background: -o-linear-gradient(top, rgba(237,237,237,1) 0%,rgba(222,222,222,1) 100%);
        background: -ms-linear-gradient(top, rgba(237,237,237,1) 0%,rgba(222,222,222,1) 100%);
        background: linear-gradient(to bottom, rgba(237,237,237,1) 0%,rgba(222,222,222,1) 100%);
        box-shadow:inset 0 0 2px 2px #CCC;
        -moz-border-radius:20px;
        -webkit-border-radius:20px;
        -o-border-radius:20px;
        padding:0;
}
h1{ color:#666666;}     
.submit
{       background: -moz-linear-gradient(top, #727376 0%, #727376 100%);
        background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#727376), color-stop(100%,#727376));
        background: -webkit-linear-gradient(top, #727376 0%,#727376 100%);
        background: -o-linear-gradient(top, #727376 0%,#727376 100%);
        background: -ms-linear-gradient(top, #727376 0%,#727376 100%);
        background: linear-gradient(top, #727376 0%,#727376 100%);
        color:#FFF; border-radius:10px;
 }
</style>

After clicking the button save as word and save as excel, the file will be automatically saved and download in the form of word and excel.Lets see the script for downloading as word and excel.

result.php :

<?
if(isset($_GET['submit']))
{
    echo "<table  width='250' border='1' cellpadding='0' cellpadding='10' cellspacing='0' align='center'  id='tbl'>
<tr><td>Name</td> <td> $_GET[name] </td></tr>
<tr><td>Fname</td> <td> $_GET[fname] </td></tr>
<tr><td>Occupation</td> <td> $_GET[occupation] </td></tr>
<tr><td>State</td> <td> $_GET[country] </td></tr>
<tr><td>Gender</td> <td> $_GET[gender] </td></tr> </table>";

header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; Filename=suganya.doc");
}
?>
<?
if(isset($_GET['submit1']))
{
    echo "<table  width='250' border='1' cellpadding='0' cellpadding='10' cellspacing='0' align='center'  id='tbl'>
<tr><td>Name</td> <td> $_GET[name] </td></tr>
<tr><td>Fname</td> <td> $_GET[fname] </td></tr>
<tr><td>Occupation</td> <td> $_GET[occupation] </td></tr>
<tr><td>State</td> <td> $_GET[country] </td></tr>
<tr><td>Gender</td> <td> $_GET[gender] </td></tr> </table>";

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; Filename=suganya.xsl");

}
?>

Finally if we click on the button save as word and save as excel,for word the filename will be file.doc and for excel, the filename will be file.xsl.