Add action to export directly to Excel xlsx files.
This commit is contained in:
1 parent
284fcc72f7
commit
8804720cb6
2 files changed
+75
-1
No files matched your search
@@ -232,6 +232,7 @@ a.module_toggle_all{color: #268CCD;}
|
|||||||
.toolbarBox .process-icon-save-and-preview { background-image: url('../img/process-icon-preview.png');}
|
.toolbarBox .process-icon-save-and-preview { background-image: url('../img/process-icon-preview.png');}
|
||||||
.toolbarBox .process-icon-import { background-image: url('../img/process-icon-export-csv.png');}
|
.toolbarBox .process-icon-import { background-image: url('../img/process-icon-export-csv.png');}
|
||||||
.toolbarBox .process-icon-export { background-image: url('../img/process-icon-export-csv.png');}
|
.toolbarBox .process-icon-export { background-image: url('../img/process-icon-export-csv.png');}
|
||||||
|
.toolbarBox .process-icon-export-excel { background-image: url('../img/process-icon-export-csv.png');}
|
||||||
.toolbarBox .process-icon-export-all { background-image: url('../img/process-icon-export-csv-details.png');}
|
.toolbarBox .process-icon-export-all { background-image: url('../img/process-icon-export-csv-details.png');}
|
||||||
.toolbarBox .process-icon-export-stock-state-quantities-csv { background-image: url('../img/process-icon-export-csv.png');}
|
.toolbarBox .process-icon-export-stock-state-quantities-csv { background-image: url('../img/process-icon-export-csv.png');}
|
||||||
.toolbarBox .process-icon-export-stock-state-prices-csv { background-image: url('../img/process-icon-export-csv-details.png');}
|
.toolbarBox .process-icon-export-stock-state-prices-csv { background-image: url('../img/process-icon-export-csv-details.png');}
|
||||||
|
|||||||
@@ -658,6 +658,69 @@ class AdminControllerCore extends Controller
|
|||||||
$this->layout = 'layout-export.tpl';
|
$this->layout = 'layout-export.tpl';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function processExportExcel()
|
||||||
|
{
|
||||||
|
// clean buffer
|
||||||
|
if (ob_get_level() && ob_get_length() > 0)
|
||||||
|
ob_clean();
|
||||||
|
$this->getList($this->context->language->id);
|
||||||
|
if (!count($this->_list))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!extension_loaded('zip')) {
|
||||||
|
$this->displayWarning($this->l('Module php_zip must be enabled in order to use Excel export feature.'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8');
|
||||||
|
header('Cache-Control: no-store, no-cache');
|
||||||
|
header('Content-disposition: attachment; filename="'.$this->table.'_'.date('Y-m-d_His').'.xlsx"');
|
||||||
|
|
||||||
|
$headers = array();
|
||||||
|
foreach ($this->fields_list as $datas)
|
||||||
|
$headers[] = Tools::htmlentitiesDecodeUTF8($datas['title']);
|
||||||
|
$content = array();
|
||||||
|
foreach ($this->_list as $i => $row)
|
||||||
|
{
|
||||||
|
$content[$i] = array();
|
||||||
|
$path_to_image = false;
|
||||||
|
foreach ($this->fields_list as $key => $params)
|
||||||
|
{
|
||||||
|
$field_value = isset($row[$key]) ? Tools::htmlentitiesDecodeUTF8($row[$key]) : '';
|
||||||
|
if ($key == 'image')
|
||||||
|
{
|
||||||
|
if ($params['image'] != 'p' || Configuration::get('PS_LEGACY_IMAGES'))
|
||||||
|
$path_to_image = Tools::getShopDomain(true)._PS_IMG_.$params['image'].'/'.$row['id_'.$this->table].(isset($row['id_image']) ? '-'.(int)$row['id_image'] : '').'.'.$this->imageType;
|
||||||
|
else
|
||||||
|
$path_to_image = Tools::getShopDomain(true)._PS_IMG_.$params['image'].'/'.Image::getImgFolderStatic($row['id_image']).(int)$row['id_image'].'.'.$this->imageType;
|
||||||
|
if ($path_to_image)
|
||||||
|
$field_value = $path_to_image;
|
||||||
|
}
|
||||||
|
$content[$i][] = $field_value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
require_once(_PS_ROOT_DIR_.'/tools/phpexcel/PHPExcel.php');
|
||||||
|
/** Create a new PHPExcel Object **/
|
||||||
|
$objPHPExcel = new PHPExcel();
|
||||||
|
$worksheet = $objPHPExcel->getSheet(0);
|
||||||
|
|
||||||
|
$worksheet->fromArray(
|
||||||
|
$headers, // The data to set
|
||||||
|
NULL, // Array values with this value will not be set
|
||||||
|
'A1' // Top left coordinate of the worksheet range where
|
||||||
|
// we want to set these values (default is A1)
|
||||||
|
);
|
||||||
|
$worksheet->fromArray(
|
||||||
|
$content,
|
||||||
|
NULL,
|
||||||
|
'A2'
|
||||||
|
);
|
||||||
|
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
|
||||||
|
$objWriter->save('php://output');
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Object Delete
|
* Object Delete
|
||||||
*/
|
*/
|
||||||
@@ -1126,11 +1189,16 @@ class AdminControllerCore extends Controller
|
|||||||
'href' => self::$currentIndex.'&add'.$this->table.'&token='.$this->token,
|
'href' => self::$currentIndex.'&add'.$this->table.'&token='.$this->token,
|
||||||
'desc' => $this->l('Add new')
|
'desc' => $this->l('Add new')
|
||||||
);
|
);
|
||||||
if ($this->allow_export)
|
if ($this->allow_export) {
|
||||||
$this->toolbar_btn['export'] = array(
|
$this->toolbar_btn['export'] = array(
|
||||||
'href' => self::$currentIndex.'&export'.$this->table.'&token='.$this->token,
|
'href' => self::$currentIndex.'&export'.$this->table.'&token='.$this->token,
|
||||||
'desc' => $this->l('Export')
|
'desc' => $this->l('Export')
|
||||||
);
|
);
|
||||||
|
$this->toolbar_btn['export-excel'] = array(
|
||||||
|
'href' => self::$currentIndex.'&export-excel'.$this->table.'&token='.$this->token,
|
||||||
|
'desc' => $this->l('Export (Excel)')
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
$this->addToolBarModulesListButton();
|
$this->addToolBarModulesListButton();
|
||||||
}
|
}
|
||||||
@@ -2057,6 +2125,11 @@ class AdminControllerCore extends Controller
|
|||||||
if ($this->tabAccess['view'] === '1')
|
if ($this->tabAccess['view'] === '1')
|
||||||
$this->action = 'export';
|
$this->action = 'export';
|
||||||
}
|
}
|
||||||
|
elseif (isset($_GET['export-excel'.$this->table]))
|
||||||
|
{
|
||||||
|
if ($this->tabAccess['view'] === '1')
|
||||||
|
$this->action = 'exportExcel';
|
||||||
|
}
|
||||||
/* Cancel all filters for this tab */
|
/* Cancel all filters for this tab */
|
||||||
elseif (isset($_POST['submitReset'.$this->list_id]))
|
elseif (isset($_POST['submitReset'.$this->list_id]))
|
||||||
$this->action = 'reset_filters';
|
$this->action = 'reset_filters';
|
||||||
|
|||||||
Reference in new issue
Block a user