report.php
<?php
class report
{
private $fileName = 'user';
public function __construct($fileName = '')
{
$this->setFileName($fileName);
}
public function setFileName($fileName)
{
$this->fileName = $fileName;
}
public function toDownload($strTable)
{
header("Content-type: application/vnd.ms-excel");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment ; filename=".$this->fileName."_".date('Y-m-d').".xls");
header('Expires:0');
header('Pragma:public');
echo '<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'.$strTable.'</html>';
}
}
demo.php
<?php
header('content-type:text/html;charset=utf-8');
require "report.php";
$ticketFile = "文件名";
$excelStr = '<table border="1px"><tr><th>卡号</th><th>网址</th><th>分类</th></tr>';
try{
$pdo = new PDO("mysql:host=127.0.0.1;dbname=collect","root","root");
}catch(PDOException $e ){
echo $e->getMessage();
}
$pdo->query("set names utf8");
$sql = "SELECT * from day0310 limit 10";
$list = $pdo->query($sql);
$list->setFetchMode(PDO::FETCH_ASSOC);
$re = $list->fetchAll();
foreach ($re as $key => $value) {
$excelStr.='<tr align="center">';
$excelStr.='<td>'.$value['id'].'</td>';
$excelStr.='<td>'.$value['link'].'</td>';
$excelStr.='<td>'.$value['title'].'</td>';
$excelStr.='</tr>';
}
$reportObj = new report($ticketFile);
$reportObj->toDownload($excelStr);