123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524 |
- <?php
- namespace excel\web;
- use common\rest\JsonController;
- use yii\web\Controller as WebController;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use PhpOffice\PhpSpreadsheet\Style\Alignment;
- use PhpOffice\PhpSpreadsheet\Style\Border;
- use PhpOffice\PhpSpreadsheet\Style\Fill;
- use yii\filters\auth\CompositeAuth;
- use yii\filters\auth\QueryParamAuth;
- use yii\web\Response;
- use Yii;
- class Controller extends WebController {
- public $app = null;
- public $req = null;
- public $res = null;
- const MIMETYPE_EXCEL = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
- const TIPO_EXCEL = "excel";
- const TIPO_PDF = "pdf";
- const COLOR_NEGRO = "FF000000";
- const COLOR_AZUL = "FF1A95E8";
- const COLOR_GRIS = "FF959595";
- public $renglonActual = 1;
- public $renglonPrevio = 0;
- protected $spreadsheet;
- protected $activeSheet;
- public static $estiloCeldaNormal = [
- 'alignment' => [
- 'horizontal' => Alignment::HORIZONTAL_LEFT,
- ],
- ];
- public static $estiloCeldaCentrada = [
- 'font' => [
- 'bold' => true,
- 'size' => 16,
- 'color' => ['argb' => 'FF000000'],
- ],
- 'alignment' => [
- 'horizontal' => Alignment::HORIZONTAL_CENTER,
- 'vertical' => Alignment::VERTICAL_CENTER,
- ],
- ];
- public static $estiloCeldaIzquierda = [
- 'font' => [
- 'bold' => true,
- 'size' => 16,
- 'color' => ['argb' => 'FF000000'],
- ],
- 'alignment' => [
- 'horizontal' => Alignment::HORIZONTAL_LEFT,
- 'vertical' => Alignment::VERTICAL_CENTER,
- ],
- ];
- public static $bordes = [
- 'borders' => [
- 'left' => [
- 'borderStyle' => Border::BORDER_THIN,
- 'color' => ['argb' => 'FF000000'],
- ],
- 'right' => [
- 'borderStyle' => Border::BORDER_THIN,
- 'color' => ['argb' => 'FF000000'],
- ],
- 'top' => [
- 'borderStyle' => Border::BORDER_THIN,
- 'color' => ['argb' => 'FF000000'],
- ],
- 'bottom' => [
- 'borderStyle' => Border::BORDER_THIN,
- 'color' => ['argb' => 'FF000000'],
- ]
- ],
- ];
- public static $bordesPrimario = [
- 'borders' => [
- 'left' => [
- 'borderStyle' => Border::BORDER_THIN,
- 'color' => ['argb' => 'FF750d44'],
- ],
- 'right' => [
- 'borderStyle' => Border::BORDER_THIN,
- 'color' => ['argb' => 'FF750d44'],
- ],
- 'top' => [
- 'borderStyle' => Border::BORDER_THIN,
- 'color' => ['argb' => 'FF750d44'],
- ],
- 'bottom' => [
- 'borderStyle' => Border::BORDER_THIN,
- 'color' => ['argb' => 'FF750d44'],
- ]
- ],
- ];
- public static $bordesWhite = [
- 'borders' => [
- 'left' => [
- 'borderStyle' => Border::BORDER_THIN,
- 'color' => ['argb' => 'FFFFFFFF'],
- ],
- 'right' => [
- 'borderStyle' => Border::BORDER_THIN,
- 'color' => ['argb' => 'FFFFFFFF'],
- ],
- 'top' => [
- 'borderStyle' => Border::BORDER_THIN,
- 'color' => ['argb' => 'FFFFFFFF'],
- ],
- 'bottom' => [
- 'borderStyle' => Border::BORDER_THIN,
- 'color' => ['argb' => 'FFFFFFFF'],
- ]
- ],
- ];
- public static $bordesGray = [
- 'borders' => [
- 'left' => [
- 'borderStyle' => Border::BORDER_THIN,
- 'color' => ['argb' => 'FFE2E2E2'],
- ],
- 'right' => [
- 'borderStyle' => Border::BORDER_THIN,
- 'color' => ['argb' => 'FFE2E2E2'],
- ],
- 'top' => [
- 'borderStyle' => Border::BORDER_THIN,
- 'color' => ['argb' => 'FFE2E2E2'],
- ],
- 'bottom' => [
- 'borderStyle' => Border::BORDER_THIN,
- 'color' => ['argb' => 'FFE2E2E2'],
- ]
- ],
- ];
- public static $estiloEncabezado = [
- 'font' => [
- 'bold' => true,
- 'size' => 9
- ],
- 'alignment' => [
- 'horizontal' => Alignment::HORIZONTAL_CENTER,
- 'vertical' => Alignment::VERTICAL_CENTER,
- ],
- 'fill' => [
- 'fillType' => Fill::FILL_SOLID,
- 'startColor' => ['argb' => self::COLOR_GRIS]
- ],
- 'font' => [
- 'bold' => true,
- 'color' => ['argb' => "AAFFFFFF"],
- ],
- 'borders' => [
- 'left' => [
- 'borderStyle' => Border::BORDER_THICK,
- 'color' => ['argb' => 'FFFFFFFF'],
- ],
- 'right' => [
- 'borderStyle' => Border::BORDER_THICK,
- 'color' => ['argb' => 'FFFFFFFF'],
- ]
- ],
- ];
- public static $estiloTitulo = [
- 'font' => [
- 'bold' => true,
- 'size' => 13,
- 'color' => ['argb' => self::COLOR_AZUL],
- ],
- 'alignment' => [
- 'horizontal' => Alignment::HORIZONTAL_CENTER,
- 'vertical' => Alignment::VERTICAL_CENTER,
- ],
- ];
- public static $CELDA_VERDE = [
- 'alignment' => [
- 'horizontal' => Alignment::HORIZONTAL_CENTER,
- ],
- 'fill' => [
- 'fillType' => Fill::FILL_SOLID,
- 'startColor' => ['argb' => 'FFD8E4BD']
- ],
- 'borders' => [
- 'left' => [
- 'borderStyle' => Border::BORDER_THICK,
- 'color' => ['argb' => 'FFFFFFFF'],
- ],
- 'right' => [
- 'borderStyle' => Border::BORDER_THICK,
- 'color' => ['argb' => 'FFFFFFFF'],
- ],
- 'bottom' => [
- 'borderStyle' => Border::BORDER_DASHED,
- ],
- ],
- ];
- public static $CELDA_AMARILLA = [
- 'alignment' => [
- 'horizontal' => Alignment::HORIZONTAL_CENTER,
- ],
- 'fill' => [
- 'fillType' => Fill::FILL_SOLID,
- 'startColor' => ['argb' => 'FFFFE597']
- ],
- 'borders' => [
- 'left' => [
- 'borderStyle' => Border::BORDER_THICK,
- 'color' => ['argb' => 'FFFFFFFF'],
- ],
- 'right' => [
- 'borderStyle' => Border::BORDER_THICK,
- 'color' => ['argb' => 'FFFFFFFF'],
- ],
- 'bottom' => [
- 'borderStyle' => Border::BORDER_DASHED,
- ],
- ],
- ];
- public static $CELDA_ROJA = [
- 'alignment' => [
- 'horizontal' => Alignment::HORIZONTAL_CENTER,
- ],
- 'fill' => [
- 'fillType' => Fill::FILL_SOLID,
- 'startColor' => ['argb' => 'FFE6B8B7']
- ],
- 'borders' => [
- 'left' => [
- 'borderStyle' => Border::BORDER_THICK,
- 'color' => ['argb' => 'FFFFFFFF'],
- ],
- 'right' => [
- 'borderStyle' => Border::BORDER_THICK,
- 'color' => ['argb' => 'FFFFFFFF'],
- ],
- 'bottom' => [
- 'borderStyle' => Border::BORDER_DASHED,
- ],
- ],
- ];
- public static $CELDA_PRIMARIA = [
- 'font' => [
- 'bold' => true,
- 'size' => 16,
- 'color' => ['argb' => 'FFFFFFFF'],
- ],
- 'alignment' => [
- 'horizontal' => Alignment::HORIZONTAL_CENTER,
- ],
- 'fill' => [
- 'fillType' => Fill::FILL_SOLID,
- 'startColor' => ['argb' => 'FF660034']
- ],
- ];
- public static $CELDA_SECUNDARIA = [
- 'font' => [
- 'bold' => true,
- 'size' => 16,
- 'color' => ['argb' => 'FFFFFFFF'],
- ],
- 'alignment' => [
- 'horizontal' => Alignment::HORIZONTAL_CENTER,
- ],
- 'fill' => [
- 'fillType' => Fill::FILL_SOLID,
- 'startColor' => ['argb' => 'FF990033']
- ],
- ];
- public static $TITULO_01 = [
- 'font' => [
- 'bold' => true,
- 'size' => 14,
- 'color' => ['argb' => 'FFFFFFFF'],
- ],
- ];
- public static $TITULO_02 = [
- 'font' => [
- 'bold' => true,
- 'size' => 14,
- 'color' => ['argb' => 'FF000000'],
- ],
- ];
- public static $DESCR_01 = [
- 'alignment' => [
- 'horizontal' => Alignment::HORIZONTAL_LEFT,
- ],
- 'font' => [
- 'bold' => false,
- 'size' => 12,
- 'color' => ['argb' => 'FF000000'],
- ],
- ];
- public static $CELDA_GRIS_01 = [
- 'font' => [
- 'bold' => true,
- 'size' => 14,
- 'color' => ['argb' => 'FFFFFFFF'],
- ],
- 'fill' => [
- 'fillType' => Fill::FILL_SOLID,
- 'startColor' => ['argb' => 'FF808080']
- ],
- ];
- public static $CELDA_GRIS_02 = [
- 'fill' => [
- 'fillType' => Fill::FILL_SOLID,
- 'startColor' => ['argb' => 'FFA6A6A6']
- ],
- ];
- public static $CELDA_GRIS_03 = [
- 'fill' => [
- 'fillType' => Fill::FILL_SOLID,
- 'startColor' => ['argb' => 'FFD9D9D9']
- ],
- ];
- /* public function behaviors() {
- $behavior = parent::behaviors();
- $behavior["authenticator"] = [
- "class" => CompositeAuth::className(),
- "authMethods" => [
- QueryParamAuth::className(),
- ]
- ];
- return $behavior;
- } */
- public function beforeAction($action) {
- parent::beforeAction($action);
- $this->app = Yii::$app;
- $this->req = $this->app->getRequest();
- $this->res = $this->app->getResponse();
- $this->spreadsheet = new Spreadsheet();
- $this->activeSheet = $this->spreadsheet->getActiveSheet();
- return true;
- }
- public function obtenerHojaDeCalculo() {
- return $this->spreadsheet;
- }
- public function nuevaHoja($indice = null, $titulo = null) {
- $this->activeSheet = $this->spreadsheet->createSheet($indice);
- if ($titulo !== null) {
- $this->activeSheet->setTitle($titulo);
- }
- }
- public function logo($logo, $coordenada = "I1", $nombre = 'Logo', $descripcion = 'Logo', $x = 27, $y = 8, $heigth = 75) {
- $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
- $drawing->setName($nombre);
- $drawing->setDescription($descripcion);
- $drawing->setPath($logo);
- $drawing->setCoordinates($coordenada);
- $drawing->setOffsetX($x);
- $drawing->setOffsetY($y);
- $drawing->setHeight($heigth);
- $drawing->setWorksheet($this->activeSheet);
- }
- public function titulo($inicio = 1, $titulo, $columnaInicio = "A", $columnaFinal = "E") {
- if ($inicio < $this->renglonActual) {
- $inicio = $this->renglonActual + 1;
- }
- $renglones = [
- "{$columnaInicio}{$inicio}" => [
- "valor" => "{$titulo}",
- "combinar" => "{$columnaFinal}{$inicio}",
- "estilo" => [
- 'alignment' => [
- 'horizontal' => Alignment::HORIZONTAL_CENTER,
- ],
- 'font' => [
- 'bold' => true,
- 'color' => ['argb' => self::COLOR_AZUL],
- // 'size' => 15,
- // 'name' => 'Verdana'
- ]
- ]
- ]
- ];
- foreach ($renglones as $coordenada => $valor) {
- $this->agregarCelda($coordenada, $valor);
- }
- $this->renglonActual = $inicio + 1;
- return $this;
- }
- public function agregarRenglones($renglones) {
- foreach ($renglones as $coordenada => $valor) {
- $this->agregarCelda($coordenada, $valor);
- }
- return $this;
- }
- public function agregarEncabezado($titulos) {
- if (count($titulos) <= 0) {
- return $this;
- }
- foreach ($titulos as $k => $v) {
- $this->agregarCelda($k, [
- "valor" => "$v",
- "estilo" => self::$estiloEncabezado
- ]);
- }
- return $this;
- }
- public function agregarCelda($coordenada, $valor) {
- if (isset($valor["valor"])) {
- $this->activeSheet
- ->setCellValue($coordenada, $valor["valor"]);
- if (isset($valor["combinar"])) {
- $coordenada = "{$coordenada}:{$valor["combinar"]}";
- $this->activeSheet
- ->mergeCells($coordenada);
- }
- }
- if (isset($valor["estilo"])) {
- $this->activeSheet
- ->getStyle($coordenada)
- ->applyFromArray($valor["estilo"]);
- }
- if (isset($valor["formato"])) {
- $this->activeSheet
- ->getStyle($coordenada)
- ->getNumberFormat()
- ->setFormatCode($valor["formato"]);
- }
- if (isset($valor["wrap"])) {
- $this->activeSheet
- ->getStyle($coordenada)
- ->getAlignment()
- ->setWrapText($valor["wrap"] || false);
- }
- }
- # Después de agregar toda la información elegir el ancho de las columnas
- public function anchoColumnas($columnas) {
- foreach ($columnas as $columna => $c) {
- if (isset($c["auto"]) && $c["auto"]) {
- $this->activeSheet
- ->getColumnDimension($columna)
- ->setAutoSize(true);
- } elseif (isset($c["ancho"]) && $c["ancho"] > 0) {
- $this->activeSheet->getColumnDimension($columna)->setWidth($c["ancho"]);
- }
- }
- return $this;
- }
- # Generar el archivo excel
- public function generar($filename = null) {
- $writer = IOFactory::createWriter($this->spreadsheet, 'Xlsx');
- try {
- ob_start();
- $writer->save("php://output");
- $documento = ob_get_contents();
- ob_clean();
- return $documento;
- } catch (\Exception $exception) {
- return null;
- }
- }
- # Descarga el archivo en formato excel o pdf
- public function crear($hojaCalculo, $archivo = null, $tipo = self::TIPO_EXCEL) {
- if (!$tipo) {
- $tipo = \Yii::$app->getRequest()->get("tipo", "excel");
- }
- $tipo_writer = 'Xlsx';
- $extension = '.xlsx';
- if ($tipo === self::TIPO_PDF) {
- $tipo_writer = 'Mpdf';
- $extension = '.pdf';
- }
- $writer = IOFactory::createWriter($hojaCalculo, $tipo_writer);
- $archivo .= $extension;
- try {
- ob_start();
- $writer->save("php://output");
- $content = ob_get_contents();
- ob_clean();
- \Yii::$app->getResponse()->sendContentAsFile($content, $archivo);
- } catch (\Exception $e) {
- echo $e->getMessage();
- }
- }
- }
|