개발! 딱 깔끔하고 센스있게!

AJAX 를 통 해 PHP 류 에서 PHPExcel 로 데 이 터 를 전달 합 니 다. 본문

개발 스크랩 메모/PHP

AJAX 를 통 해 PHP 류 에서 PHPExcel 로 데 이 터 를 전달 합 니 다.

렉사이 2020. 12. 3. 02:39

나 는 OOP PHP 와 제 이 슨 데이터 에 갇 혔 다.

나 는 OOP 에 대해 아직 완전히 낯 설 지 는 않 지만 이 점 을 이해 할 수 없다.

누군가가 나 에 게 설명 을 해 줄 수 있다 면, 정말 좋 겠 다!

나 는 PHP 중 다음 의 grid 대상 이 있다:

 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 callegrid. phop 에서 호출 되 었 습 니 다:

$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 질의 (모든 검색 결과) 를 Excel 로 내 보 내 는 PHP Excel 입 니 다.

그래서 저 는 to excel. phop 과 toExcel ($query) 함 수 를 얻 었 습 니 다.

이것 은 하나의 조 사 를 받 고 이 를 엑셀 로 내 보 냅 니 다.

 

현재 - AJAX 를 통 해 모 바 일 에서 톡 스 셀 로 전송 하 는 방법 은?

  1. $data () 에 추가 해 야 한 다 는 것 을 알 고 있 습 니 다:

    $data [sql] = $sql;

다음은?


업데이트:다음 jquery 격자 를 사용 하고 있 습 니 다:http: / squa - bracket. com / openjs 사이트

PHPExcel 을 grid 나 jquery 로 시작 해 야 한 다 는 것 을 압 니 다.

 

대답 하 다.

 

당신 이 무엇 을 할 수 있 는 지 에 대한 대체적인 생각:

만 들 기 버튼, 예 를 들 어.

export to Excel 

그 다음 에 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 토 excel. pp 에는 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. 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 호출, excel 파일 생 성, 파일 을 되 돌려 줍 니 다 / 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); 

그리고 자바 script 에서 이 줄 로 파일 을 표시 합 니 다.

 

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