cakePHP - PHP EXCEL saves as .html -
i have implemented phpexcel within cakephp application, helper:
<?php app::uses('apphelper', 'helper'); /** * helper working phpexcel class. * phpexcel has in vendors directory. */ class phpexcelhelper extends apphelper { /** * instance of phpexcel class * @var object */ public $xls; /** * pointer actual row * @var int */ protected $row = 1; /** * internal table params * @var array */ protected $tableparams; /** * constructor */ public function __construct(view $view, $settings = array()) { parent::__construct($view, $settings); } /** * create new worksheet */ public function createworksheet() { $this->loadessentials(); $this->xls = new phpexcel(); } /** * create new worksheet existing file */ public function loadworksheet($path) { $this->loadessentials(); $this->xls = phpexcel_iofactory::load($path); } /** * set row pointer */ public function setrow($to) { $this->row = (int)$to; } /** * set default font */ public function setdefaultfont($name, $size) { $this->xls->getdefaultstyle()->getfont()->setname($name); $this->xls->getdefaultstyle()->getfont()->setsize($size); } /** * start table * inserts table header , sets table params * possible keys data: * label - table heading * width - "auto" or units * filter - true set excel filter column * wrap - true wrap text in column * possible keys params: * offset - column offset (numeric or text) * font - font name * size - font size * bold - true bold text * italic - true italic text * */ public function addtableheader($data, $params = array()) { // offset if (array_key_exists('offset', $params)) $offset = is_numeric($params['offset']) ? (int)$params['offset'] : phpexcel_cell::columnindexfromstring($params['offset']); // font name if (array_key_exists('font', $params)) $this->xls->getactivesheet()->getstyle($this->row)->getfont()->setname($params['font_name']); // font size if (array_key_exists('size', $params)) $this->xls->getactivesheet()->getstyle($this->row)->getfont()->setsize($params['font_size']); // bold if (array_key_exists('bold', $params)) $this->xls->getactivesheet()->getstyle($this->row)->getfont()->setbold($params['bold']); // italic if (array_key_exists('italic', $params)) $this->xls->getactivesheet()->getstyle($this->row)->getfont()->setitalic($params['italic']); // set internal params need processed after data inserted $this->tableparams = array( 'header_row' => $this->row, 'offset' => $offset, 'row_count' => 0, 'auto_width' => array(), 'filter' => array(), 'wrap' => array() ); foreach ($data $d) { // set label $this->xls->getactivesheet()->setcellvaluebycolumnandrow($offset, $this->row, $d['label']); // set width if (array_key_exists('width', $d)) { if ($d['width'] == 'auto') $this->tableparams['auto_width'][] = $offset; else $this->xls->getactivesheet()->getcolumndimensionbycolumn($offset)->setwidth((float)$d['width']); } // filter if (array_key_exists('filter', $d) && $d['filter']) $this->tableparams['filter'][] = $offset; // wrap if (array_key_exists('wrap', $d) && $d['wrap']) $this->tableparams['wrap'][] = $offset; $offset++; } $this->row++; } /** * write array of data actual row */ public function addtablerow($data) { $offset = $this->tableparams['offset']; foreach ($data $d) { $this->xls->getactivesheet()->setcellvaluebycolumnandrow($offset++, $this->row, $d); } $this->row++; $this->tableparams['row_count']++; } /** * end table * sets params , styles required data inserted */ public function addtablefooter() { // auto width foreach ($this->tableparams['auto_width'] $col) $this->xls->getactivesheet()->getcolumndimensionbycolumn($col)->setautosize(true); // filter (has set whole range) if (count($this->tableparams['filter'])) $this->xls->getactivesheet()->setautofilter(phpexcel_cell::stringfromcolumnindex($this->tableparams['filter'][0]).($this->tableparams['header_row']).':'.phpexcel_cell::stringfromcolumnindex($this->tableparams['filter'][count($this->tableparams['filter']) - 1]).($this->tableparams['header_row'] + $this->tableparams['row_count'])); // wrap foreach ($this->tableparams['wrap'] $col) $this->xls->getactivesheet()->getstyle(phpexcel_cell::stringfromcolumnindex($col).($this->tableparams['header_row'] + 1).':'.phpexcel_cell::stringfromcolumnindex($col).($this->tableparams['header_row'] + $this->tableparams['row_count']))->getalignment()->setwraptext(true); } /** * write array of data actual row starting column defined offset * offset can textual or numeric representation */ public function adddata($data, $offset = 0) { // solve textual representation if (!is_numeric($offset)) $offset = phpexcel_cell::columnindexfromstring($offset); foreach ($data $d) { $this->xls->getactivesheet()->setcellvaluebycolumnandrow($offset++, $this->row, $d); } $this->row++; } /** * output file browser */ public function output($filename = 'export.xlsx') { // set layout $this->view->layout = ''; // headers header('content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('content-disposition: attachment;filename="'.$filename.'"'); header('cache-control: max-age=0'); // writer $objwriter = phpexcel_iofactory::createwriter($this->xls, 'excel2007'); $objwriter->save('php://output'); // clear memory $this->xls->disconnectworksheets(); } /** * load vendor classes */ protected function loadessentials() { // load vendor class app::import('vendor', 'phpexcel/classes/phpexcel'); if (!class_exists('phpexcel')) { throw new cakeexception('vendor class phpexcel not found!'); } } } and controller:
public $helpers = array('phpexcel'); ... public function excel() { $this->set('participants', $this->participant->find('all')); } this view:
<?php $this->phpexcel->createworksheet(); $this->phpexcel->setdefaultfont('calibri', 12); // define table cells $table = array( array('label' => __('id'), 'width' => 'auto', 'filter' => true), array('label' => __('förnamn'), 'width' => 'auto', 'filter' => true), array('label' => __('efternamn'), 'width' => 'auto', 'filter' => true), array('label' => __('e-postadress'), 'width' => 'auto', 'filter' => true), array('label' => __('mobiltelefon'), 'width' => 'auto', 'filter' => true), array('label' => __('specialkost'), 'width' => 'auto', 'filter' => true), array('label' => __('enhet'), 'width' => 'auto', 'filter' => true), array('label' => __('seminarium'), 'width' => 'auto', 'filter' => true), array('label' => __('utanför stockholm'), 'width' => 'auto', 'filter' => true), array('label' => __('dela rum'), 'width' => 'auto', 'filter' => true), array('label' => __('transfer'), 'width' => 'auto', 'filter' => true), array('label' => __('bara där på dagen'), 'width' => 'auto', 'filter' => true), array('label' => __('låt'), 'width' => 'auto', 'filter' => true), array('label' => __('lärare som blivit hyllad'), 'width' => 'auto', 'filter' => true), array('label' => __('kommentar'), 'width' => 'auto', 'filter' => true), ); // heading $this->phpexcel->addtableheader($table, array('name' => 'cambria', 'bold' => true)); foreach ($participants $d) { $this->phpexcel->addtablerow(array( $d['participant']['id'], $d['participant']['f_name'], $d['participant']['l_name'], $d['participant']['email'], $d['participant']['mobile_phone'], $d['participant']['special'], $d['participant']['school'], $d['participant']['seminarium_id'], $d['participant']['outside_sthlm'], $d['participant']['share_room'], $d['participant']['transfer'], $d['participant']['only_day'], $d['participant']['song'], $d['participant']['teacher'], $d['participant']['comments'] )); } $this->phpexcel->addtablefooter(); $this->phpexcel->output(); $this->phpexcel->exit(); ?> when try download in firefox right file extension, xlsx when trying download safari gives me report.xlsx.html ? , file becomes useless unless rename report.xlsx, how come?
first of all, should set 'layout' 'false' prevent view being loaded in default (html) layout of website.
also set response-type type excel (as others have mentioned. can via response-object (http://book.cakephp.org/2.0/en/controllers/request-response.html#dealing-with-content-types)
and finally, not exit(); in helper. if you're not rendering view in layout, don't have exit after outputting result
in controller;
public $helpers = array('phpexcel'); ... public function excel() { // disable layout $this->layout = false; // add/define xls contenttype $this->response->type(array('xls' => 'application/vnd.ms-excel')); // set response content-type xls $this->response->type('xls'); $this->set('participants', $this->participant->find('all')); } note mark baker mentioned, xls , xlsx use different mime-type, example uses mime type 'classic' xls, if you're outputting xlsx, modify mime type accordingly
Comments
Post a Comment