MySQL export woes

Topics: Developer Forum, User Forum
Jun 16, 2012 at 1:23 PM

I'm new to PHPMyExcel. Can someone direct me to some tutorial or an example of exporting the result of a MySQL query to Excel. I tried to scour the documentation along with the PHPExcel libraries but I couldn't find a listing for the same.

Jun 16, 2012 at 2:12 PM
Edited Jun 16, 2012 at 3:24 PM

Using MySQL, you should probably switch to using MySQLi or PDO:


// connection with the database 
$dbhost = "localhost"; 
$dbuser = "root"; 
$dbpass = ""; 
$dbname = "database"; 


// require the PHPExcel file 
require 'Classes/PHPExcel.php'; 

// simple query 

$query = "SELECT username,emailAdress,locationCity FROM users ORDER by id DESC"; 
$headings = array('User Name', 'EMail Address','City'); 

if ($result = mysql_query($query) or die(mysql_error())) { 
    // Create a new PHPExcel object 
    $objPHPExcel = new PHPExcel(); 
    $objPHPExcel->getActiveSheet()->setTitle('List of Users'); 

    $rowNumber = 1; 
    $col = 'A'; 
    foreach($headings as $heading) { 

    // Loop through the result set 
    $rowNumber = 2; 
    while ($row = mysql_fetch_row($result)) { 
       $col = 'A'; 
       foreach($row as $cell) { 

    // Freeze pane so that the heading line won't scroll 

    // Save as an Excel BIFF (xls) file 
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 

   header('Content-Type: application/'); 
   header('Content-Disposition: attachment;filename="userList.xls"'); 
   header('Cache-Control: max-age=0'); 

echo 'a problem has occurred... no data retrieved from the database'; 
Jun 17, 2012 at 4:49 AM

Gracias amigo ! This works just fine. Normally I use PDO but I've to do some additions on an old project where I used MySQL queries. And hence the query.

Sep 22, 2014 at 7:23 PM
can you upload the PHPExcel file too?

-thank you-
Sep 23, 2014 at 4:16 PM
what is the content of PHPexcel file?

Sep 23, 2014 at 4:47 PM
What PHPExcel file are you talking about?
Sep 23, 2014 at 6:33 PM
I mean this :

// require the PHPExcel file
require 'Classes/PHPExcel.php';
Sep 23, 2014 at 6:57 PM
That is the main library file that you need to download to use the PHPExcel library.... if you read this site, it has a download tab that gives you access to the PHPExcel download, which provides you with the files you need for the library..... downloads are quite common for libraries, and normally they provide instructions explaining how to install and use them. Reading the instructions provided on this site should help you install it on your site, and show you how to use it.
Feb 10, 2015 at 4:51 PM
Excelente, me funciono, gracias por el aporte