Return to Snippet

Revision: 50283
at August 18, 2011 01:38 by stur


Initial Code
Controller:

    public function exportAction() {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        
        $this->tbl = new Contacts();
        $xlsTbl = $this->tbl->exportContacts();
        
        header("Content-Type: application/vnd.ms-excel");
	header("Expires: 0");
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header("content-disposition: attachment;filename=contacts-download-" . time() . ".xls");
        
        echo "<table>$xlsTbl</table>";
    }

Model:

	public function exportContacts() {

		$result = $this->_db->fetchAll('SELECT * FROM contacts');
			$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>";
		foreach($result as $key=>$val){
			$xlsTbl .= "<tr>";
			$xlsTbl .= "<td>" . $val->first_name . "</td>";
			$xlsTbl .= "<td>" . $val->last_name . "</td>";
			$xlsTbl .= "<td>" . $val->email . "</td>";
			$xlsTbl .= "<td>" . $val->region . "</td>";
			$xlsTbl .= "<td>" . $val->enquiry_type . "</td>";
			$xlsTbl .= "<td>" . $val->telephone . "</td>";
			$xlsTbl .= "<td>" . $val->message . "</td>";
			$xlsTbl .= "<td>" . $val->type . "</td>";
			$xlsTbl .= "</tr>";
		}
			
		return $xlsTbl;
	}

Initial URL


Initial Description


Initial Title
PHP (Zend) to XLS Excel Spreadsheet

Initial Tags
php, excel

Initial Language
PHP