I am trying to use PHPexcel plugin to generate downloadable excel file from mysql data. Unfortunately, I am only getting only the column headers and not their values in the downloaded excel file
Where am I going wrong ? Also, I would like unicode characters to show properly. Following is my code of the downloadexcel.php file -
<?php
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/Writer/Excel2007.php';
$objPHPExcel = new PHPExcel();
$data=array();
$sql="SELECT s.t_id,s.t_text,p.user_name,p.description,s.time,p.place from t
AS s INNER JOIN users AS p ON s.user_name=p.user_name order by s.time desc";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
$array=array("T Link" => $row[0],"T"=>$row[1] , "User Name" => $row[2] ,
"User Profile" => $row[3], "Time" => $row[4] , "Place" => $row[5]);
array_push($data,$array);
}
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'T Link');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'T');
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'User Name');
$objPHPExcel->getActiveSheet()->setCellValue('D1', 'User Profile');
$objPHPExcel->getActiveSheet()->setCellValue('E1', 'Time');
$objPHPExcel->getActiveSheet()->setCellValue('F1', 'Place');
$row=2;
foreach($data as $row->$value) {
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row,$value->t_id);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$row,$value->t_text);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$row,$value->user_name);
$objPHPExcel->getActiveSheet()->setCellValue('D'.$row,$value->description);
$objPHPExcel->getActiveSheet()->setCellValue('E'.$row,$value->time);
$objPHPExcel->getActiveSheet()->setCellValue('F'.$row,$value->place);
$row++;
}
header('Content-Type: application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="helloworld.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
?>
Based on comments of Mark Baker, edited the code but still not getting any result. This is my edited code -
<?php
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/Writer/Excel2007.php';
$objPHPExcel = new PHPExcel();
$data=array();
$sql="SELECT s.t_id,s.t_text,p.user_name,p.description,s.time,p.place from t
AS s INNER JOIN users AS p ON s.user_name=p.user_name order by s.time desc";
$result = mysqli_query($con,$sql);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'T Link');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'T');
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'User Name');
$objPHPExcel->getActiveSheet()->setCellValue('D1', 'User Profile');
$objPHPExcel->getActiveSheet()->setCellValue('E1', 'Time');
$objPHPExcel->getActiveSheet()->setCellValue('F1', 'Place');
while ($row = mysqli_fetch_array($result))
{
$n=2;
foreach($data as $row) {
$objPHPExcel->getActiveSheet()->setCellValue('A'.$n,$row['t_id']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$n,$row['t_text']);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$n,$row['user_name']);
$objPHPExcel->getActiveSheet()->setCellValue('D'.$n,$row['description']);
$objPHPExcel->getActiveSheet()->setCellValue('E'.$n,$row['time']);
$objPHPExcel->getActiveSheet()->setCellValue('F'.$n,$row['place']);
$n++;
}
}
header('Content-Type: application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="helloworld.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
?>