2015-10-06 58 views
1

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

您是如何尝试使用phpexcel生成excel文件的?您收到的错误消息是什么或您遇到的意外行为? – Shadow

+0

我没有尝试过,但我知道如何生成Excel文件,但没有使用phpexcel及其唯一的表格格式。 –

+0

为什么不创建一个CSV – 2015-10-06 08:04:26

回答

2

您需要更换

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文件的基础

+0

谢谢我将从这里开始...... –

0

创建和下载Excel中使用phpExcel

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