简体   繁体   中英

Creating excel report from mysql database using phpexcel

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>";
}

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

Create and download excel file or report using phpExcel

  1. First of all download excel-report.zip and extract it.
  2. You will find index.php in root and one more folder.
  3. Open index.php and take a look at the code.
  4. Code itself is self descriptive and well commented.
  5. You can add column heading and records.
  6. You can also add color in particular cell or entire row.
  7. If you want to set bold font for particular cell or row,it is also given in code.
  8. Width of any column in excel file is adjustable.I have set it to auto adjust.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM