1、vue代码
/导出excel呼叫日志
export function DownloadExcel(data){
returnrequest({
url: '/admin/system/calllog',
method: 'post',
responseType:"blob",
data
})
}
//点击事件
DownloadExcel(this.formRing).then((res)=>{
//let fileName = res.headers['content-disposition'].split('=')[1]
let fileName = "测试.xlsx"
//console.log(fileName)
// 获取文件名
let objectUrl = URL.createObjectURL(new Blob([res]))
// 文件地址
const link = document.createElement('a')
link.download = fileName
link.href = objectUrl
link.click()
}).catch((err)=>{
console.log(err);
})
2、后端使用的是PHP
public function GetCallLogByExcel($dataArr){
$objPHPExcel = new \PHPExcel();//方法二
$cellName = array('A','B','C','D','E','F');
$xlsCell = array('主叫','被叫','开始时间',"结束时间","是否应答","时长(秒)");
$cellNum = count($xlsCell);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(23);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(23);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(23);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(23);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(23);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(23);
//设置A1
for($i=0;$i<$cellNum;$i++){
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'1', $xlsCell[$i]);
}
$row = 2;
foreach ($dataArr as $key => $value) {
$objPHPExcel->getActiveSheet(0)->setCellValue('A'.$row,$value['usr']); //主叫
$objPHPExcel->getActiveSheet(0)->setCellValue('B'.$row,$value['shownumber']); //被叫
$objPHPExcel->getActiveSheet(0)->setCellValue('C'.$row,date("Y-m-d H:i:s",$value['curtime'])); //开始时间
$objPHPExcel->getActiveSheet(0)->setCellValue('D'.$row,date("Y-m-d H:i:s",$value['curtime']+$value['callingtime'])); //四级目录
$objPHPExcel->getActiveSheet(0)->setCellValue('E'.$row,$value['callingtime']>0?"未应答":"已应答"); //人员名称
$objPHPExcel->getActiveSheet(0)->setCellValue('F'.$row,$value['callingtime']); //人员名称
$row++;
}
ob_end_clean();//这一步非常关键,用来清除缓冲区防止导出的excel乱码
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name=public.xlsx');
header("Content-Disposition:attachment;filename=CDR历史记录.xlsx");//"xls"参考下一条备注
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');//"Excel2007"生成2007版本的xlsx,"Excel5"生成2003版本的xls
$objWriter->save('php://output');
exit;
}