How to fetch data in excel or generate excel file in PHP

File structure for this tutorial :
  config.php (database connection file)
  index.php (For fetching data )
  genrate-excel.php (For genrating excel file )
Create a sql table tblemployee.
Structure of sql table employe .

Now insert some data into this table.

Index.php in this file we will  read the data from database.

genrate-excel.php in this file we will fetch the data from database and generate excel file for the same data.

header(“Content-type: application/octet-stream”);
header(“Content-Disposition: attachment; filename=”.$filename.”-Report.xls”);
The content-type should be whatever it is known to be, if you know it. application/octet-stream is defined as “arbitrary binary data” in RFC 2046″.
Means “I don’t know what the hell this is. Please save it as a file, preferably named $filename.”-Report.xls”.
header(“Pragma: no-cache”);
cache-control is the HTTP/1.1 implementation . cache-control  used to prevent the client from caching the response.
header(“Expires: 0”);
The Expires header specifies when content will expire, or how long content is “fresh.” After this time, the portal server will always check back with the remote server to see if the content has changed.
Expires: 0
The value 0 indicates that the content expires immediately and would have to be re-requested before being displayed again.


View Demo

How to run this script

1. Download and Unzip file on your local system.
2. Put this file inside root directory
3. Database Configuration

Database Configuration

Open phpmyadmin
Create Database empdata
Import database empdata.sql (available inside zip package)
Open Your browser put inside browser “http://localhost/phpexcl”

Download Full Source Code

How to fetch data in excel in PHP (Full Source Code)
Size: 2.01 KB
Version: V 1.0

Anuj Kumar

I'm Anuj kumar a web developer with 4+ year experience .I write blogs in my free time. I love to learn new technologies and share with others. I founded PHPGurukul in September 2015. The main aim of this website to is provide php , jquery , mysql , phpoops and other web development tutorials. I am trying best effort to make PHPGurukul useful for every single moment spend on this website. If you think this website is useful to visit please share with your friend and buddies.

Recommended Tutorials for you

You may also like...