Export MySQL To Excel Using PHP

Inserting data to your database and getting all the data when required is a much needed operation in websites maintenance or development. So making all these operations easy is really needy.

I am presenting you a PHP script to export mysql to excel sheet and download. Script is orginally written by Oliver Schwarz <[email protected]>

Following are the script to call the class file and produce the output. Just download the php file and class file and follow the following steps,

1 : Create Download Link

<a href=”export.php”>Export To Excel</a>

2 : Include the .class file to “php-excel.php”

// load library
require ‘php-excel.class.php’;

3 : Include db file for database connection

// load your db file for databse connection
require ‘php-db-connect.php’;

4 : Set your column names for excel file

5 : Create loop to fetch data and write to excel file

6 : Create Excel File & Export MySQL to Excel

Its so easy and handy to use. Download Export-To-Excel.zip

dipak Written by:

  • chittaranjan sahoo

    nice one

  • Sandra R.

    Hello Dipak, I am trying to use this class but I get the following error
    Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /usr/www/users/****/php-excel.class.php on line 22

    I have not made any changes to the class, what could be the problem? It is the line that says
    private $header = “n”;
    I would appreciate your help, thanks!

  • Ingila

    How do you set headers of the excel file in it?

  • Rahamat Ullah Khan

    Thanks for code, It works good. But the problem is when i have German character – like ä ü ö ß
    Please help me