通过AJAX将数据从PHP类传递到PHPExcel
我遇到了OOP
PHP和json数据.我对OOP并不是全新的,但我无法理解这一点.如果有人可以请我解释,会很棒!
我在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调用: $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); 我想要的是能够使用PHPExcel将当前的SQL查询(它可以是全部或搜索结果)导出到Excel中.所以我有toExcel.php函数toexcel($query) – 这将获取一个查询并将其导出为ex??cel. 现在 – 如何通过AJAX将sql查询从网格传递到toexcel? >我明白我需要添加到$data(): $data [‘sql’] = $sql; 接下来是什么? 更新: 我知道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 这听起来可能过于复杂,但要尝试将目标分开并一次实现一个目标.例如. >创建按钮. 编辑 为了帮助您:您只需要一个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); 然后在javascript中显示该行的文件 window.location.href = response.url; //response.url is $response['url'] from the PHP script (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |