PHP导出数据表数据生成Excel表格文件
有些时候我们会遇到将数据库的数据导出生成Excel表格,比如名单表等等。。。
接下来 我提供两种使用PHP将数据表信息导出生成Excel表格的方法,其中第一种为自己使用的,在Thinkphp下。第二种为使用比较多的PHPExcel,网上看到的,经测试完全没问题。
1.直接自己进行代码拼接生成表格
<?php
//导出excel
public function excel(){
ob_end_clean();
header("Content-Type:application/vnd.ms-excel;charset=UTF-8");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=".'报名表'.".xls");
header("Content-Transfer-Encoding: binary ");
$model = M("enroll");//实例化数据表
$result = $model->where('writetime desc')->select();
foreach ($result as $k => &$v) {
$data[$k]['id'] = $k+1;
$data[$k]['username'] = $v['username'];
$data[$k]['mobile'] = $v['mobile'];
$data[$k]['drivedate'] = $v['drivedate'];
$data[$k]['drivetime'] = $v['drivetime'];
}
$str.='<table border="1" width="1000">';
$str.='<tr>
<th>编号</th><th>姓名</th><th>电话</th><th>预约日期</th><th>预约时间</th>
</tr>';
$str=iconv('UTF-8','GB2312',$str);
foreach ($data as $key=>&$value) {
$str.='<tr>';
foreach ($value as $k=>$v) {
$str.='<td Class="format" style="align:center">'.iconv('UTF-8','GBK',' '.$v).'</td>';
}
$str.='</tr>';
}
$str.='</table>';
echo ($str);die();
}
2.使用比较流行的 PHPExcel生成表格
/**
* 数组转xls格式的excel文件
* @param array $data 需要生成excel文件的数组
* @param string $filename 生成的excel文件名
* 示例数据:
$data = array(
array(NULL, 2010, 2011, 2012),
array('Q1', 12, 15, 21),
array('Q2', 56, 73, 86),
array('Q3', 52, 61, 69),
array('Q4', 30, 32, 0),
);
*/
function createXls($data,$filename='simple.xls'){
ini_set('max_execution_time', '0');
include_once './PHPExcel/PHPExcel.php';
$filename=str_replace('.xls', '', $filename).'.xls';
$phpexcel = new PHPExcel();
$phpexcel->getProperties()
->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
$phpexcel->getActiveSheet()->fromArray($data);
$phpexcel->getActiveSheet()->setTitle('Sheet1');
$phpexcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=$filename");
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objwriter = PHPExcel_IOFactory::createWriter($phpexcel, 'Excel5');
$objwriter->save('php://output');
exit;
}
// 数据
$data = array(
array('姓名', '性别', '出生时间'),
array('小明', '男', '2012-01-21'),
array('小红', '女', '2011-04-12'),
);
// 文件名
$fileName = '个人信息' + date('YmdHis');
// 生成xls格式的excel文件
createXls($data, $fileName);
PHPExcel点击下载
密码:0ses
还没有评论,来说两句吧...