PHP tutorials,a way to getting some thing new in web

Followers

Powered by Blogger.

Friday 5 September 2014

Excel Spreadsheet creation in PHP

No comments :
Spreadsheets are used everywhere and they are most useful to manage and organize data.They are quite popular.

PHP also supports creation of excel spreadsheets using PHP.I am wondered that PHP creates it without any class.


It only needs 4-5 lines of code using php.Mostly \t and \n tags are used.You can generate excel spreadsheets easilyusing PHP. You need to use below code to create excel file.




<?php
    header( "Content-Type: application/vnd.ms-excel" );
    header( "Content-disposition: attachment; filename=spreadsheet.xls" );
   
    // print your data here. note the following:
    // - cells/columns are separated by tabs ("\t")
    // - rows are separated by newlines ("\n")
   
    // for example:
    echo 'First Name' . "\t" . 'Last Name' . "\t" . 'Phone' . "\n";
    echo 'Ruchi' . "\t" . 'Sanghvi' . "\t" . '666-6666' . "\n";
?>


To define content type following line is used:

header( "Content-Type: application/vnd.ms-excel" );

To define its name and download it following line is used:

header( "Content-disposition: attachment; filename=spreadsheet.xls" );

FirstName,LastName and Phone define Header of excel sheet.

Last line defines data to be displayed in excel sheet.

\t and \n is to define space and new line in code accordingly.

No comments :

Post a Comment