1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
// 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;
/*------------------------------------------------------------------------------------------------------------
Method Name : __construct
Purpose : Class Constructor
Parameter : $options
Returns : None
Revisions :
------------------------------------------------------------------------------------------------------------*/
function __construct($options = array())
{ parent::__construct($options);
$this->setMimeEncoding('application/vnd.ms-excel');
//$this->setMimeEncoding('application/msexcel ');
$this->setType('xls');
if(isset($options['name'])) $this->setName($options['name']);
}
/*------------------------------------------------------------------------------------------------------------
Method Name : getName
Purpose : To get the current filename for the excel file
Parameter : None
Returns : String
Revisions :
------------------------------------------------------------------------------------------------------------*/
function getName()
{ return $this->_name;
}
/*------------------------------------------------------------------------------------------------------------
Method Name : getPhpExcelObj
Purpose : To get the PHPExcel object for use
Parameter : None
Returns : Object
Revisions :
------------------------------------------------------------------------------------------------------------*/
function &getPhpExcelObj()
{ if(!$this->_phpexcel) $this->_phpexcel = new PHPExcel();
return $this->_phpexcel;
}
/*------------------------------------------------------------------------------------------------------------
Method Name : render
Purpose : To render the document
Parameter : @param boolean $cache If true, cache the output
Parameter : @param array $params Associative array of attributes
Returns : String
Revisions :
------------------------------------------------------------------------------------------------------------*/
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/vnd.ms-excel', true);
//JResponse::setHeader('Content-Type', 'application/msexcel ', 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);
//JResponse::setHeader('Content-disposition', 'attachment; filename='.$this->getName() . '.xls; size=' . strlen($buffer) . ';', true);
JResponse::setHeader('Content-Disposition', 'inline; filename='.$this->getName() . '.xls; size=' . strlen($buffer) . ';', 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();
//***************************** MODIF VERDY *****************************************
$objWriter->save('php://output');
//$objWriter->save('C:\\'.$this->getName() .'.xls');
//$objWriter->save(str_replace('.php', '.xls', __FILE__));
//***********************************************************************************
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
/*------------------------------------------------------------------------------------------------------------
Method Name : setPhpExcelObj
Purpose : To set the PHPExcel object for use
Parameter : $objPhpExcel
Returns : None
Revisions :
------------------------------------------------------------------------------------------------------------*/
function setPhpExcelObj($objPhpExcel)
{ $this->_phpexcel = $objPhpExcel;
}
/*------------------------------------------------------------------------------------------------------------
Method Name : setName
Purpose : To set the current filename for the excel file
Parameter : $name
Returns : None
Revisions :
------------------------------------------------------------------------------------------------------------*/
function setName($name)
{ $this->_name = JFilterOutput::stringURLSafe($name);
}
} |
Partager