Advertisement
AmourSpirit

xlsx.php for joomla and PhpExcel

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