douba9776 2019-06-05 16:36
浏览 100

使用ajax将表数据导出到php

I'm writing PHP code (in drupal) to export data to excel file using ajax i'm not receiving any error but file is not getting download when I click button to export to excel. But file getting download when i directly heat the url(from browser)

This is the ajax code

jQuery('.export-csv', context).once('export').click(function() {
console.log('test');

var role = 'teacher';

 /*get basepath*/
 var basepath = drupalSettings.path.baseUrl;
jQuery.ajax({
 type : 'GET',
url  : basepath + 'report-export/'+role,
    success : function (data) {
console.log('success');
},
error: function () {
console.log('error');
}
})
})

and php function in controller to download excel file

 public function exportToExcel($role)
    {
        $filename = "report.xls";       
        header("Content-Type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=\"$filename\"");
        $records = array(
        '0' => array('Name'=> 'user1', 'Status' =>'complete', 'Priority'=>'Low', 'Salary'=>'001'),
        '1' => array('Name'=> 'user2', 'Status' =>'inprogress', 'Priority'=>'Low', 'Salary'=>'111'),
        '2' => array('Name'=> 'user3', 'Status' =>'hold', 'Priority'=>'Low', 'Salary'=>'333'),
        '3' => array('Name'=> 'user4', 'Status' =>'pending', 'Priority'=>'Low', 'Salary'=>'444'),
        '4' => array('Name'=> 'user5', 'Status' =>'pending', 'Priority'=>'Low', 'Salary'=>'777'),
        '5' => array('Name'=> 'user6', 'Status' =>'pending', 'Priority'=>'Low', 'Salary'=>'777')
        );

$heading = false;
    if(!empty($records))
      foreach($records as $row) {
        if(!$heading) {
          // display field/column names as a first row
          echo implode("\t", array_keys($row)) . "
";
          $heading = true;
        }
        echo implode("\t", array_values($row)) . "
";
      }
        exit;
    }

though i m not getting error but file not getting downloading. and in ajax console also getting success message. and when i direct hit url as site.com/report-export/ file gets download

  • 写回答

1条回答

  • duanleixun2439 2019-06-05 16:54
    关注

    You are missing few header tags to make this work. This works for me, procedural style. Quick, hassle-free.

    form.php

     <a class="btn btn-default" href="download.php">Download Excelsheet</a>
    

    download.php

    <?php
    $file = '../path-to/filename.csv';//csv
    $file = '../path-to/filename.xls';//excel
    
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');//csv
        header("Content-Type: application/xls");//excel
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        readfile($file);
        exit;
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog