php - Add custom title to excel columns using PHPEXCEL

194

i am writing values from a while loop into an excel sheet using PHPEXCEL. in doing this i need to write a title for each of the rows . what i only get now is the result from the database , how can i add a custom title

e.g
                Premier League
EVENTID STARTDATE     STATUS
1022            17-09-2013        Won

                Premier League
EVENTID     STARTDATE     STATUS
3421           22-10-2012        lost

Here is my code

    <?php
    mysql_connect("localhost", "root", "");
    mysql_select_db("test2");
    $sql=mysql_query("select * from event");

    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
   date_default_timezone_set('Europe/London');


    require_once '../Classes/PHPExcel.php';


    $objPHPExcel = new PHPExcel();


    $objPHPExcel->setActiveSheetIndex(0);
    $rowCount = 1;
    while($row = mysql_fetch_array($sql)){

     $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['EventID']);

     $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['StartDate']);

 $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $row['Status']);
     $rowCount++;
      }

      // Redirect output to a client’s web browser (Excel5)
      header('Content-Type: application/vnd.ms-excel');
      header('Content-Disposition: attachment;filename="01simple.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->save('php://output');
        exit;
128

Answer

Solution:

something like this:

$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);

$rowCount = 1;
$customTitle = array ('test a','test b','test c');
$objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $customTitle[0]);
$objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $customTitle[1]);
$objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $customTitle[2]);
$rowCount++;

while($row = mysql_fetch_array($sql)){
    $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['EventID']);
    $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['StartDate']);
    $objPHPExcel->getActiveSheet()->SetCellValue('C'.$rowCount, $row['Status']);
    $rowCount++;
}

People are also looking for solutions to the problem: php - Select condition within a hash column using Doctrine mongoDB ODM query builder

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.