Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use Akeeba\Engine\Base\Object;
- // custom doc for Joomla when usnig xls via PhpExcel
- // File name: xls.php
- // file path /libraries/joomla/document/xls/
- // see also http://pastebin.com/71iRCXB1 (Joomla PhpExcel working example first report)
- // see also http://pastebin.com/jptfwmxw (Joomla PhpExcel layout working example)
- // see also http://pastebin.com/TfKq8NZ4 (xlsx.php for joomla and PhpExcel)
- // Check to ensure this file is included in Joomla!
- defined('_JEXEC') or die();
- //Include Joomla Classes
- JLoader::import('joomla.filesystem.folder');
- //Include PHPExcel classes
- JLoader::import('phpexcel.Classes.PHPExcel');
- JLoader::import('phpexcel.Classes.PHPExcel.IOFactory');
- class JDocumentXLS extends JDocument
- {
- private $_name = 'export';
- private $_phpexcel = null;
- /**
- * Class Constructor
- *
- * @param unknown $options
- */
- public function __construct($options = array())
- {
- parent::__construct($options);
- $this->setMimeEncoding('application/vnd.ms-excel');
- $this->setType('xls');
- if(isset($options['name'])) $this->setName($options['name']);
- }
- /**
- * To get the current filename for the excel file
- * @return string
- */
- public function getName()
- {
- return $this->_name;
- }
- /**
- * To get the PHPExcel object for use
- * @return Object
- */
- public function &getPhpExcelObj()
- {
- if(!$this->_phpexcel) $this->_phpexcel = new PHPExcel();
- return $this->_phpexcel;
- }
- /**
- * (non-PHPdoc)
- * @see JDocument::render()
- */
- public function render($cache = false, $params = array())
- { //Write out response headers
- JResponse::setHeader('Pragma', 'public', true);
- JResponse::setHeader('Expires', '0', true);
- JResponse::setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true);
- JResponse::setHeader('Content-Type', 'application/force-download', true);
- JResponse::setHeader('Content-Type', 'application/octet-stream', true);
- JResponse::setHeader('Content-Type', 'application/download', true);
- JResponse::setHeader('Content-Transfer-Encoding', 'binary', true);
- //Set workbook properties to some defaults if not currently set
- //Currently all these properties are not set for the Excel5 (xls) writer but are here in case future versions do
- $objPhpExcel =& $this->getPhpExcelObj();
- $config = new JConfig();
- $workbook_properties = $objPhpExcel->getProperties();
- if(!$workbook_properties->getCategory()) $workbook_properties->setCategory('Exported Report From '. $config->sitename);
- if($workbook_properties->getCompany() == 'Microsoft Corporation' && $config->sitename) $workbook_properties->setCompany($config->sitename);
- if($workbook_properties->getCreator() == 'Unknown Creator' && $config->sitename) $workbook_properties->setCreator($config->sitename);
- if(!$workbook_properties->getDescription()) $workbook_properties->setDescription($this->getDescription());
- if(!$workbook_properties->getLastModifiedBy()) $workbook_properties->setLastModifiedBy($config->sitename);
- if(!$workbook_properties->getSubject()) $workbook_properties->setSubject($this->getTitle());
- if($workbook_properties->getTitle() == 'Untitled Spreadsheet' && $this->getTitle()) $workbook_properties->setTitle($this->getTitle());
- $objPhpExcel->setProperties($workbook_properties);
- //Get the Excel 5 type IO object to write out the binary document
- $objWriter = PHPExcel_IOFactory::createWriter($objPhpExcel, 'Excel5');
- if(JFolder::exists($config->tmp_path)) $objWriter->setTempDir($config->tmp_path);
- //Save the file to the PHP Output Stream and read the stream back in to set the buffer
- ob_start();
- $objWriter->save('php://output');
- $buffer = ob_get_contents();
- ob_end_clean();
- JResponse::setHeader('Content-disposition', 'attachment; filename="' . $this->getName() . '.' . $this->getType() . '"; size=' . strlen($buffer) . ';', true);
- return $buffer;
- }
- /**
- * To set the PHPExcel object for use
- * @param unknown $objPhpExcel
- */
- public function setPhpExcelObj($objPhpExcel)
- {
- $this->_phpexcel = $objPhpExcel;
- }
- /**
- * To set the current filename for the excel file
- * @param string $name
- */
- public function setName($name)
- {
- $this->_name = JFilterOutput::stringURLSafe($name);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement