title: Retrieve table in database to phpexcel
body: <p>I am using php excel export the data in database to Excel format.
  I can retrieve all the data from database to Excel ady. But if i wanna to retrieve the column name of the table, how i going to do. Anyone can help?</p>
  
  <p>Here's my code:</p>
  
  <pre><code>&lt;?php
  
     header("Content-Type:application/vnd.ms-excel");
      header("Content-Disposition:attachment;filename=product.xls");
      header("Pragma:no-cache");
      header("Expires:0");
  
    header('Content-Type: text/html; charset=UTF-8');
          header("Content-type: application/octetstream");
          header('Content-Disposition: attachment; filename="export.csv"');
   /** Error reporting */
    error_reporting(E_ALL);
   ini_set('display_errors', TRUE);
   ini_set('display_startup_errors', TRUE);
     date_default_timezone_set('Europe/London');
  
    if (PHP_SAPI == 'cli')
  die('This example should only be run from a Web Browser');
  
   /** Include PHPExcel */
  require_once '../Classes/PHPExcel.php';
   require_once '../Classes/PHPExcel/Writer/Excel2007.php';
    require_once '../Classes/PHPExcel/IOFactory.php';  
  
    $con = mysql_connect("localhost","root",""); 
    if (!$con) 
    { 
   die('Could not connect: ' . mysql_error()); 
   } 
  
   mysql_select_db("litako", $con); 
  
  
  
     $objPHPExcel = new PHPExcel();
  
    $res= mysql_query("SELECT * FROM vendorlist ");
  
    /** Error reporting */
    error_reporting(E_ALL);
  
    date_default_timezone_set('Europe/London');
  
  
  
    $objPHPExcel = new PHPExcel();
  
   if(!$res){
  die("Error");
   }
  
  
   $col = 0; 
   $row = 3; 
   while($mrow = mysql_fetch_assoc($res)) { 
  $col = 0; 
   foreach($mrow as $key=&gt;$value) { 
      $objPHPExcel-&gt;getActiveSheet()-&gt;setCellValueByColumnAndRow($col, $row, $value); 
      $col++; 
  } 
  $row++; 
   }
    // Set active sheet index to the first sheet, so Excel opens this as the first sheet
    $objPHPExcel-&gt;setActiveSheetIndex(0);
  
    // Save Excel 2007 file
   $objPHPExcel-&gt;setActiveSheetIndex(0);
  
  
  header('Content-Type: application/vnd.ms-excel');
   header('Content-Disposition: attachment;filename="report.xls"');
    header('Cache-Control: max-age=0');
   // If you're serving to IE 9, then the following may be needed
   header('Cache-Control: max-age=1');
    // If you're serving to IE over SSL, then the following may be needed
   header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
   header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
    header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
    header ('Pragma: public'); // HTTP/1.0
  
  $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
      $objWriter-&gt;save('php://output');
  
  
   exit;
  </code></pre>