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

标签 php mysql excel pdo

我有一个显示每个员工出勤记录的 sql 查询,所以我现在想要的是从中生成一个 excel 报告,但我真的不明白 phpexcel 是如何工作的,我还使用了像 crystal 这样的拖放报告工具报道。有人可以帮助在 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>";
}

最佳答案

你需要更换

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

用类似的东西:

$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');

您可能需要稍微尝试一下,将日期/时间转换为 MS Excel 序列化时间戳,并应用格式掩码,但这是使用 PHPExcel 从数据库查询创建 Excel 文件的基础知识

关于php - 使用 phpexcel 从 mysql 数据库创建 excel 报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32964501/

相关文章:

PHP 队列问题 - 冗余进程

java - 使用另一个 java 类更新 ResultSet

mysql - 使用mysql select将数据放入另一个表中?

MySQL EXPLAIN 'type' 由 'range' 变为 'ref' where 语句中的日期更改时?

Excel - 制作一个图表,显示列中每个值的出现次数

excel - 如何在 Excel 中创建状态对话框

php - 使用 SWFupload 或 Uploadify 时,是否需要编辑 php.ini 设置?

php - PHP : Ignoring errors and notices in a specific class

php - 阅读电子邮件的完整标题

vba - 删除和创建具有相同名称的新工作簿