PHP (Zend) to XLS Excel Spreadsheet


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. Controller:
  2.  
  3. public function exportAction() {
  4. $this->_helper->layout()->disableLayout();
  5. $this->_helper->viewRenderer->setNoRender(true);
  6.  
  7. $this->tbl = new Contacts();
  8. $xlsTbl = $this->tbl->exportContacts();
  9.  
  10. header("Content-Type: application/vnd.ms-excel");
  11. header("Expires: 0");
  12. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  13. header("content-disposition: attachment;filename=contacts-download-" . time() . ".xls");
  14.  
  15. echo "<table>$xlsTbl</table>";
  16. }
  17.  
  18. Model:
  19.  
  20. public function exportContacts() {
  21.  
  22. $result = $this->_db->fetchAll('SELECT * FROM contacts');
  23. $xlsTbl = "<tr><th>First Name</th><th>Last Name</th><th>Email</th><th>Region</th><th>Enquiry Type</th><th>Telephone</th><th>Message</th><th>Type</th></tr>";
  24. foreach($result as $key=>$val){
  25. $xlsTbl .= "<tr>";
  26. $xlsTbl .= "<td>" . $val->first_name . "</td>";
  27. $xlsTbl .= "<td>" . $val->last_name . "</td>";
  28. $xlsTbl .= "<td>" . $val->email . "</td>";
  29. $xlsTbl .= "<td>" . $val->region . "</td>";
  30. $xlsTbl .= "<td>" . $val->enquiry_type . "</td>";
  31. $xlsTbl .= "<td>" . $val->telephone . "</td>";
  32. $xlsTbl .= "<td>" . $val->message . "</td>";
  33. $xlsTbl .= "<td>" . $val->type . "</td>";
  34. $xlsTbl .= "</tr>";
  35. }
  36.  
  37. return $xlsTbl;
  38. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.