【问题标题】:Enable to download CSV file in Codeigniter允许在 Codeigniter 中下载 CSV 文件
【发布时间】:2017-07-20 06:18:14
【问题描述】:

这里我有一些从数据库表中获取的数据。现在我想将所需的数据下载为csv 并要求用户下载该csv 文件。

我的csv 文件已创建,但主要问题是,不会要求用户下载该文件。

这是我的AJAX 代码,在单击导出按钮时执行

$.ajax({
     url:"<?php echo site_url('Customer/ExportCsv'); ?>",
     type: 'post',
     data: { userId: userId },
     success:function(data){
        alert(data);
     }
});

这里是我的控制器功能

public function ExportCsv(){     
    $csvdata=array('customer_name','customer_email', 'customer_mobile','birth_date', 'marriage_date','address','status');//Column headers        
    // fetch the data
    $query = $this->db->get_where('customer', array('user_id' => $_POST['userId'],'is_deleted' => '0'));
    //$this->db->order_by("create_date", "desc");
    $getRes = $query->result_array();

    $csv = "ID,Customer Name,SenderId,UserID,Receiver Number,ReceiverID,Message Content,Send Date,Status,Message Length,Message Type,RequestID,FailureMsg \n";//Column headers

    foreach ($getRes as $record){
        $csv.= $record['customer_name'].','.$record['customer_email'].','.$record['customer_mobile'].','.$record['birth_date'].','.$record['anniversery_date'].','.$record['address'].','.$record['status']."\n"; //Append data to csv
    }

// output headers so that the file is downloaded rather than displayed
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=Yourdata.csv');

    $csv_handler = fopen('php://output', 'w');            
    fwrite ($csv_handler,$csv); //Write headers and data
    fclose ($csv_handler);  //Close CSV file connections
}

请指导我哪里出错了..!谢谢

【问题讨论】:

  • 你遇到了什么错误?按 f12 并检查
  • 它只是在控制台打印所有 csv 数据,但我想要求下载 csv 文件
  • 为什么你使用ajax而不是ajax你可以直接调用你需要的页面
  • 他们与 AJAX 有什么关系吗?
  • 好的,我自己解决了。看我的回答

标签: php codeigniter csv


【解决方案1】:

只是我的 AJAX 中的一个小改动,它就可以工作了 :)

$.ajax({
            url:"<?php echo site_url('Customer/ExportCsv'); ?>",
            type: 'post',
            data: { userId: userId },
            success:function(data){
                $('#ExpCsv').attr('Disabled',false);
                $('#ExpCsvLoad').removeClass('fa fa-refresh fa-spin');
                $('#ExpCsvLoad').addClass('fa fa-cloud-download');
                $('#ExpCsvStatus').text('Export Customer CSV');

                // newly appended code line
                document.location.href = '<?php echo site_url('Customer/ExportCsv'); ?>';
            }
        });

【讨论】:

    【解决方案2】:

    按照以下步骤操作:

    第一步:

    $header_name=array('customer_name','customer_email','customer_mobile','birth_date','marriage_date','address','status');//Column headers
    
    // fetch the data
        $query = $this->db->get_where('customer', array('user_id' => $_POST['userId'],'is_deleted' => '0'));
        $getRes = $query->result_array();
    

    第 2 步:

    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Type: application/vnd.ms-excel'); 
    header("Content-Disposition: attachment; filename=filename.csv");
    

    第 3 步:

    // create a file pointer connected to the output stream
    $output = fopen('php://output', 'w');
    
    // output the column headings
    fputcsv($output,$header_name);
    
    // loop over the rows, outputting them
    $finalData = array();
    
    $fp = fopen('file.csv','w');
    

    第 4 步:

    foreach( $getRes as  $data)
    {
    
        $finalData[]=$data['customer_name'];
        $finalData[]=$data['customer_email'];
        $finalData[]=$data['customer_mobile'];
        $finalData[]=$data['birth_date'];
        $finalData[]=$data['marriage_date'];
        $finalData[]=$data['address'];
        $finalData[]=$data['status'];
    
        fputcsv($output, $finalData);
        unset($finalData);
    } 
    

    第 5 步:

    fclose($fp);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-04
      • 2014-07-09
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多