使用phpexcel从mysql数据库创建excel报告

时间:2015-10-06 07:50:25

标签: php mysql excel pdo

我有一个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>";
}

2 个答案:

答案 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文件或报告
  1. 首先下载excel-report.zip并将其解压缩。
  2. 你会在root中找到index.php,还有一个文件夹。
  3. 打开index.php并查看代码。
  4. 代码本身具有自我描述性和良好的评论性。
  5. 您可以添加列标题和记录。
  6. 您还可以在特定单元格或整行中添加颜色。
  7. 如果要为特定单元格或行设置粗体字体,也会在代码中给出。
  8. excel文件中任何列的宽度都是可调的。我已将其设置为自动调整。