Skip to content

Instantly share code, notes, and snippets.

@noherczeg
Created December 20, 2013 13:52
Show Gist options
  • Save noherczeg/8055060 to your computer and use it in GitHub Desktop.
Save noherczeg/8055060 to your computer and use it in GitHub Desktop.
phpexcel magic
<?php
abstract class AbstractReport {
/** @var string Jelentes fileok mappaja */
protected $reports_folder = null;
protected $records = 0;
/** @var int Cimsav sorainak szama */
protected $title_section_rows = 2;
/** @var \PHPExcel Dokumentum objektum */
protected $document = null;
/** @var PHPExcel_Worksheet Dokumentum ful */
protected $sheet = null;
/** @var array Beallitasok */
protected $settings = array();
/** @var array Formazasi beallitasok */
protected $styles = array();
/** @var string Mentendo file neve */
protected $file_name = '';
/** @var string Mentett file konkret eleresi utvonala */
protected $file_path = '';
public function __construct() {
// beallitjuk a mentes helyet
$this->reports_folder = public_path() . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'reports' . DIRECTORY_SEPARATOR;
// inicializaljuk a feldolgozo dokumentumot
$this->document = new PHPExcel();
// beallitjuk a stilusokat
$this->styles = $this->set_styles();
}
/**
* Jelentes keszitese
*
* @param array $arrayApplications
* @param array $settings Beallitasok
* @return void
*/
abstract public function create(array $arrayApplications, array $settings);
/**
* Feltolti a tablazatot sorokkal
*
* @param array $data Sorok
* @return void
*/
protected function populate(array $data) {
// 1 bazisu!
$row = $this->title_section_rows + 1;
$counter = 1;
foreach($data AS $val) {
$col = 1;
$this->sheet->setCellValueByColumnAndRow(0, $row, $counter);
foreach($val AS $iVal) {
$this->sheet->setCellValueByColumnAndRow($col, $row, $iVal);
$col++;
}
$counter++;
$row++;
}
}
/**
* Dokumentum metaadatokat allit be
*
* @param array $data
* @return void
*/
protected function set_metadata(array $data) {
$allowed_methods = array('creator', 'title', 'created', 'company', 'subject', 'modified');
foreach ($data as $key => $value) {
if (!in_array($key, $allowed_methods)) {
continue;
}
$method_name = 'set' . ucfirst(strtolower($key));
$this->document->$method_name($value);
}
}
/**
* Kiirja tarolora az osszeallitott filet
*
* @param string $file_name File neve (datum prefixet fog kapni!)
* @param string $file_type File tipusa pl. Excel2007
* @return void
*/
protected function write ($file_name = '', $file_type = 'Excel2007') {
$writer = PHPExcel_IOFactory::createWriter($this->document, $file_type);
$this->file_name = date('Ymd_His_', time()) . $file_name;
$this->file_path = $this->reports_folder . $this->file_name;
$writer->save($this->file_path);
}
/**
* Stilusokat allitja ossze es adja vissza
*
* @return array
*/
protected function set_styles() {
$styles = array();
$header_border = array(
'style' => PHPExcel_Style_Border::BORDER_THICK,
'color' => array('rgb' => '000000')
);
$styles['header'] = array(
'borders' => array(
'bottom' => $header_border,
'left' => $header_border,
'top' => $header_border,
'right' => $header_border
),
'font' => array(
'bold' => true
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER
)
);
$styles['borderArray'] = array(
'borders' => array(
'allborders' => array(
'style' => PHPExcel_Style_Border::BORDER_THIN
)
)
);
return $styles;
}
/**
* Visszater a kuldendo file eleresi utvonalaval, vagy hamissal
*
* @return boolean
*/
public function file_path() {
if (strlen($this->file_path) == 0 || is_null($this->file_path)) {
return false;
}
return $this->file_path;
}
/**
* Mentett file nevet adja vissza
*
* @return string
*/
public function get_filename() {
return $this->file_name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment