一、GitHub上下载PHPExcel库,并将Classes文件夹拷贝到tp5的extend文件夹下,
二、代码如下
1、生成后excel文件,返回下载地址
<?php
function expExcel($arr,$name){
require_once str_replace( '\\' , '/' , realpath(dirname(__FILE__).'/../../../')).
'/extend/lib/PHPExcel/PHPExcel.php';
//实例化
$objPHPExcel = new \PHPExcel();
/*右键属性所显示的信息*/
$objPHPExcel->getProperties()->setCreator("zxf") //作者
->setLastModifiedBy("zxf") //最后一次保存者
->setTitle('数据EXCEL导出') //标题
->setSubject('数据EXCEL导出') //主题
->setDescription('导出数据') //描述
->setKeywords("excel") //标记
->setCategory("result file"); //类别
//设置当前的表格
$objPHPExcel->setActiveSheetIndex(0);
// 设置表格第一行显示内容
$objPHPExcel->getActiveSheet()
->setCellValue('A1', '序号')
->setCellValue('B1', '申请号')
->setCellValue('C1', '专利名称')
->setCellValue('D1', '申请人')
->setCellValue('E1', '申请日')
->setCellValue('F1', '法律状态')
->setCellValue('G1', '缴费状态')
->setCellValue('H1', '发明人')
->setCellValue('I1', '专利类型')
->setCellValue('J1', '代理机构')
->setCellValue('K1', '授权日')
->setCellValue('L1', '备注')
//设置第一行为红色字体
->getStyle('A1:L1')->getFont()->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_RED);
$list=[];
foreach($list as $key =>$v){
$key = 1;
/*以下就是对处理Excel里的数据,横着取数据*/
$objPHPExcel->getActiveSheet()
//Excel的第A列,name是你查出数组的键值字段,下面以此类推
->setCellValue('A'.$key, $key-1)
->setCellValue('B'.$key, $v[''])
->setCellValue('C'.$key, $v[''])
->setCellValue('D'.$key, $v[''])
->setCellValue('E'.$key, $v[''])
->setCellValue('F'.$key, $v[''])
->setCellValue('G'.$key, $fee)
->setCellValue('H'.$key, $v[''])
->setCellValue('I'.$key, $v[''])
->setCellValue('J'.$key, $v[''])
->setCellValue('K'.$key, $v[''])
->setCellValue('L'.$key, '');
}
//设置当前的表格
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(40);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(20);
$objPHPExcel->setActiveSheetIndex(0);
ob_end_clean();//清除缓冲区,避免乱码
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="'.$name.'"');
header("Content-Transfer-Encoding: binary");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('down/'.$name.'.xls');
$down_host = $_SERVER['HTTP_HOST'].'/'; //当前域名
//生成的excel路径
$excelurl=$down_host.'patent/down/'.$name.'.xls';
// header('location:'.$down_host.'patent/down/'.$name.'.xls');
return $excelurl;
}
2、ajax请求方法
function daochu(){
var checkID=[];
$$("input[name='checkbox']:checked").each(function(i){
checkID[i] = $$(this).val();});
$$.ajax({
type: "POST",
url: "{:url('shangbiao/export')}",
data: {
shangbiao_id: checkID,
},
success: function (data) {
$$('#down1').attr('href', 'http://'+data.url);
document.getElementById("down1").click(function(){});
}
});
}
3.ajax返回成功后特换a标签里面的href属性值,并触发点击事件,即可导出excel表格并下载
<a href = "" style="display:none;" id="down1"></a>