可以通过创建的php中的php下载xlsx文件

发布于 2025-01-23 22:41:49 字数 1868 浏览 2 评论 0原文

在public/js文件中,我请求$ ajax,然后仅响应字符串而不是文件。

如果客户端使用AJAX发送请求,则服务器使用字符串而不是XLSX文件响应。

这是请求部分:

$("#downloadXLS").on('click', function () {
        $.ajax({
            type: 'get',
            url: appRoot + "transactions/downloadXLS/",

            success: function () {
            },

            error: function () {
            }
        });
    });

这里是php响应函数:

public function downloadXLS()
  {
    // Filter the excel data 
    function filterData(&$str)
    {
      $str = preg_replace("/\t/", "\\t", $str);
      $str = preg_replace("/\r?\n/", "\\n", $str);
      if (strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
    }

    // Excel file name for download 
    $fileName = "transaction-data_" . date('Y-m-d') . ".xls";

    // Column names 
    $fields = array('No.',  'Pay Date',  'Company');

    // Display column names as first row 
    $excelData = implode("\t", array_values($fields)) . "\n";

    // Headers for download 
    header("Content-Type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=\"$fileName\"");

    $allTranscations = $this->transaction->getAll();
    if (is_array($allTranscations)) {
      // Output each row of the data 
      foreach ($allTranscations as $key => &$trans) {
        $lineData = array($key + 1, $trans->pay_time, $trans->company);
        array_walk($lineData, 'filterData');
        $excelData .= implode("\t", array_values($lineData)) . "\n";
      }
    } else {
      $excelData .= 'No records found...' . "\n";
    }

    // Render excel data 
    echo $excelData;

    exit();
  }

”在此处输入图像说明”

In public/js file, I request $ajax, and get responding just string, not file.

If the client sends a request using ajax, the server responds with a string instead of xlsx file.

Here is request part:

$("#downloadXLS").on('click', function () {
        $.ajax({
            type: 'get',
            url: appRoot + "transactions/downloadXLS/",

            success: function () {
            },

            error: function () {
            }
        });
    });

And here is php responding function:

public function downloadXLS()
  {
    // Filter the excel data 
    function filterData(&$str)
    {
      $str = preg_replace("/\t/", "\\t", $str);
      $str = preg_replace("/\r?\n/", "\\n", $str);
      if (strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
    }

    // Excel file name for download 
    $fileName = "transaction-data_" . date('Y-m-d') . ".xls";

    // Column names 
    $fields = array('No.',  'Pay Date',  'Company');

    // Display column names as first row 
    $excelData = implode("\t", array_values($fields)) . "\n";

    // Headers for download 
    header("Content-Type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=\"$fileName\"");

    $allTranscations = $this->transaction->getAll();
    if (is_array($allTranscations)) {
      // Output each row of the data 
      foreach ($allTranscations as $key => &$trans) {
        $lineData = array($key + 1, $trans->pay_time, $trans->company);
        array_walk($lineData, 'filterData');
        $excelData .= implode("\t", array_values($lineData)) . "\n";
      }
    } else {
      $excelData .= 'No records found...' . "\n";
    }

    // Render excel data 
    echo $excelData;

    exit();
  }

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

能怎样 2025-01-30 22:41:50

您无法通过AJAX请求下载文件。
尝试位置。

You can't download file via ajax request.
Try location.href=transactions/downloadXLS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文