通过AJAX的PHP类将数据传递到PHPExcel [英] Passing data from PHP class to PHPExcel via AJAX

查看:115
本文介绍了通过AJAX的PHP类将数据传递到PHPExcel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困面向对象的PHP和JSON数据。我不是完全新的面向对象,但我不能让我的头,围绕这一点。如果有人能请向我解释,将是巨大的!

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!

我在PHP以下网格对象:

i have the following grid object in 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;
        }

和它称为AJAX callgrid.php:

and it's called by 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);

我想要得到的是要能够导出当前的SQL查询(也可以是全部或搜索结果),使用PHPExcel到Excel中。所以,我得toExcel.php与功能toexcel($查询) - 这将需要查询和导出到excel

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.

现在 - 我如何通过SQL查询从电网通过AJAX来toexcel

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

  1. 据我所知,我需要添加到$数据()

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

$数据['SQL'] = $ SQL;

$data['sql'] = $sql;

何去何从?

更新: 我用下面的jQuery网格: http://square-bracket.com/openjs

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

据我所知,PHPExcel要么通过网格或jQuery的启动

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

推荐答案

什么,你可以做一个总体思路:

A general idea of what you could do:

创建一个按钮,例如

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

然后在jQuery中,你必须创建这样的:

Then in jquery you have to create something like:

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

});

文件export_to_excel.php将包含code生成Excel文件:

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

  1. 在这里,您将启动PHPExcel类,并创建一个文件如: new_excel.xls
  2. 在响应数组$回应[网址]将包含绝对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. 创建按钮。
  2. 然后尝试击中按钮时,做一个简单的AJAX调用。
  3. 然后创建export_to_excel.php文件,并尝试与PHPExcel类的工作。
  4. 创建一个样本擅长基于找到的教程文件。
  5. 创建基于你自己的数据的Excel文件,但硬codeD中的PHP文件。
  6. 创建正确的AJAX调用发送所需的数据到PHP文件。
  7. 抓住正确的AJAX调用。
  8. 从AJAX调用PHPExcel类传递数据。
  9. 创建Excel文件。
  10. 发送回的URL Excel文件。
  11. 将用户重定向到Excel文件的URL。
  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.

修改

要帮助你多一点:你只需要一个PHP脚本/文件。同样的人会收到AJAX调用的JavaScript文件,将生成excel文件,并返回/响应文件的URL的JavaScript文件(按顺序)。一个简单的例子是:

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

然后在你的JavaScript此行显示的文件

And then in javascript you display the file with this line

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

这篇关于通过AJAX的PHP类将数据传递到PHPExcel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆