Advertisement
AmourSpirit

Joomla PhpExcel working example first report

Jul 31st, 2015
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.42 KB | None | 0 0
  1. <?php
  2. // Check to ensure this file is included in Joomla!
  3. defined('_JEXEC') or die();
  4. //
  5. // view.xls.php code and view.xlsx.php code.
  6. // See Also: http://pastebin.com/jptfwmxw (Joomla PhpExcel layout working example)
  7. // see also http://pastebin.com/TfKq8NZ4 (xlsx.php for joomla and PhpExcel)
  8. // see also http://pastebin.com/kM9wtd1c (xls.php for joomla and PhpExcel)
  9. // Import PHPExcel library
  10. jimport('phpexcel.library.PHPExcel');
  11.  
  12. //Import needed Joomla classes
  13. jimport('joomla.application.component.view');
  14. jimport('joomla.utilities.date');
  15. jimport('joomla.filesystem.file');
  16.  
  17. class Cn_reportsViewRetailpartnersalesbypartners extends JViewLegacy
  18. {
  19.     /**
  20.      * Class Constructor
  21.      */
  22.     public function __construct()
  23.     {
  24.         parent::__construct();
  25.     }
  26.  
  27.     /**
  28.      * To display the page
  29.      * @param string $tpl template to use
  30.      */
  31.     public function display($tpl = null)
  32.     {
  33.         global $mainframe;
  34.         // Get the user requesting this view
  35.         $params = JComponentHelper::getParams('com_cn_reports');
  36.  
  37.         $user = JFactory::getUser();
  38.         $utz = $user->getParam('timezone');
  39.         $unow = JFactory::getDate('now', $utz);
  40.         $model = $this->getModel();
  41.         // Get the date properties from the current model state
  42.         $startDate = $model->getTzStartDate();  // get the start date as JDate
  43.         $endDate = $model->getTzEndDate();      // get the end date as JDate
  44.         $rname = $model->getRetailPartnerName();    // et the retail partner name from the model
  45.         // $ruserid =$model->getRetailPartnerId();
  46.  
  47.  
  48.         // Get and set the document properties
  49.         $document = JFactory::getDocument();
  50.         $date = new JDate();
  51.         $download_desc = JText::sprintf('COM_CN_REPORTS_VIEW_RETAILPARTNERSALESBYPARTNERS_XLS_DOWNLOAD_DESC', JText::_('COM_CN_REPORTS_VIEW_RETAILPARTNERSALESBYPARTNERS_XLS_COMMISSIONS_TITLE'), $user->name, $date->format('%Y-%m-%d'), $user->name);
  52.         $document->setDescription($download_desc);
  53.  
  54.         $download_title = JText::sprintf('COM_CN_REPORTS_VIEW_RETAILPARTNERSALESBYPARTNERS_XLS_DOWNLOAD_TITLE',
  55.                 $rname, $startDate->format('Y-m-d'), $endDate->format('Y-m-d'));
  56.         $document->setName($download_title);
  57.         $document->setTitle($download_title);
  58.         //Get the PHPExcel object to set some properties
  59.         $objPhpExcel =& $document->getPhpExcelObj();
  60.         $objPhpExcel->getProperties()->setCreator($user->name)->setLastModifiedBy($user->name);
  61.  
  62.         $objPhpExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&C' . $download_desc);
  63.         $objPhpExcel->getActiveSheet()->setTitle(JText::_('COM_CN_REPORTS_VIEW_RETAILPARTNERSALESBYPARTNERS_XLS_COMMISSIONS_TITLE'));
  64.  
  65.         //$myWorkSheet = new PHPExcel_Worksheet($objPhpExcel, JText::_('COM_CN_REPORTS_VIEW_RETAILPARTNERSALESBYPARTNERS_XLS_OVERRIDES_TITLE'));
  66.         // Attach the “My Data” worksheet as the first worksheet in the PHPExcel object
  67.         //$objPhpExcel->addSheet($myWorkSheet, 1);
  68.         $objPhpExcel->setActiveSheetIndex(0);
  69.  
  70.         // Assign the data from the model to the view template - getData(false) in this model gets all rows
  71.         // ignoring pagination otherwise getData would return only the current data page on the screen
  72.         // $this->assignRef('rows', $this->getModel()->getData(false));
  73.         $items = $model->getItems();
  74.         $cn_reports_export_xls_show_grid                = $params->get('cn_reports_export_xls_show_grid');
  75.         $cn_reports_export_xls_logo                     = $params->get('cn_reports_export_xls_logo');
  76.         $cn_reports_export_xls_title                    = $params->get('cn_reports_export_xls_title');
  77.         $cn_reports_export_xls_header                   = $params->get('cn_reports_export_xls_header');
  78.         $cn_reports_export_xls_footer                   = $params->get('cn_reports_export_xls_footer');
  79.         $cn_reports_export_xls_rowColumnTitleBgColor    = $params->get('cn_reports_export_xls_rowcolumntitlebgcolor');
  80.         $cn_reports_export_xls_rowColumnTitleBgColor    = str_ireplace('#', '', $cn_reports_export_xls_rowColumnTitleBgColor);
  81.         $cn_reports_export_xls_rowcolumntitlefontcolor  = $params->get('cn_reports_export_xls_rowcolumntitlefontcolor');
  82.         $cn_reports_export_xls_rowcolumntitlefontcolor  = str_ireplace('#', '', $cn_reports_export_xls_rowcolumntitlefontcolor);
  83.         $cn_reports_export_xls_rowRptTitleFontcolor     = $params->get('cn_reports_export_xls_rowrpttitlefontcolor');
  84.         $cn_reports_export_xls_rowRptTitleFontcolor     = str_ireplace('#', '', $cn_reports_export_xls_rowRptTitleFontcolor);
  85.         $cn_reports_export_xls_rowColumnHeaderBgColor   = $params->get('cn_reports_export_xls_rowcolumnheaderbgcolor');
  86.         $cn_reports_export_xls_rowColumnHeaderBgColor   = str_ireplace('#', '', $cn_reports_export_xls_rowColumnHeaderBgColor);
  87.         $cn_reports_export_xls_rowColumnHeaderFontcolor = $params->get('cn_reports_export_xls_rowcolumnheaderfontcolor');
  88.         $cn_reports_export_xls_rowColumnHeaderFontcolor = str_ireplace('#', '', $cn_reports_export_xls_rowColumnHeaderFontcolor);
  89.         $cn_reports_export_xls_rowcolumnfooterfontcolor = $params->get('cn_reports_export_xls_rowcolumnfooterfontcolor');
  90.         $cn_reports_export_xls_rowcolumnfooterfontcolor = str_ireplace('#', '', $cn_reports_export_xls_rowcolumnfooterfontcolor);
  91.         $cn_reports_export_xls_rowcolumnfooterbgcolor   = $params->get('cn_reports_export_xls_rowcolumnfooterbgcolor');
  92.         $cn_reports_export_xls_rowcolumnfooterbgcolor   = str_ireplace('#', '', $cn_reports_export_xls_rowcolumnfooterbgcolor);
  93.         $cn_reports_export_xls_rowaltbgcolor            = $params->get('cn_reports_export_xls_rowaltbgcolor');
  94.         $cn_reports_export_xls_rowaltbgcolor            = str_ireplace('#', '', $cn_reports_export_xls_rowaltbgcolor);
  95.         $cn_reports_export_xls_rowaltfontcolor          = $params->get('cn_reports_export_xls_rowaltfontcolor');
  96.         $cn_reports_export_xls_rowaltfontcolor          = str_ireplace('#', '', $cn_reports_export_xls_rowaltfontcolor);
  97.  
  98.         $this->assignRef('rows', $items);
  99.         $this->assignRef('reportStart', $startDate);
  100.         $this->assignRef('reportEnd', $endDate);
  101.         $this->assignRef('partnerName', $rname);
  102.         //Assign the PHPExcel object for use in the template
  103.         $this->assignRef('phpexcel', $objPhpExcel);
  104.         $this->assignRef('showGrid', $cn_reports_export_xls_show_grid);
  105.         $this->assignRef('showLogo', $cn_reports_export_xls_logo);
  106.         $this->assignRef('showTitle', $cn_reports_export_xls_title);
  107.         $this->assignRef('showHeader', $cn_reports_export_xls_header);
  108.         $this->assignRef('showFooter', $cn_reports_export_xls_footer);
  109.         $this->assignRef('rowColumnTitleBgColor', $cn_reports_export_xls_rowColumnTitleBgColor);
  110.         $this->assignRef('rowColumnTitleFontColor', $cn_reports_export_xls_rowcolumntitlefontcolor);
  111.         $this->assignRef('rowRptTitleFontcolor', $cn_reports_export_xls_rowRptTitleFontcolor);
  112.         $this->assignRef('rowColumnHeaderBgColor', $cn_reports_export_xls_rowColumnHeaderBgColor);
  113.         $this->assignRef('rowColumnHeaderFontColor', $cn_reports_export_xls_rowColumnHeaderFontcolor);
  114.         $this->assignRef('rowcolumnfooterfontcolor', $cn_reports_export_xls_rowcolumnfooterfontcolor);
  115.         $this->assignRef('rowcolumnfooterbgcolor', $cn_reports_export_xls_rowcolumnfooterbgcolor);
  116.         $this->assignRef('altRowaltBgColor', $cn_reports_export_xls_rowaltbgcolor);
  117.         $this->assignRef('rowAltFontColor', $cn_reports_export_xls_rowaltfontcolor);
  118.  
  119.         // I use the 'excel' template in the <View Folder Name>/tmpl folder for a variety of document types to
  120.         // provide consistency between those document types (xls, xlsx, cvs, pdf) generated by PHPExcel but you
  121.         // could use 'xls' here if you preferred instead to create a specific XLS template.
  122.         $jinput = JFactory::getApplication()->input;
  123.         $layout = $jinput->get('layout', 'excel', 'CMD');
  124.         if(is_null($tpl)) $tpl = $layout;
  125.         //Display the results
  126.         parent::display($tpl);
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement