-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprice_list_opt.php
More file actions
73 lines (60 loc) · 2.54 KB
/
price_list_opt.php
File metadata and controls
73 lines (60 loc) · 2.54 KB
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
<?php
require 'vendor/autoload.php';
ini_set('memory_limit', '2048M');
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
class PriceListOpt
{
private $dir = '/upload/dealer_assortment/price_list_opt/';
/**
* Шаблонный файл
* @var string
*/
private $fileTemplate = 'price_list_template.xlsx';
public function __construct()
{
if (!is_dir($this->dir) ) {
mkdir($this->dir, null, true);
}
}
public function generateExcel()
{
// вернуться к варианту с макросами (тогда файл читался) + эл.упр.
// иначе: в ручную сделать шаблон копипастом листов
// на листе "Прайс-заказ" - фильтры, группировка строк.
try {
$obSpreadsheet = IOFactory::createReader("Xlsx")->load($this->fileTemplate);
$obActiveSheet = $obSpreadsheet->setActiveSheetIndexByName('Прайс-заказ');
$arData = [
['2010', 'Q1', 'United States', 790],
['2010', 'Q2', 'United States', 730],
['2010', 'Q2', 'United States', 730],
['2010', 'Q1', 'Belgium', 380],
['2011', 'Q2', 'Belgium', 390],
['2011', 'Q2', 'Belgium', 390],
['2011', 'Q2', 'Belgium', 390],
['2011', 'Q2', 'Belgium', 390],
['2011', 'Q2', 'Belgium', 390],
['2011', 'Q2', 'Belgium', 390],
];
$obActiveSheet->fromArray($arData, null, 'A2');
$obActiveSheet->getStyle('A1:D1')->getFont()->setBold(true);
// Установка фильтров
$obActiveSheet->setAutoFilter($obSpreadsheet->getActiveSheet()->calculateWorksheetDimension());
// Группировка строк
foreach ($arData as $key => $value) {
if ($key >= 3 && $key <= 6) {
// $indexRow = $key + 3;
$obActiveSheet->getRowDimension($key)->setOutlineLevel(1);
$obActiveSheet->getRowDimension($key)->setVisible(false);
}
}
$obActiveSheet->getRowDimension(6)->setCollapsed(true);
$writer = new Xlsx($obSpreadsheet);
$writer->save('price_list_opt.xlsx');
} catch (Exception $exception) {
echo $exception->getMessage();
}
}
}
(new PriceListOpt())->generateExcel();