Controller.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. <?php
  2. namespace excel\web;
  3. use common\rest\JsonController;
  4. use yii\web\Controller as WebController;
  5. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  6. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  7. use PhpOffice\PhpSpreadsheet\IOFactory;
  8. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  9. use PhpOffice\PhpSpreadsheet\Style\Border;
  10. use PhpOffice\PhpSpreadsheet\Style\Fill;
  11. use yii\filters\auth\CompositeAuth;
  12. use yii\filters\auth\QueryParamAuth;
  13. use yii\web\Response;
  14. use Yii;
  15. class Controller extends WebController {
  16. public $app = null;
  17. public $req = null;
  18. public $res = null;
  19. const MIMETYPE_EXCEL = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  20. const TIPO_EXCEL = "excel";
  21. const TIPO_PDF = "pdf";
  22. const COLOR_NEGRO = "FF000000";
  23. const COLOR_AZUL = "FF1A95E8";
  24. const COLOR_GRIS = "FF959595";
  25. public $renglonActual = 1;
  26. public $renglonPrevio = 0;
  27. protected $spreadsheet;
  28. protected $activeSheet;
  29. public static $estiloCeldaNormal = [
  30. 'alignment' => [
  31. 'horizontal' => Alignment::HORIZONTAL_LEFT,
  32. ],
  33. ];
  34. public static $estiloCeldaCentrada = [
  35. 'font' => [
  36. 'bold' => true,
  37. 'size' => 16,
  38. 'color' => ['argb' => 'FF000000'],
  39. ],
  40. 'alignment' => [
  41. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  42. 'vertical' => Alignment::VERTICAL_CENTER,
  43. ],
  44. ];
  45. public static $estiloCeldaIzquierda = [
  46. 'font' => [
  47. 'bold' => true,
  48. 'size' => 16,
  49. 'color' => ['argb' => 'FF000000'],
  50. ],
  51. 'alignment' => [
  52. 'horizontal' => Alignment::HORIZONTAL_LEFT,
  53. 'vertical' => Alignment::VERTICAL_CENTER,
  54. ],
  55. ];
  56. public static $bordes = [
  57. 'borders' => [
  58. 'left' => [
  59. 'borderStyle' => Border::BORDER_THIN,
  60. 'color' => ['argb' => 'FF000000'],
  61. ],
  62. 'right' => [
  63. 'borderStyle' => Border::BORDER_THIN,
  64. 'color' => ['argb' => 'FF000000'],
  65. ],
  66. 'top' => [
  67. 'borderStyle' => Border::BORDER_THIN,
  68. 'color' => ['argb' => 'FF000000'],
  69. ],
  70. 'bottom' => [
  71. 'borderStyle' => Border::BORDER_THIN,
  72. 'color' => ['argb' => 'FF000000'],
  73. ]
  74. ],
  75. ];
  76. public static $bordesPrimario = [
  77. 'borders' => [
  78. 'left' => [
  79. 'borderStyle' => Border::BORDER_THIN,
  80. 'color' => ['argb' => 'FF750d44'],
  81. ],
  82. 'right' => [
  83. 'borderStyle' => Border::BORDER_THIN,
  84. 'color' => ['argb' => 'FF750d44'],
  85. ],
  86. 'top' => [
  87. 'borderStyle' => Border::BORDER_THIN,
  88. 'color' => ['argb' => 'FF750d44'],
  89. ],
  90. 'bottom' => [
  91. 'borderStyle' => Border::BORDER_THIN,
  92. 'color' => ['argb' => 'FF750d44'],
  93. ]
  94. ],
  95. ];
  96. public static $bordesWhite = [
  97. 'borders' => [
  98. 'left' => [
  99. 'borderStyle' => Border::BORDER_THIN,
  100. 'color' => ['argb' => 'FFFFFFFF'],
  101. ],
  102. 'right' => [
  103. 'borderStyle' => Border::BORDER_THIN,
  104. 'color' => ['argb' => 'FFFFFFFF'],
  105. ],
  106. 'top' => [
  107. 'borderStyle' => Border::BORDER_THIN,
  108. 'color' => ['argb' => 'FFFFFFFF'],
  109. ],
  110. 'bottom' => [
  111. 'borderStyle' => Border::BORDER_THIN,
  112. 'color' => ['argb' => 'FFFFFFFF'],
  113. ]
  114. ],
  115. ];
  116. public static $bordesGray = [
  117. 'borders' => [
  118. 'left' => [
  119. 'borderStyle' => Border::BORDER_THIN,
  120. 'color' => ['argb' => 'FFE2E2E2'],
  121. ],
  122. 'right' => [
  123. 'borderStyle' => Border::BORDER_THIN,
  124. 'color' => ['argb' => 'FFE2E2E2'],
  125. ],
  126. 'top' => [
  127. 'borderStyle' => Border::BORDER_THIN,
  128. 'color' => ['argb' => 'FFE2E2E2'],
  129. ],
  130. 'bottom' => [
  131. 'borderStyle' => Border::BORDER_THIN,
  132. 'color' => ['argb' => 'FFE2E2E2'],
  133. ]
  134. ],
  135. ];
  136. public static $estiloEncabezado = [
  137. 'font' => [
  138. 'bold' => true,
  139. 'size' => 9
  140. ],
  141. 'alignment' => [
  142. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  143. 'vertical' => Alignment::VERTICAL_CENTER,
  144. ],
  145. 'fill' => [
  146. 'fillType' => Fill::FILL_SOLID,
  147. 'startColor' => ['argb' => self::COLOR_GRIS]
  148. ],
  149. 'font' => [
  150. 'bold' => true,
  151. 'color' => ['argb' => "AAFFFFFF"],
  152. ],
  153. 'borders' => [
  154. 'left' => [
  155. 'borderStyle' => Border::BORDER_THICK,
  156. 'color' => ['argb' => 'FFFFFFFF'],
  157. ],
  158. 'right' => [
  159. 'borderStyle' => Border::BORDER_THICK,
  160. 'color' => ['argb' => 'FFFFFFFF'],
  161. ]
  162. ],
  163. ];
  164. public static $estiloTitulo = [
  165. 'font' => [
  166. 'bold' => true,
  167. 'size' => 13,
  168. 'color' => ['argb' => self::COLOR_AZUL],
  169. ],
  170. 'alignment' => [
  171. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  172. 'vertical' => Alignment::VERTICAL_CENTER,
  173. ],
  174. ];
  175. public static $CELDA_VERDE = [
  176. 'alignment' => [
  177. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  178. ],
  179. 'fill' => [
  180. 'fillType' => Fill::FILL_SOLID,
  181. 'startColor' => ['argb' => 'FFD8E4BD']
  182. ],
  183. 'borders' => [
  184. 'left' => [
  185. 'borderStyle' => Border::BORDER_THICK,
  186. 'color' => ['argb' => 'FFFFFFFF'],
  187. ],
  188. 'right' => [
  189. 'borderStyle' => Border::BORDER_THICK,
  190. 'color' => ['argb' => 'FFFFFFFF'],
  191. ],
  192. 'bottom' => [
  193. 'borderStyle' => Border::BORDER_DASHED,
  194. ],
  195. ],
  196. ];
  197. public static $CELDA_AMARILLA = [
  198. 'alignment' => [
  199. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  200. ],
  201. 'fill' => [
  202. 'fillType' => Fill::FILL_SOLID,
  203. 'startColor' => ['argb' => 'FFFFE597']
  204. ],
  205. 'borders' => [
  206. 'left' => [
  207. 'borderStyle' => Border::BORDER_THICK,
  208. 'color' => ['argb' => 'FFFFFFFF'],
  209. ],
  210. 'right' => [
  211. 'borderStyle' => Border::BORDER_THICK,
  212. 'color' => ['argb' => 'FFFFFFFF'],
  213. ],
  214. 'bottom' => [
  215. 'borderStyle' => Border::BORDER_DASHED,
  216. ],
  217. ],
  218. ];
  219. public static $CELDA_ROJA = [
  220. 'alignment' => [
  221. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  222. ],
  223. 'fill' => [
  224. 'fillType' => Fill::FILL_SOLID,
  225. 'startColor' => ['argb' => 'FFE6B8B7']
  226. ],
  227. 'borders' => [
  228. 'left' => [
  229. 'borderStyle' => Border::BORDER_THICK,
  230. 'color' => ['argb' => 'FFFFFFFF'],
  231. ],
  232. 'right' => [
  233. 'borderStyle' => Border::BORDER_THICK,
  234. 'color' => ['argb' => 'FFFFFFFF'],
  235. ],
  236. 'bottom' => [
  237. 'borderStyle' => Border::BORDER_DASHED,
  238. ],
  239. ],
  240. ];
  241. public static $CELDA_PRIMARIA = [
  242. 'font' => [
  243. 'bold' => true,
  244. 'size' => 16,
  245. 'color' => ['argb' => 'FFFFFFFF'],
  246. ],
  247. 'alignment' => [
  248. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  249. ],
  250. 'fill' => [
  251. 'fillType' => Fill::FILL_SOLID,
  252. 'startColor' => ['argb' => 'FF660034']
  253. ],
  254. ];
  255. public static $CELDA_SECUNDARIA = [
  256. 'font' => [
  257. 'bold' => true,
  258. 'size' => 16,
  259. 'color' => ['argb' => 'FFFFFFFF'],
  260. ],
  261. 'alignment' => [
  262. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  263. ],
  264. 'fill' => [
  265. 'fillType' => Fill::FILL_SOLID,
  266. 'startColor' => ['argb' => 'FF990033']
  267. ],
  268. ];
  269. public static $TITULO_01 = [
  270. 'font' => [
  271. 'bold' => true,
  272. 'size' => 14,
  273. 'color' => ['argb' => 'FFFFFFFF'],
  274. ],
  275. ];
  276. public static $TITULO_02 = [
  277. 'font' => [
  278. 'bold' => true,
  279. 'size' => 14,
  280. 'color' => ['argb' => 'FF000000'],
  281. ],
  282. ];
  283. public static $DESCR_01 = [
  284. 'alignment' => [
  285. 'horizontal' => Alignment::HORIZONTAL_LEFT,
  286. ],
  287. 'font' => [
  288. 'bold' => false,
  289. 'size' => 12,
  290. 'color' => ['argb' => 'FF000000'],
  291. ],
  292. ];
  293. public static $CELDA_GRIS_01 = [
  294. 'font' => [
  295. 'bold' => true,
  296. 'size' => 14,
  297. 'color' => ['argb' => 'FFFFFFFF'],
  298. ],
  299. 'fill' => [
  300. 'fillType' => Fill::FILL_SOLID,
  301. 'startColor' => ['argb' => 'FF808080']
  302. ],
  303. ];
  304. public static $CELDA_GRIS_02 = [
  305. 'fill' => [
  306. 'fillType' => Fill::FILL_SOLID,
  307. 'startColor' => ['argb' => 'FFA6A6A6']
  308. ],
  309. ];
  310. public static $CELDA_GRIS_03 = [
  311. 'fill' => [
  312. 'fillType' => Fill::FILL_SOLID,
  313. 'startColor' => ['argb' => 'FFD9D9D9']
  314. ],
  315. ];
  316. /* public function behaviors() {
  317. $behavior = parent::behaviors();
  318. $behavior["authenticator"] = [
  319. "class" => CompositeAuth::className(),
  320. "authMethods" => [
  321. QueryParamAuth::className(),
  322. ]
  323. ];
  324. return $behavior;
  325. } */
  326. public function beforeAction($action) {
  327. parent::beforeAction($action);
  328. $this->app = Yii::$app;
  329. $this->req = $this->app->getRequest();
  330. $this->res = $this->app->getResponse();
  331. $this->spreadsheet = new Spreadsheet();
  332. $this->activeSheet = $this->spreadsheet->getActiveSheet();
  333. return true;
  334. }
  335. public function obtenerHojaDeCalculo() {
  336. return $this->spreadsheet;
  337. }
  338. public function nuevaHoja($indice = null, $titulo = null) {
  339. $this->activeSheet = $this->spreadsheet->createSheet($indice);
  340. if ($titulo !== null) {
  341. $this->activeSheet->setTitle($titulo);
  342. }
  343. }
  344. public function logo($logo, $coordenada = "I1", $nombre = 'Logo', $descripcion = 'Logo', $x = 27, $y = 8, $heigth = 75) {
  345. $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
  346. $drawing->setName($nombre);
  347. $drawing->setDescription($descripcion);
  348. $drawing->setPath($logo);
  349. $drawing->setCoordinates($coordenada);
  350. $drawing->setOffsetX($x);
  351. $drawing->setOffsetY($y);
  352. $drawing->setHeight($heigth);
  353. $drawing->setWorksheet($this->activeSheet);
  354. }
  355. public function titulo($inicio = 1, $titulo, $columnaInicio = "A", $columnaFinal = "E") {
  356. if ($inicio < $this->renglonActual) {
  357. $inicio = $this->renglonActual + 1;
  358. }
  359. $renglones = [
  360. "{$columnaInicio}{$inicio}" => [
  361. "valor" => "{$titulo}",
  362. "combinar" => "{$columnaFinal}{$inicio}",
  363. "estilo" => [
  364. 'alignment' => [
  365. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  366. ],
  367. 'font' => [
  368. 'bold' => true,
  369. 'color' => ['argb' => self::COLOR_AZUL],
  370. // 'size' => 15,
  371. // 'name' => 'Verdana'
  372. ]
  373. ]
  374. ]
  375. ];
  376. foreach ($renglones as $coordenada => $valor) {
  377. $this->agregarCelda($coordenada, $valor);
  378. }
  379. $this->renglonActual = $inicio + 1;
  380. return $this;
  381. }
  382. public function agregarRenglones($renglones) {
  383. foreach ($renglones as $coordenada => $valor) {
  384. $this->agregarCelda($coordenada, $valor);
  385. }
  386. return $this;
  387. }
  388. public function agregarEncabezado($titulos) {
  389. if (count($titulos) <= 0) {
  390. return $this;
  391. }
  392. foreach ($titulos as $k => $v) {
  393. $this->agregarCelda($k, [
  394. "valor" => "$v",
  395. "estilo" => self::$estiloEncabezado
  396. ]);
  397. }
  398. return $this;
  399. }
  400. public function agregarCelda($coordenada, $valor) {
  401. if (isset($valor["valor"])) {
  402. $this->activeSheet
  403. ->setCellValue($coordenada, $valor["valor"]);
  404. if (isset($valor["combinar"])) {
  405. $coordenada = "{$coordenada}:{$valor["combinar"]}";
  406. $this->activeSheet
  407. ->mergeCells($coordenada);
  408. }
  409. }
  410. if (isset($valor["estilo"])) {
  411. $this->activeSheet
  412. ->getStyle($coordenada)
  413. ->applyFromArray($valor["estilo"]);
  414. }
  415. if (isset($valor["formato"])) {
  416. $this->activeSheet
  417. ->getStyle($coordenada)
  418. ->getNumberFormat()
  419. ->setFormatCode($valor["formato"]);
  420. }
  421. if (isset($valor["wrap"])) {
  422. $this->activeSheet
  423. ->getStyle($coordenada)
  424. ->getAlignment()
  425. ->setWrapText($valor["wrap"] || false);
  426. }
  427. }
  428. # Después de agregar toda la información elegir el ancho de las columnas
  429. public function anchoColumnas($columnas) {
  430. foreach ($columnas as $columna => $c) {
  431. if (isset($c["auto"]) && $c["auto"]) {
  432. $this->activeSheet
  433. ->getColumnDimension($columna)
  434. ->setAutoSize(true);
  435. } elseif (isset($c["ancho"]) && $c["ancho"] > 0) {
  436. $this->activeSheet->getColumnDimension($columna)->setWidth($c["ancho"]);
  437. }
  438. }
  439. return $this;
  440. }
  441. # Generar el archivo excel
  442. public function generar($filename = null) {
  443. $writer = IOFactory::createWriter($this->spreadsheet, 'Xlsx');
  444. try {
  445. ob_start();
  446. $writer->save("php://output");
  447. $documento = ob_get_contents();
  448. ob_clean();
  449. return $documento;
  450. } catch (\Exception $exception) {
  451. return null;
  452. }
  453. }
  454. # Descarga el archivo en formato excel o pdf
  455. public function crear($hojaCalculo, $archivo = null, $tipo = self::TIPO_EXCEL) {
  456. if (!$tipo) {
  457. $tipo = \Yii::$app->getRequest()->get("tipo", "excel");
  458. }
  459. $tipo_writer = 'Xlsx';
  460. $extension = '.xlsx';
  461. if ($tipo === self::TIPO_PDF) {
  462. $tipo_writer = 'Mpdf';
  463. $extension = '.pdf';
  464. }
  465. $writer = IOFactory::createWriter($hojaCalculo, $tipo_writer);
  466. $archivo .= $extension;
  467. try {
  468. ob_start();
  469. $writer->save("php://output");
  470. $content = ob_get_contents();
  471. ob_clean();
  472. \Yii::$app->getResponse()->sendContentAsFile($content, $archivo);
  473. } catch (\Exception $e) {
  474. echo $e->getMessage();
  475. }
  476. }
  477. }