我有一个sql查询,显示每位员工的出勤记录,所以我现在想要的是从中生成excel报告,但我不太了解phpexcel是如何工作的,我也用于拖放报告工具喜欢水晶报道。有人可以帮助在phpexcel中制作它。这是我在mysql数据库中的完整代码:
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();
}
//print first employee table
try{
$query = $con->prepare("SELECT * FROM emptb WHERE Department = :dept AND SectionName = :section AND LineName = :line ORDER BY id ASC");
$query->bindParam(':dept',$dept);
$query->bindParam(':section',$section);
$query->bindParam(':line',$line);
$query->execute();
}
catch(PDOException $e){
echo $e->getMessage();
exit();
}
while($row = $query->fetch())
{
echo <<<_END
ID: <input type='text' value='$row[EmpID]' readonly='readonly'/> Employee: <input type='text' value='$row[Lastname], $row[Firstname]' readonly='readonly'/> <p/>
Section: <input type='text' value='$row[SectionName]' readonly='readonly'/> Line: <input type='text' value='$row[LineName]' readonly='readonly'/><p/>
_END;
try{
$qry = $con->prepare("SELECT * FROM attendance WHERE EmpID = :id AND ValidDate BETWEEN DATE('2015-08-01') AND DATE('2015-08-30') GROUP BY ValidDate ORDER BY ValidDate ASC");
$qry->bindParam(':id',$row['EmpID']);
$qry->execute();
}
catch(PDOException $e){
echo $e->getMessage();
exit();
}
echo "<table>";
while($subrow = $qry->fetch())
{
echo <<<_END
<tr>
<td>$subrow[ValidDate]</td>
<td>$subrow[TimeIn]</td>
<td>$subrow[LunchOut]</td>
<td>$subrow[LunchIn]</td>
<td>$subrow[TimeOut]</td>
</tr>
_END;
}
echo "</table>";
}
答案 0 :(得分:3)
You need to replace
echo "<table>";
while($subrow = $qry->fetch())
{
echo <<<_END
<tr>
<td>$subrow[ValidDate]</td>
<td>$subrow[TimeIn]</td>
<td>$subrow[LunchOut]</td>
<td>$subrow[LunchIn]</td>
<td>$subrow[TimeOut]</td>
</tr>
_END;
}
echo "</table>";
with something like:
$objPHPExcel = new PHPExcel();
$row = 1;
while($subrow = $qry->fetch())
{
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $subrow['ValidDate']);
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $subrow['TimeIn']);
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $subrow['LunchOut']);
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $subrow['LunchIn']);
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $subrow['TimeOut']);
$row++;
}
$excelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('myFileName.xlsx');
You'll probably need to play with it a bit to convert dates/times to MS Excel serialized timestamps, and apply format masks but that's the basics of creating an Excel file from a database query using PHPExcel
答案 1 :(得分:1)
使用phpExcel
创建和下载Excel文件或报告