Reputation: 91
I have a sql query that displays records of attendance per employee so what I want now is generate an excel report from it but I don't really understand how does phpexcel works and I also used in drag and drop reporting tools like crystal reports. Could someone help to make it in phpexcel. Here's my complete code from mysql database:
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>";
}
Upvotes: 1
Views: 12546
Reputation: 631
Create and download excel file or report using phpExcel
Upvotes: 1
Reputation: 212412
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
Upvotes: 3