前端js代码:

newsdc = function (){
                 var myloding;
                 var areaid=$('#areaid').val();
                 var startTime=$('#startTime').val();
                 if(startTime=='') {
                     layer.msg('请选择开始日期');
                     return false;
                 }
                 var endTime=$('#endTime').val();
                 if(endTime=='') {
                     layer.msg('请选择结束日期');
                     return false;
                 }
                 $.ajax({
                     method:"post",
                     data:{"areaid":areaid,"startTime":startTime,"endTime":endTime},
                     url:"newsdc.php",
                     dataType:"JSON",
                     success:function(redata){
                         layer.close(myloding);
                         //alert(redata.filename);
                         // 创建a标签,设置属性,并触发点击下载
                        var $a = $("<a>");
                         $a.attr("href", redata.file);
                         $a.attr("download", redata.filename);
                         $("body").append($a);
                         $a[0].click();
                         $a.remove();
                     },
                     beforeSend: function(){
                         myloding = layer.load(2); //风格1的加载
                     },
                     complete: function(){
                         layer.msg('统计完成,请点击下载!', {time: 2000,icon: 1}, function(){
                             layer.close(myloding);
                         });
                     }

                 })

              }

插入代码结束
php代码



$write = new PHPExcel_Writer_Excel5($excel);
$filename = "id_title_userid_".time().".xls";
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/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");;
header('Content-Disposition:attachment;filename="'.$filename.'"');
header("Content-Transfer-Encoding:binary");
ob_start();
$write->save('php://output');
$xlsData = ob_get_contents();
ob_end_clean();//解决乱码
echo json_encode(['filename' => $filename, 'file' => "data:application/vnd.ms-excel;base64," . base64_encode($xlsData)]);

php主要代码


发表评论 已发布 0

还可以输入 800 个字
 
 
打印