php文件下载

!!!!不要用ajax请求下载,ajax没有数据流形式的数据,需要使用的话要模拟表单请求

前端使用window.location.href直接请求页面即可

public function process(Vtiger_Request $request)
{
    $mul_class_id = $request->get('productinstanceid');

    $sql = " XXXXXXX ";

    $arrayName[] = array(
        '0' => '学号',
        '1' => '学员姓名',
        '2' => '性别',
        '3' => '学校',
        '4' => '年级',
        '5' => '跟进人',
        '6' => '跟进人团队',
        '7' => '跟进人校区',
        '8' => '状态',
        '9' => '授课信息',
        '10' => '开班成绩'
    );

    //获取需要导出的数据
    $info = ProductInstance_Record_Model::getDownLoadMsg($sql);

    foreach($info as $k => $v){
        $arrayName[] = array(
            '0' => $v['smpid'],
            '1' => $v['accountname'],
            '2' => $v['account_type'],
            '3' => $v['campus'],
            '4' => $v['grade'],
            '5' => $v['last_name'],
            '6' => $v['team'],
            '7' => $v['campusid'],
            '8' => $v['statusname'],
            '9' => $v['teach'],
            '10' => $v['score']
        );
    }

    //p($arrayName)调用方法导出
    $file_name = date('Y-m-d',time()).'-'.$mul_class_id;
    $this->create_xls($arrayName,$file_name);

}

//导出excel的方法
function create_xls($data,$filename='student_info.xls'){
    ini_set('max_execution_time', '0');
    require_once("libraries/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);
    ob_end_clean();
    header('Content-Type: application/vnd.ms-excel');
    /*header("content-type:text/html;charset=utf-8");
    header("content-type:application/octem-stream");*/
    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
    ob_clean();
    $objwriter = PHPExcel_IOFactory::createWriter($phpexcel, 'Excel5');
    $objwriter->save('php://output');
    exit;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值