XLSXExporter.php in Loft Data Grids 7.2
File
vendor/aklump/loft_data_grids/src/AKlump/LoftDataGrids/XLSXExporter.php
View source
<?php
namespace AKlump\LoftDataGrids;
class XLSXExporter extends Exporter implements ExporterInterface {
protected $extension = '.xlsx';
protected $sheets = array();
protected $excel;
public function __construct(ExportDataInterface $data = null, $filename = '', $properties = array()) {
parent::__construct($data, $filename);
$this->output = false;
$this->excel = new \PHPExcel();
if ($properties) {
$this
->setProperties($properties);
}
}
public function setProperties($properties) {
$obj = $this->excel
->getProperties();
foreach ($properties as $property_name => $value) {
$method = "set{$property_name}";
if (method_exists($obj, $method)) {
$obj
->{$method}($value);
}
}
return $this;
}
public function getInfo() {
$info = parent::getInfo();
$info = array(
'name' => 'Excel Format',
'shortname' => 'Excel',
'description' => 'Export data in the .xlsx file format.',
) + $info;
return $info;
}
public function setTitle($title) {
parent::setTitle($title);
$this->excel
->getProperties()
->setTitle($title);
}
public function export($page_id = null) {
return $this->excel;
}
public function compile($page_id = null) {
$pages = $this
->getData()
->get();
$this->output = !empty($pages);
foreach ($pages as $page_id => $data) {
if (empty($this->sheets)) {
$this->sheets[] = $page_id;
}
elseif (!in_array($page_id, $this->sheets)) {
$this->sheets[] = $page_id;
$this->excel
->createSheet();
}
$active_sheet = array_search($page_id, $this->sheets);
$this->excel
->setActiveSheetIndex($active_sheet);
$worksheet = $this->excel
->getActiveSheet();
$header = $this
->getHeader($page_id);
$row_index = 1;
$worksheet
->fromArray($header, null, 'A' . $row_index++, true);
foreach ($data as $row) {
$col_index = 'A';
$worksheet
->fromArray($row, null, 'A' . $row_index, true);
$row_index++;
}
}
return $this;
}
public function save($filename = '', $page_id = null) {
if (empty($this->output)) {
$this
->compile($page_id);
}
if ($filename) {
$this
->setFilename($filename);
}
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $this
->getFilename() . '"');
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
$objWriter
->save('php://output');
exit;
}
public function saveFile($directory, $filename = '', $page_id = null) {
$filename = $filename ? $this
->setFilename($filename) : $this
->getFilename();
if (!is_writable($directory)) {
throw new \RuntimeException("{$directory} is not writable; cannot save {$filename}.");
}
if (empty($this->output)) {
$this
->compile($page_id);
}
$path = $directory . '/' . $filename;
$objWriter = \PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
$objWriter
->save($path);
return $path;
}
public function formatColumn($column, $format_code) {
$format_code = isset($format_code) ? $format_code : 'USD';
$phpexcel_format = \PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE;
if ($format_code === 'USD') {
$phpexcel_format = \PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE;
}
elseif ($format_code === \PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_USD_SIMPLE) {
$format_code = 'USD';
}
$columns = $this
->getPHPExcelColumns();
if (!array_key_exists($column, $columns)) {
return;
}
$phpexcel_column = $columns[$column];
$page = $this->excel
->getActiveSheet();
foreach ($page
->getRowIterator() as $row) {
$row_index = $row
->getRowIndex();
$page
->getStyle("{$phpexcel_column}{$row_index}")
->getNumberFormat()
->setFormatCode($phpexcel_format);
}
return parent::formatColumn($column, $format_code);
}
public function getPHPExcelColumns() {
$columns = array();
$current = array(
65,
);
$chr =& $current[count($current) - 1];
foreach ($this
->getHeader() as $header_key) {
$columns[$header_key] = chr($chr++);
}
return $columns;
}
public function getProperty($property_name) {
$obj = $this->excel
->getProperties();
$method = "get{$property_name}";
return method_exists($obj, $method) ? $obj
->{$method}() : null;
}
}