No data is written into Excel

2019-09-01 09:23发布

问题:

I have the following code:

require 'PHPExcel.php';
require 'PHPExcel/Writer/Excel2007.php';
require 'PHPExcel/IOFactory.php';

$user = "root";
$pass = "admin";
$host = "localhost";
$db = "hrtms";

$objPHPExcel = new PHPExcel();

try{
$con = new PDO("mysql:host=$host;dbname=$db",$user,$pass);
$con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$con->exec('SET NAMES "utf8"');
}
catch(PDOException $e){
echo $e->getMessage();
exit();
}

try{
$query = $con->prepare("SELECT * FROM emptb WHERE Department =  'ADMIN/OFFICE' ORDER BY id ASC");
$query->execute();
}
catch(PDOException $e){
echo $e->getMessage();
exit();
}
$rowCount = 1;
$objPHPExcel->setActiveSheetIndex(0);
while($row = $query->fetch())
{
    $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount,$row['EmpID']);
    $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount,$row['Lastname'] . ", " . $row['Firstname']);
    $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount,$row['SectionName']);
    $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount,$row['LineName']);
    $rowCount++;
}
$excelWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$excelWriter->save('sample.xlsx');

Though its syntax is correct but it doesn't write any data to excel. Please help me because I only have 2 days deadline. Thank you in advance.