繁体   English   中英

将数据下载到Excel工作表

[英]downloading data into excel sheet

<a style="margin:0px 10px;" href="<?php echo base_url(); ?>admin/download_members" class="btn btn-info" ><i class="glyphicon glyphicon-cloud-download"></i> Download Excel</a>

当我在锚标签上方单击时,我的excel会完美下载。 我需要使用一些过滤器下载excel。 而且我正在使用AJAX下载但无法下载。

我的阿贾克斯

$(document).ready(function(){
    $("#download_excel").click(function(){
          var state = $("#state").val();
          var city = $("#city").val();
          $.post("<?php echo base_url()?>admin/download_members",
          {
             state:state,city:city
          },
          function(response){

          });
    });
});

我的控制器

function download_members(){
    $state = $this->input->post('state');
    $city = $this->input->post('city');
    $data['members'] =  $this->Select->view_members_to_download($state,$city,$status);
    $this->load->view('admin/spreadsheet_view',$data);
}

我的模特

public function view_members_to_download($state,$city){
    if($state){
        $this->db->where('u.state',$state);
    }
    if($city){
        $this->db->where('u.city',$city);
    }
    $this->db->order_by('u.plan','asc');
    $this->db->from('users u');
    $this->db->join('states s', 'u.state=s.id', 'left');
    $this->db->join('cities c', 'u.city=c.id', 'left');
    $this->db->join('packages p', 'p.package_id=u.plan', 'left');
    $this->db->join('profile_type pt', 'pt.profile_type_id=u.profile_type', 'left');
    $query = $this->db->get();  
    return $query->result(); 
}

您可以在控制器中使用下载助手

function download_members(){
    $this->load->helper('download');
    $state = $this->input->post('state');
    $city = $this->input->post('city');
    $data['members'] = $this->Select->view_members_to_download($state,$city,$status);

    $list = $data['members'];
    $fp = fopen('php://output', 'w');
    foreach ($list as $fields) {
         fputcsv($fp, $fields);
    }

    $data = file_get_contents('php://output'); 
    $name = 'member.csv';

    // Build the headers to push out the file properly.
    header('Pragma: public');     // required
    header('Expires: 0');         // no cache
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Cache-Control: private',false);
    header('Content-Disposition: attachment; filename="'.basename($name).'"');  // Add the file name
    header('Content-Transfer-Encoding: binary');
    header('Connection: close');
    exit();


    force_download($name, $data);
    fclose($fp);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM