本代码是我自己写的达人库的代码,仅供各位参考
PHP代码部分
1,在控制器里面写下如下方法
public function printshowers(){
// 从数据库里查询出你所要打印到excel的数据信息
$model = D('User');
$sql = 'select a.*,b.city FROM sh_user as a left join sh_city_info as b on a.address=b.id order by a.id desc';
$data = $model->query($sql);
$url = showersexcel($data);
echo json_encode($url);
exit();
}
// 下载excel文件
public function tmp(){
$url = $_SERVER['REQUEST_URI'];
$fileinfo = pathinfo($url);
$download = './tmp/' . $fileinfo['basename']; // 文件要下载的地址
header('Content-type: application/x-'.$fileinfo['extension']);
header('Content-Disposition: attachment; filename='.$fileinfo['basename']);
header('Content-Length: '.filesize($download));
readfile($download);
exit();
}
2,在Common/common/function.php里面写下以下代码
// 输出excel表格
function showersexcel($info){
// header("Content-type: text/html; charset=utf-8");
require_once './Public/PHPExcel/PHPExcel.php';
$objPHPExcel=new PHPExcel();//实例化PHPExcel类, 等同于在桌面上新建一个excel
$objSheet=$ob