通过 AJAX 将数据从 PHP 类传递到 PHPExcel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9209009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:21:43  来源:igfitidea点击:

Passing data from PHP class to PHPExcel via AJAX

phpajaxoopphpexcel

提问by Elen

i got stuck with OOP PHP and json data. i'm not completely new to OOP, but i can't get my head around this. if anyone can please explain to me, would be great!

我被 OOP PHP 和 json 数据困住了。我对 OOP 并不完全陌生,但我无法理解这一点。如果有人可以向我解释一下,那就太好了!

i have the following grid object in PHP:

我在 PHP 中有以下网格对象:

    Class Grid {

        var $data;
        var $joins;
        var $fields;
        var $where;
        var $table;
        var $groupBy;
        var $having;
        var $limit;
        var $order_by;
        var $sort;
        var $security;
        var $set;
        var $sql;

....

        // loads data into the grid
        function load() {
    ...
            // setup the sql - bring it all together
            $sql = "
                SELECT $post[cols]
                FROM `$table`
                $joins
                $where
                $groupBy
                $having
                ORDER BY $order_by $sort
                $limit
            ";

            $this->sql = $sql;

            // execute the sql, get back a multi dimensial array
            $rows = $this->_queryMulti($sql);

            // form an array of the data to send back
            $data = array();
            $data['rows'] = array();
            foreach($rows as $i=>$row) {
                foreach($row as $col=>$cell) {
                    // use primary key if possible, other wise use index
                    $key = $primaryKey ? $row[$primaryKey] : $i;
                    // primary key has an _ infront becuase of google chrome re ordering JSON objects
                    //http://code.google.com/p/v8/issues/detail?id=164
                    $data['rows']["_".$key][$col] = $cell;
                }
            }

    ...        
            $data['order_by'] = $order_by;
            $data['sort'] = $sort;
            $data['page'] = $page;
            $data['start'] = $startRow + 1;
            $data['end'] = $startRow + $nRowsShowing;
            $data['colData'] = $colData;

            $this->data = $data;
        }

and it's called by AJAX callgrid.php:

它由 AJAX callgrid.php 调用:

$grid->load();
        // here we need to add field in data[sql] = sql query, then we can pass it to toExcel() - how?
        echo json_encode($grid->data);

what i'm trying to get is to be able to export current sql query (it can be all or searched results) into Excel using PHPExcel. So i've got toExcel.php with function toexcel($query) - that will take a query and export it to excel.

我想要得到的是能够使用 PHPExcel 将当前的 sql 查询(可以是全部或搜索结果)导出到 Excel 中。所以我有 toExcel.php 和函数 toexcel($query) - 这将进行查询并将其导出到 excel。

now - HOW do i pass sql query from grid to toexcel via AJAX?

现在 - 我如何通过 AJAX 将 sql 查询从网格传递到 toexcel?

  1. I understand that i need to add to $data():

    $data['sql'] = $sql;

  1. 我知道我需要添加到 $data():

    $data['sql'] = $sql;

what next?

接下来是什么?



UPDATE: I'm using the following jquery grid: http://square-bracket.com/openjs

更新:我正在使用以下 jquery 网格:http: //square-bracket.com/openjs

I understand that PHPExcel should be initiated either by grid or jquery

我知道 PHPExcel 应该由网格或 jquery 启动

回答by aletzo

A general idea of what you could do:

您可以做什么的一般想法:

Create a button e.g.

创建一个按钮,例如

<a href="#" id="export">export to Excel</a>

Then in jquery you have to create something like:

然后在 jquery 中你必须创建类似的东西:

var grid = $(".grid.digital_edit").loadGrid({...}); //or similar - what you did to load the data into the grid

$('#export').click(function() {
    $.ajax({
        url: "export_to_excel.php", // the url of the php file that will generate the excel file
        data: grid.getData(), //or similar - based on the grid's API
        success: function(response){
            window.location.href = response.url;
        }
    })

});

The file export_to_excel.php will contain the code that generates the excel file:

文件 export_to_excel.php 将包含生成 excel 文件的代码:

  1. This is where you'll initiate the PHPExcel class and create a file e.g. new_excel.xls
  2. In your response array the $response['url'] will contain the absolute url to the newly created file. (http://www.example.com/files/new_excel.xls)
  1. 您将在此处启动 PHPExcel 类并创建一个文件,例如 new_excel.xls
  2. 在您的响应数组中, $response['url'] 将包含新创建文件的绝对 url。(http://www.example.com/files/new_excel.xls)

It may sound too complex, but try to separate your goals and achieve one at a time. E.g.

这听起来可能太复杂了,但请尝试将您的目标分开并一次实现一个。例如

  1. Create the button.
  2. Then try to make a simple AJAX call when hitting the button.
  3. Then create your export_to_excel.php file and try to work with the PHPExcel class.
  4. Create a sample excel file based on the tutorials found.
  5. Create an excel file based on your own data, but hard-coded in the php file.
  6. Create the correct AJAX call that sends the wanted data to a php file.
  7. Catch the correct AJAX call.
  8. Pass the data from the AJAX call to the PHPExcel class.
  9. Create the excel file.
  10. Send back the url to the excel file.
  11. Redirect the user to the url of the excel file.
  1. 创建按钮。
  2. 然后尝试在按下按钮时进行简单的 AJAX 调用。
  3. 然后创建您的 export_to_excel.php 文件并尝试使用 PHPExcel 类。
  4. 根据找到的教程创建示例 Excel 文件。
  5. 根据自己的数据创建一个excel文件,但是硬编码在php文件中。
  6. 创建将所需数据发送到 php 文件的正确 AJAX 调用。
  7. 捕捉正确的 AJAX 调用。
  8. 将来自 AJAX 调用的数据传递给 PHPExcel 类。
  9. 创建excel文件。
  10. 将 url 发送回 excel 文件。
  11. 将用户重定向到 excel 文件的 url。

EDIT

编辑

To help you a bit more: You need only one PHP script/file. The same one will receive the AJAX call from the javascript file, will generate the excel file and will return/respond the file url to the javascript file(in that order). A simplified example would be:

为您提供更多帮助:您只需要一个 PHP 脚本/文件。同一个将接收来自 javascript 文件的 AJAX 调用,将生成 excel 文件并将文件 url 返回/响应到 javascript 文件(按该顺序)。一个简化的例子是:

<?php
//export_to_excel.php

$data = $_POST['data']; // get the data from the AJAX call - it's the "data: grid.getData()" line from above

//... format the received data properly for the PHPExcel class

// later on in the same file:
$xls = new PHPExcel();
$xls->loadData($formattedData); //I assume that there is a similar loadData() method
$xls->exportToFile('/vaw/www/example.com/public/files/new_excel.xls'); // I assume that there is an exportToFile() method

$response = array(
    'success' => true,
    'url' => 'http://www.example.com/files/new_excel.xls'
);

header('Content-type: application/json');

// and in the end you respond back to javascript the file location
echo json_encode($response);

And then in javascript you display the file with this line

然后在 javascript 中用这一行显示文件

window.location.href = response.url; //response.url is $response['url'] from the PHP script