Translate

Tuesday, May 31, 2016

Simple php script for making Excel files

Simple PHP script for making Excel files



This tutorial describes a simple way to generate Microsoft excel file from the php script. As you notice that, there are a lot of complicated external libraries for this purpose (like phpexcel etc.). Here I am describing a simple way to generate excel files without using any external libraries.

Please copy following php code to your web server and run the file. You can see that, a spreadsheet automatically generated there and you can download the same. The contents in the excel file are the same which is given in the code.


<?php
            header( "Content-Type: application/vnd.ms-excel" );
            header( "Content-disposition: attachment; filename=spreadsheet.xls" );
            // print here what you want to see in excel for example:
            echo 'First Name' . "\t" . 'Last Name' . "\t" . 'Mobile' . "\n";
            echo 'Praveen' . "\t" . 'Thappily' . "\t" . '999999999' . "\n";
?>


I believes that this code is self explanatory. The file name should be given in the second header tag. Here the file name is “spreadsheet.xls”. It can be noted that, for changing excel column, you have to use the tab (\t”) switch and similarly to goto the next row you need to use new line switch  (“\n”) in the code as demonstrated above. Figure below shows typical output excel file.

You can use the same file for making excel file from your database (mysql or mssql), just read it using select query and echo the contents. if any doubt please contact us
info.pravysoft@gmail.com
www.pravysoft.org