我在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;
- }
我想要的是能够使用PHPExcel将当前的SQL查询(它可以是全部或搜索结果)导出到Excel中.所以我有toExcel.PHP函数toexcel($query) – 这将获取一个查询并将其导出为excel.
现在 – 如何通过AJAX将SQL查询从网格传递到toexcel?
>我明白我需要添加到$data():
接下来是什么?
更新:
我正在使用以下jquery网格:
http://square-bracket.com/openjs
我知道PHPExcel应该通过grid或jquery启动
解决方法
创建按钮,例如
- <a href="#" id="export">export to Excel</a>
然后在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;
- }
- })
- });
文件export_to_excel.PHP将包含生成excel文件的代码:
>这是您启动PHPExcel类并创建文件的地方,例如new_excel.xls
>在响应数组中,$response [‘url’]将包含新创建文件的绝对URL. (http://www.example.com/files/new_excel.xls)
这听起来可能过于复杂,但要尝试将目标分开并一次实现一个目标.例如.
>创建按钮.
>然后在按下按钮时尝试进行简单的AJAX调用.
>然后创建您的export_to_excel.PHP文件并尝试使用PHPExcel类.
>根据找到的教程创建示例excel文件.
>根据您自己的数据创建一个excel文件,但在PHP文件中进行硬编码.
>创建正确的AJAX调用,将所需数据发送到PHP文件.
>抓住正确的AJAX呼叫.
>将数据从AJAX调用传递给PHPExcel类.
>创建Excel文件.
>将网址发送回excel文件.
>将用户重定向到excel文件的URL.
编辑
为了帮助您:您只需要一个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);
- window.location.href = response.url; //response.url is $response['url'] from the PHP script