【问题标题】:downloading data into excel sheet将数据下载到 Excel 工作表中
【发布时间】:2023-03-02 22:06:01
【问题描述】:
<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 下载但无法下载。

我的 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(); 
}

【问题讨论】:

  • 用“#”替换href

标签: php html ajax codeigniter activerecord


【解决方案1】:

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

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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多