Advertisement
AmourSpirit

xls.php for joomla and PhpExcel

Jul 31st, 2015
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.32 KB | None | 0 0
  1. <?php
  2. use Akeeba\Engine\Base\Object;
  3. // custom doc for Joomla when usnig xls via PhpExcel
  4. // File name: xls.php
  5. // file path /libraries/joomla/document/xls/
  6. // see also http://pastebin.com/71iRCXB1 (Joomla PhpExcel working example first report)
  7. // see also http://pastebin.com/jptfwmxw (Joomla PhpExcel layout working example)
  8. // see also http://pastebin.com/TfKq8NZ4 (xlsx.php for joomla and PhpExcel)
  9. // Check to ensure this file is included in Joomla!
  10. defined('_JEXEC') or die();
  11.  
  12. //Include Joomla Classes
  13. JLoader::import('joomla.filesystem.folder');
  14.  
  15. //Include PHPExcel classes
  16. JLoader::import('phpexcel.Classes.PHPExcel');
  17. JLoader::import('phpexcel.Classes.PHPExcel.IOFactory');
  18.  
  19. class JDocumentXLS extends JDocument
  20. {
  21.   private $_name = 'export';
  22.   private $_phpexcel = null;
  23.  
  24.   /**
  25.    * Class Constructor
  26.    *
  27.    * @param unknown $options
  28.    */
  29.   public function __construct($options = array())
  30.   {
  31.     parent::__construct($options);
  32.     $this->setMimeEncoding('application/vnd.ms-excel');
  33.     $this->setType('xls');
  34.     if(isset($options['name'])) $this->setName($options['name']);
  35.   }
  36.  
  37.  
  38.   /**
  39.    * To get the current filename for the excel file
  40.    * @return string
  41.    */
  42.   public function getName()
  43.   {
  44.     return $this->_name;
  45.   }
  46.  
  47.  
  48.   /**
  49.    * To get the PHPExcel object for use
  50.    * @return Object
  51.    */
  52.  public function &getPhpExcelObj()
  53.   {
  54.     if(!$this->_phpexcel) $this->_phpexcel = new PHPExcel();
  55.     return $this->_phpexcel;
  56.   }
  57.  
  58.   /**
  59.    * (non-PHPdoc)
  60.    * @see JDocument::render()
  61.    */
  62.   public function render($cache = false, $params = array())
  63.   { //Write out response headers
  64.     JResponse::setHeader('Pragma', 'public', true);
  65.     JResponse::setHeader('Expires', '0', true);
  66.     JResponse::setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true);
  67.     JResponse::setHeader('Content-Type', 'application/force-download', true);
  68.     JResponse::setHeader('Content-Type', 'application/octet-stream', true);
  69.     JResponse::setHeader('Content-Type', 'application/download', true);
  70.     JResponse::setHeader('Content-Transfer-Encoding', 'binary', true);
  71.     //Set workbook properties to some defaults if not currently set
  72.     //Currently all these properties are not set for the Excel5 (xls) writer but are here in case future versions do
  73.     $objPhpExcel =& $this->getPhpExcelObj();
  74.     $config = new JConfig();
  75.     $workbook_properties = $objPhpExcel->getProperties();
  76.     if(!$workbook_properties->getCategory()) $workbook_properties->setCategory('Exported Report From '. $config->sitename);
  77.     if($workbook_properties->getCompany() == 'Microsoft Corporation' && $config->sitename) $workbook_properties->setCompany($config->sitename);
  78.     if($workbook_properties->getCreator() == 'Unknown Creator' && $config->sitename) $workbook_properties->setCreator($config->sitename);
  79.     if(!$workbook_properties->getDescription()) $workbook_properties->setDescription($this->getDescription());
  80.     if(!$workbook_properties->getLastModifiedBy()) $workbook_properties->setLastModifiedBy($config->sitename);
  81.     if(!$workbook_properties->getSubject()) $workbook_properties->setSubject($this->getTitle());
  82.     if($workbook_properties->getTitle() == 'Untitled Spreadsheet' && $this->getTitle()) $workbook_properties->setTitle($this->getTitle());
  83.     $objPhpExcel->setProperties($workbook_properties);
  84.     //Get the Excel 5 type IO object to write out the binary document
  85.     $objWriter = PHPExcel_IOFactory::createWriter($objPhpExcel, 'Excel5');
  86.  
  87.     if(JFolder::exists($config->tmp_path)) $objWriter->setTempDir($config->tmp_path);
  88.     //Save the file to the PHP Output Stream and read the stream back in to set the buffer
  89.     ob_start();
  90.     $objWriter->save('php://output');
  91.     $buffer = ob_get_contents();
  92.     ob_end_clean();
  93.     JResponse::setHeader('Content-disposition', 'attachment; filename="' . $this->getName() . '.' . $this->getType() . '"; size=' . strlen($buffer) . ';', true);
  94.     return $buffer;
  95.   }
  96.  
  97.   /**
  98.    * To set the PHPExcel object for use
  99.    * @param unknown $objPhpExcel
  100.    */
  101.   public function setPhpExcelObj($objPhpExcel)
  102.   {
  103.     $this->_phpexcel = $objPhpExcel;
  104.   }
  105.  
  106.   /**
  107.    * To set the current filename for the excel file
  108.    * @param string $name
  109.    */
  110.   public function setName($name)
  111.   {
  112.     $this->_name = JFilterOutput::stringURLSafe($name);
  113.   }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement