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

前端之家收集整理的这篇文章主要介绍了通过AJAX将数据从PHP类传递到PHPExcel前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我遇到了OOP PHP和json数据.我对OOP并不是全新的,但我无法理解这一点.如果有人可以请我解释,会很棒!

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

  1. Class Grid {
  2.  
  3. var $data;
  4. var $joins;
  5. var $fields;
  6. var $where;
  7. var $table;
  8. var $groupBy;
  9. var $having;
  10. var $limit;
  11. var $order_by;
  12. var $sort;
  13. var $security;
  14. var $set;
  15. var $sql;
  16.  
  17. ....
  18.  
  19. // loads data into the grid
  20. function load() {
  21. ...
  22. // setup the sql - bring it all together
  23. $sql = "
  24. SELECT $post[cols]
  25. FROM `$table`
  26. $joins
  27. $where
  28. $groupBy
  29. $having
  30. ORDER BY $order_by $sort
  31. $limit
  32. ";
  33.  
  34. $this->sql = $sql;
  35.  
  36. // execute the sql,get back a multi dimensial array
  37. $rows = $this->_queryMulti($sql);
  38.  
  39. // form an array of the data to send back
  40. $data = array();
  41. $data['rows'] = array();
  42. foreach($rows as $i=>$row) {
  43. foreach($row as $col=>$cell) {
  44. // use primary key if possible,other wise use index
  45. $key = $primaryKey ? $row[$primaryKey] : $i;
  46. // primary key has an _ infront becuase of google chrome re ordering JSON objects
  47. //http://code.google.com/p/v8/issues/detail?id=164
  48. $data['rows']["_".$key][$col] = $cell;
  49. }
  50. }
  51.  
  52. ...
  53. $data['order_by'] = $order_by;
  54. $data['sort'] = $sort;
  55. $data['page'] = $page;
  56. $data['start'] = $startRow + 1;
  57. $data['end'] = $startRow + $nRowsShowing;
  58. $data['colData'] = $colData;
  59.  
  60. $this->data = $data;
  61. }

它由AJAX callgrid.PHP调用

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

我想要的是能够使用PHPExcel将当前的SQL查询(它可以是全部或搜索结果)导出到Excel中.所以我有toExcel.PHP函数toexcel($query) – 这将获取一个查询并将其导出为ex​​cel.

现在 – 如何通过AJAX将SQL查询从网格传递到toexcel?

>我明白我需要添加到$data():

$data [‘sql’] = $sql;

接下来是什么?

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

我知道PHPExcel应该通过grid或jquery启动

解决方法

您可以做的一般概念:

创建按钮,例如

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

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

  1. var grid = $(".grid.digital_edit").loadGrid({...}); //or similar - what you did to load the data into the grid
  2.  
  3. $('#export').click(function() {
  4. $.ajax({
  5. url: "export_to_excel.PHP",// the url of the PHP file that will generate the excel file
  6. data: grid.getData(),//or similar - based on the grid's API
  7. success: function(response){
  8. window.location.href = response.url;
  9. }
  10. })
  11.  
  12. });

文件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文件(按此顺序).一个简化的例子是:

  1. <?PHP
  2. //export_to_excel.PHP
  3.  
  4. $data = $_POST['data']; // get the data from the AJAX call - it's the "data: grid.getData()" line from above
  5.  
  6. //... format the received data properly for the PHPExcel class
  7.  
  8. // later on in the same file:
  9. $xls = new PHPExcel();
  10. $xls->loadData($formattedData); //I assume that there is a similar loadData() method
  11. $xls->exportToFile('/vaw/www/example.com/public/files/new_excel.xls'); // I assume that there is an exportToFile() method
  12.  
  13. $response = array(
  14. 'success' => true,'url' => 'http://www.example.com/files/new_excel.xls'
  15. );
  16.  
  17. header('Content-type: application/json');
  18.  
  19. // and in the end you respond back to javascript the file location
  20. echo json_encode($response);

然后在javascript中显示该行的文件

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

猜你在找的Ajax相关文章