没有数据写入Excel

时间:2015-10-12 01:33:06

标签: php mysql pdo phpexcel

我有以下代码:

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');

虽然它的语法是正确的,但它不会将任何数据写入excel。请帮助我,因为我只有2天的截止日期。提前谢谢。

0 个答案:

没有答案