我有一個sql查詢,顯示每個員工的出勤記錄,所以我現在想要的是從它生成一個excel報告,但我不太瞭解phpexcel如何工作,我也用拖放報表工具如水晶報表。有人可以幫助它在phpexcel。這是我從MySQL數據庫的完整代碼:使用phpexcel從mysql數據庫創建excel報告
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>";
}
您是如何嘗試使用phpexcel生成excel文件的?您收到的錯誤消息是什麼或您遇到的意外行爲? – Shadow
我沒有嘗試過,但我知道如何生成Excel文件,但沒有使用phpexcel及其唯一的表格格式。 –
爲什麼不創建一個CSV – 2015-10-06 08:04:26