<?php

namespace Shuchkin;

class SimpleXLSXGen
{
    public $curSheet;

    protected $defaultFont;

    protected $defaultFontSize;

    protected $rtl;

    protected $sheets;

    protected $template;

    protected $NF;

    protected $NF_KEYS;

    protected $XF;

    protected $XF_KEYS;

    protected $BR_STYLE;

    protected $SI;

    protected $SI_KEYS;

    protected $title;

    protected $subject;

    protected $author;

    protected $company;

    protected $manager;

    protected $description;

    protected $application;

    protected $keywords;

    protected $category;

    protected $language;

    protected $lastModifiedBy;

    public const N_NORMAL = 0;

    public const N_INT = 1;

    public const N_DEC = 2;

    public const N_PERCENT_INT = 9;

    public const N_PRECENT_DEC = 10;

    public const N_DATE = 14;

    public const N_TIME = 20;

    public const N_RUB = 164;

    public const N_DOLLAR = 165;

    public const N_EURO = 166;

    public const N_DATETIME = 22;

    public const N_CUSTOM = 197;

    public const F_NORMAL = 0;

    public const F_HYPERLINK = 1;

    public const F_BOLD = 2;

    public const F_ITALIC = 4;

    public const F_UNDERLINE = 8;

    public const F_STRIKE = 16;

    public const F_COLOR = 32;

    public const FL_NONE = 0;

    public const FL_SOLID = 1;

    public const FL_MEDIUM_GRAY = 2;

    public const FL_DARK_GRAY = 4;

    public const FL_LIGHT_GRAY = 8;

    public const FL_GRAY_125 = 16;

    public const FL_COLOR = 32;

    public const A_DEFAULT = 0;

    public const A_LEFT = 1;

    public const A_RIGHT = 2;

    public const A_CENTER = 4;

    public const A_TOP = 8;

    public const A_MIDDLE = 16;

    public const A_BOTTOM = 32;

    public const A_WRAPTEXT = 64;

    public const A_ROTATION_90 = 128;

    public const B_NONE = 0;

    public const B_THIN = 1;

    public const B_MEDIUM = 2;

    public const B_DASHED = 3;

    public const B_DOTTED = 4;

    public const B_THICK = 5;

    public const B_DOUBLE = 6;

    public const B_HAIR = 7;

    public const B_MEDIUM_DASHED = 8;

    public const B_DASH_DOT = 9;

    public const B_MEDIUM_DASH_DOT = 10;

    public const B_DASH_DOT_DOT = 11;

    public const B_MEDIUM_DASH_DOT_DOT = 12;

    public const B_SLANT_DASH_DOT = 13;

    public function __construct()
    {
        $this->subject = '';
        $this->title = '';
        $this->author = '';
        $this->company = '';
        $this->manager = '';
        $this->description = '';
        $this->keywords = '';
        $this->category = '';
        $this->language = 'en-US';
        $this->lastModifiedBy = '';
        $this->application = __CLASS__;
        $this->curSheet = -1;
        $this->defaultFont = 'Calibri';
        $this->defaultFontSize = 10;
        $this->rtl = false;
        $this->sheets = [];
        $this->SI = [];
        $this->SI_KEYS = [];
        $this->NF = [self::N_RUB => '#,##0.00\ "₽"', self::N_DOLLAR => '[$$-1]#,##0.00', self::N_EURO => '#,##0.00\ [$€-1]'];
        $this->NF_KEYS = array_flip($this->NF);
        $this->BR_STYLE = [self::B_NONE => 'none', self::B_THIN => 'thin', self::B_MEDIUM => 'medium', self::B_DASHED => 'dashed', self::B_DOTTED => 'dotted', self::B_THICK => 'thick', self::B_DOUBLE => 'double', self::B_HAIR => 'hair', self::B_MEDIUM_DASHED => 'mediumDashed', self::B_DASH_DOT => 'dashDot', self::B_MEDIUM_DASH_DOT => 'mediumDashDot', self::B_DASH_DOT_DOT => 'dashDotDot', self::B_MEDIUM_DASH_DOT_DOT => 'mediumDashDotDot', self::B_SLANT_DASH_DOT => 'slantDashDot'];
        $this->XF = [[self::N_NORMAL, self::A_DEFAULT, self::F_NORMAL, self::FL_NONE, 0, 0, '', 0], [self::N_NORMAL, self::A_DEFAULT, self::F_NORMAL, self::FL_GRAY_125, 0, 0, '', 0], ];
        $this->XF_KEYS[implode('-', $this->XF[0])] = 0;
        $this->XF_KEYS[implode('-', $this->XF[1])] = 1;
        $this->template = ['_rels/.rels' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\r\n" .'<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'."\r\n" .'<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>'."\r\n" .'<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>'."\r\n" .'<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>'."\r\n" .'</Relationships>', 'docProps/app.xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\r\n" .'<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties">'."\r\n" .'<TotalTime>0</TotalTime>'."\r\n" .'<Application>{APP}</Application>'."\r\n" .'<Company>{COMPANY}</Company>'."\r\n" .'<Manager>{MANAGER}</Manager>'."\r\n" .'</Properties>', 'docProps/core.xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\r\n" .'<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'."\r\n" .'<dcterms:created xsi:type="dcterms:W3CDTF">{DATE}</dcterms:created>'."\r\n" .'<dc:title>{TITLE}</dc:title>'."\r\n" .'<dc:subject>{SUBJECT}</dc:subject>'."\r\n" .'<dc:creator>{AUTHOR}</dc:creator>'."\r\n" .'<cp:lastModifiedBy>{LAST_MODIFY_BY}</cp:lastModifiedBy>'."\r\n" .'<cp:keywords>{KEYWORD}</cp:keywords>'."\r\n" .'<dc:description>{DESCRIPTION}</dc:description>'."\r\n" .'<cp:category>{CATEGORY}</cp:category>'."\r\n" .'<dc:language>{LANGUAGE}</dc:language>'."\r\n" .'<dcterms:modified xsi:type="dcterms:W3CDTF">{DATE}</dcterms:modified>'."\r\n" .'<cp:revision>1</cp:revision>'."\r\n" .'</cp:coreProperties>', 'xl/_rels/workbook.xml.rels' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\r\n" .'<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">' ."\r\n{RELS}\r\n</Relationships>", 'xl/worksheets/sheet1.xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\r\n" . '<worksheet' . "\r\n" . ' xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"' . "\r\n" . ' xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"'."\r\n" . ' xmlns:mx="http://schemas.microsoft.com/office/mac/excel/2008/main"' . "\r\n" . ' xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"' . "\r\n" . ' xmlns:mv="urn:schemas-microsoft-com:mac:vml"' . "\r\n" . ' xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"' . "\r\n" . ' xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main"' . "\r\n" . ' xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac"' . "\r\n" . ' xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main">' . "\r\n" .'<dimension ref="{REF}"/>'."\r\n" ."{SHEETVIEWS}\r\n{COLS}\r\n{ROWS}\r\n{AUTOFILTER}{MERGECELLS}{HYPERLINKS}{VML}</worksheet>", 'xl/worksheets/_rels/sheet1.xml.rels' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\r\n" .'<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">{RELS}</Relationships>', 'xl/sharedStrings.xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\r\n" .'<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="{CNT}" uniqueCount="{CNT}">{STRINGS}</sst>', 'xl/styles.xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\r\n" .'<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">' ."\r\n{NUMFMTS}\r\n{FONTS}\r\n{FILLS}\r\n{BORDERS}\r\n" .'<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0" /></cellStyleXfs>' ."\r\n{XF}\r\n" .'<cellStyles count="1"><cellStyle name="Normal" xfId="0" builtinId="0"/></cellStyles></styleSheet>', 'xl/workbook.xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\r\n" .'<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">'."\r\n" .'<fileVersion appName="{APP}"/><sheets>{SHEETS}</sheets></workbook>', 'xl/comments1.xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\r\n" . '<comments' . "\r\n" . ' xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"' . "\r\n" . ' xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">' . "\r\n" . '<authors>{AUTHORS}</authors>' . "\r\n" . '<commentList>{COMMENTS}</commentList>' . "\r\n" . '</comments>', 'xl/drawings/vmlDrawing1.vml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . "\r\n" . '<xml xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:oa="urn:schemas-microsoft-com:office:activation" xmlns:x="urn:schemas-microsoft-com:office:excel">' . "\r\n" .'<o:shapelayout v:ext="edit"><o:idmap data="1" v:ext="edit"/></o:shapelayout>' . "\r\n" .'<v:shapetype id="_x0000_t202" coordsize="21600,21600" o:spt="202.0" path="m,l,21600r21600,l21600,xe"><v:stroke joinstyle="miter"/><v:path o:connecttype="rect" gradientshapeok="t"/></v:shapetype>{SHAPES}</xml>', '[Content_Types].xml' => '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\r\n" .'<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">'."\r\n" .'<Default Extension="vml" ContentType="application/vnd.openxmlformats-officedocument.vmlDrawing"/>' . "\r\n" .'<Default Extension="xml" ContentType="application/xml"/>' . "\r\n" .'<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>' . "\r\n" .'<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>'."\r\n" .'<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>'."\r\n" .'<Override PartName="/xl/sharedStrings.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"/>'."\r\n" .'<Override PartName="/xl/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"/>
<Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/>' ."\r\n{TYPES}</Types>", ];
    }

    public static function create($title = null)
    {
        $xlsx = new static();

        if ($title) {
            $xlsx->setTitle($title);
        }

return $xlsx;
    }

    public static function fromArray(array $rows, $sheetName = null)
    {
        $xlsx = new static();
        $xlsx->addSheet($rows, $sheetName);

        if ($sheetName) {
            $xlsx->setTitle($sheetName);
        }

return $xlsx;
    }

    public function addSheet(array $rows, $name = null)
    {
        $this->curSheet++;

        if ($name === null) {
            $name = ($this->title ? mb_substr($this->title, 0, 31) : 'Sheet') . ($this->curSheet + 1);
        } else {
            $name = mb_substr($name, 0, 31);
            $names = [];

            foreach ($this->sheets as $sh) {
                $names[mb_strtoupper($sh['name'])] = 1;
            } for ($i = 0; $i < 100; $i++) {
                $postfix = ' (' . $i . ')';
                $new_name = ($i === 0) ? $name : $name . $postfix;

                if (mb_strlen($new_name) > 31) {
                    $new_name = mb_substr($name, 0, 31 - mb_strlen($postfix)) . $postfix;
                } $NEW_NAME = mb_strtoupper($new_name);

                if (!isset($names[$NEW_NAME])) {
                    $name = $new_name;
                    break;
                }
            }
        } $this->sheets[$this->curSheet] = ['name' => $name, 'relidx' => 0, 'hyperlinks' => [], 'comments' => [], 'mergecells' => [], 'colwidth' => [], 'autofilter' => '', 'frozen' => ''];

        if (isset($rows[0]) && is_array($rows[0])) {
            $this->sheets[$this->curSheet]['rows'] = $rows;
        } else {
            $this->sheets[$this->curSheet]['rows'] = [];
        }

return $this;
    }

    public function __toString()
    {
        $fh = fopen('php://memory', 'wb');

        if (!$fh) {
            return '';
        }

if (!$this->_write($fh)) {
            fclose($fh);

            return '';
        } $size = ftell($fh);
        fseek($fh, 0);

        return (string)fread($fh, $size);
    }

    public function save()
    {
        return $this->saveAs(($this->title ?: gmdate('YmdHi')) . '.xlsx');
    }

    public function saveAs($filename)
    {
        $fh = fopen(str_replace(["\0","\r","\n","\t",'"'], '', $filename), 'wb');

        if (!$fh) {
            return false;
        }

if (!$this->_write($fh)) {
            fclose($fh);

            return false;
        } fclose($fh);

        return true;
    }

    public function download()
    {
        return $this->downloadAs(($this->title ?: gmdate('YmdHi')) . '.xlsx');
    }

    public function downloadAs($filename)
    {
        $fh = fopen('php://memory', 'wb');

        if (!$fh) {
            return false;
        }

if (!$this->_write($fh)) {
            fclose($fh);

            return false;
        } $size = ftell($fh);
        header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment; filename="' . str_replace(["\0","\r","\n","\t",'"'], '', $filename) . '"');
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s \G\M\T', time()));
        header('Content-Length: ' . $size);

        while (ob_get_level()) {
            ob_end_clean();
        } fseek($fh, 0);

        if (function_exists('fpassthru')) {
            fpassthru($fh);
        } else {
            echo stream_get_contents($fh);
        } fclose($fh);

        return true;
    }

    protected function _write($fh)
    {
        $dirSignatureE = "\x50\x4b\x05\x06";
        $zipComments = 'Generated by ' . __CLASS__ . ' PHP class, thanks sergey.shuchkin@gmail.com';

        if (!$fh) {
            return false;
        } $cdrec = '';
        $entries = 0;
        $cnt_sheets = count($this->sheets);

        if ($cnt_sheets === 0) {
            $this->addSheet([], 'No data');
            $cnt_sheets = 1;
        }

foreach ($this->template as $cfilename => $template) {
            if ($cfilename === 'xl/_rels/workbook.xml.rels') {
                $s = '';
                for ($i = 0; $i < $cnt_sheets; $i++) {
                    $s .= '<Relationship Id="rId' . ($i + 1) . '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"' . ' Target="worksheets/sheet' . ($i + 1) . ".xml\"/>\r\n";
                } $s .= '<Relationship Id="rId' . ($cnt_sheets + 1) . '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>' . "\r\n";
                $s .= '<Relationship Id="rId' . ($cnt_sheets + 2) . '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings" Target="sharedStrings.xml"/>';
                $template = str_replace('{RELS}', $s, $template);
                $this->_writeEntry($fh, $cdrec, $cfilename, $template);
                $entries++;
            } elseif ($cfilename === 'xl/workbook.xml') {
                $s = '';

                foreach ($this->sheets as $k => $v) {
                    $s .= '<sheet name="' . self::esc($v['name']) . '" sheetId="' . ($k + 1) . '" r:id="rId' . ($k + 1) . '"/>';
                } $search = ['{SHEETS}', '{APP}'];
                $replace = [$s, self::esc($this->application)];
                $template = str_replace($search, $replace, $template);
                $this->_writeEntry($fh, $cdrec, $cfilename, $template);
                $entries++;
            } elseif ($cfilename === 'docProps/app.xml') {
                $search = ['{APP}', '{COMPANY}', '{MANAGER}'];
                $replace = [self::esc($this->application), self::esc($this->company), self::esc($this->manager)];
                $template = str_replace($search, $replace, $template);
                $this->_writeEntry($fh, $cdrec, $cfilename, $template);
                $entries++;
            } elseif ($cfilename === 'docProps/core.xml') {
                $search = ['{DATE}', '{AUTHOR}', '{TITLE}', '{SUBJECT}', '{KEYWORD}', '{DESCRIPTION}', '{CATEGORY}', '{LANGUAGE}', '{LAST_MODIFY_BY}'];
                $replace = [gmdate('Y-m-d\TH:i:s\Z'), self::esc($this->author), self::esc($this->title), self::esc($this->subject), self::esc($this->keywords), self::esc($this->description), self::esc($this->category), self::esc($this->language), self::esc($this->lastModifiedBy)];
                $template = str_replace($search, $replace, $template);
                $this->_writeEntry($fh, $cdrec, $cfilename, $template);
                $entries++;
            } elseif ($cfilename === 'xl/sharedStrings.xml') {
                $si_cnt = count($this->SI);

                if ($si_cnt) {
                    $si = [];

                    foreach ($this->SI as $s) {
                        $si[] = '<si>' . (preg_match('/^\s|\s$/', $s) ? '<t xml:space="preserve">' . $s . '</t>' : '<t>' . $s . '</t>') . '</si>';
                    } $template = str_replace(['{CNT}', '{STRINGS}'], [$si_cnt, implode("\r\n", $si)], $template);
                    $this->_writeEntry($fh, $cdrec, $cfilename, $template);
                    $entries++;
                }
            } elseif ($cfilename === 'xl/worksheets/sheet1.xml') {
                foreach ($this->sheets as $k => $v) {
                    $filename = 'xl/worksheets/sheet' . ($k + 1) . '.xml';
                    $xml = $this->_sheetToXML($k, $template);
                    $this->_writeEntry($fh, $cdrec, $filename, $xml);
                    $entries++;
                } $xml = null;
            } elseif ($cfilename === 'xl/worksheets/_rels/sheet1.xml.rels') {
                foreach ($this->sheets as $k => $v) {
                    $R = [];

                    if (count($v['comments'])) {
                        $this->sheets[$k]['relidx']++;
                        $R[] = '<Relationship Id="rId' . $this->sheets[$k]['relidx'] . '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="../comments' . ($k + 1) . '.xml"/>';
                        $R[] = '<Relationship Id="rVMLCom" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing" Target="../drawings/vmlDrawing' . ($k + 1) . '.vml"/>';
                    }

foreach ($v['hyperlinks'] as $h) {
                        if ($h['ID']) {
                            $R[] = '<Relationship Id="' . $h['ID'] . '" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="' . self::esc($h['H']) . '" TargetMode="External"/>';
                        }
                    }

if (count($R)) {
                        $filename = 'xl/worksheets/_rels/sheet' . ($k + 1) . '.xml.rels';
                        $xml = str_replace('{RELS}', implode("\r\n", $R), $template);
                        $this->_writeEntry($fh, $cdrec, $filename, $xml);
                        $entries++;
                    }
                } $xml = null;
            } elseif ($cfilename === 'xl/comments1.xml') {
                foreach ($this->sheets as $k => $v) {
                    $C = [];
                    $A = [];

                    foreach ($v['comments'] as $com) {
                        $A[$com['A']] = count($A);
                    }

foreach ($v['comments'] as $com) {
                        $C[] = '<comment ref="' . $com['N'] . '" authorId="' . $A[$com['A']] . '"><text>' . "\r\n" . ($com['A'] ? ('<r><rPr><b/><sz val="10"/><family val="2"/></rPr><t xml:space="preserve">' . $com['A'] . ":\r\n" .'</t></r>' . "\r\n") : '') .'<r><rPr><sz val="10"/><family val="2"/></rPr><t xml:space="preserve">' . self::esc($com['T']) . '</t></r>' . "\r\n" . '</text></comment>';
                    }

if (count($C)) {
                        $filename = 'xl/comments' . ($k + 1) . '.xml';
                        $xml = str_replace(array('{AUTHORS}', '{COMMENTS}'), array('<author>' . implode("</author>\r\n<author>", array_keys($A)) . '</author>', implode("\r\n", $C)), $template);
                        $this->_writeEntry($fh, $cdrec, $filename, $xml);
                        $entries++;
                    }
                } $xml = null;
            } elseif ($cfilename === 'xl/drawings/vmlDrawing1.vml') {
                foreach ($this->sheets as $k => $v) {
                    if (count($v['comments'])) {
                        $S = [];

                        foreach ($v['comments'] as $k2 => $com) {
                            $a = [$com['C'], 15, $com['R'], 10, $com['C'] + 4, 15, $com['R'] + 4, 4];
                            $S[] = '<v:shape id="_x0000_s' . (1025 + $k2) .'"' . ' style="visibility:hidden" fillcolor="#ffffe1" type="#_x0000_t202">' . "\r\n" . '  <v:fill angle="0" color2="#ffffe1"/>' . "\r\n" . '  <v:shadow color="black" obscured="t" on="t"/>' . "\r\n" . '  <v:path o:connecttype="none"/><v:textbox/>' . "\r\n" . '  <x:ClientData ObjectType="Note">' . "\r\n" . '    <x:MoveWithCells/>' . "\r\n" . '    <x:SizeWithCells/>' . "\r\n" . '    <x:Anchor>' . implode(', ', $a) .'</x:Anchor>' . "\r\n" . '    <x:AutoFill>False</x:AutoFill>' . "\r\n" . '    <x:Row>' . ($com['R'] - 1) . '</x:Row>' . "\r\n" . '    <x:Column>' . ($com['C'] - 1) . '</x:Column>' . "\r\n" . '  </x:ClientData>' . "\r\n" . '</v:shape>';
                        } $filename = 'xl/drawings/vmlDrawing' . ($k + 1) . '.vml';
                        $xml = str_replace('{SHAPES}', "\r\n" . implode("\r\n", $S) . "\r\n", $template);
                        $this->_writeEntry($fh, $cdrec, $filename, $xml);
                        $entries++;
                    }
                } $xml = null;
            } elseif ($cfilename === '[Content_Types].xml') {
                $TYPES = [];

                foreach ($this->sheets as $k => $v) {
                    $TYPES[] = '<Override PartName="/xl/worksheets/sheet' . ($k + 1) . '.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/>';

                    if (count($v['comments'])) {
                        $TYPES[] = '<Override PartName="/xl/comments' . ($k + 1). '.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml" />';
                    }
                } $template = str_replace('{TYPES}', implode("\r\n", $TYPES), $template);
                $this->_writeEntry($fh, $cdrec, $cfilename, $template);
                $entries++;
            } elseif ($cfilename === 'xl/styles.xml') {
                $NF = $XF = $FONTS = $F_KEYS = $FILLS = $FL_KEYS = [];
                $BR = ['<border><left/><right/><top/><bottom/><diagonal/></border>'];
                $BR_KEYS = [0 => 0];

                foreach ($this->NF as $k => $v) {
                    $NF[] = '<numFmt numFmtId="' . $k . '" formatCode="' . htmlspecialchars($v, ENT_QUOTES) . '"/>';
                }

foreach ($this->XF as $xf) {
                    $F_KEY = $xf[2] . '-' . $xf[4] . '-' . $xf[7];

                    if (isset($F_KEYS[$F_KEY])) {
                        $F_ID = $F_KEYS[$F_KEY];
                    } else {
                        $F_ID = $F_KEYS[$F_KEY] = count($FONTS);
                        $FONTS[] = '<font><name val="' . $this->defaultFont . '"/><family val="2"/>' . ($xf[7] ? '<sz val="' . $xf[7] . '"/>' : '<sz val="' . $this->defaultFontSize . '"/>') . ($xf[2] & self::F_BOLD ? '<b/>' : '') . ($xf[2] & self::F_ITALIC ? '<i/>' : '') . ($xf[2] & self::F_UNDERLINE ? '<u/>' : '') . ($xf[2] & self::F_STRIKE ? '<strike/>' : '') . ($xf[2] & self::F_HYPERLINK ? '<u/>' : '') . ($xf[2] & self::F_COLOR ? '<color rgb="' . $xf[4] . '"/>' : '') . '</font>';
                    } $FL_KEY = $xf[3] . '-' . $xf[5];

                    if (isset($FL_KEYS[$FL_KEY])) {
                        $FL_ID = $FL_KEYS[$FL_KEY];
                    } else {
                        $FL_ID = $FL_KEYS[$FL_KEY] = count($FILLS);
                        $FILLS[] = '<fill><patternFill patternType="' . ($xf[3] === 0 ? 'none' : '') . ($xf[3] & self::FL_SOLID ? 'solid' : '') . ($xf[3] & self::FL_MEDIUM_GRAY ? 'mediumGray' : '') . ($xf[3] & self::FL_DARK_GRAY ? 'darkGray' : '') . ($xf[3] & self::FL_LIGHT_GRAY ? 'lightGray' : '') . ($xf[3] & self::FL_GRAY_125 ? 'gray125' : '') . '"' . ($xf[3] & self::FL_COLOR ? '><fgColor rgb="' . $xf[5] . '"/><bgColor indexed="64"/></patternFill>' : ' />') . '</fill>';
                    } $align = '';

                    if ($xf[1] & self::A_LEFT) {
                        $align .= ' horizontal="left"';
                    } elseif ($xf[1] & self::A_RIGHT) {
                        $align .= ' horizontal="right"';
                    } elseif ($xf[1] & self::A_CENTER) {
                        $align .= ' horizontal="center"';
                    }

if ($xf[1] & self::A_TOP) {
                        $align .= ' vertical="top"';
                    } elseif ($xf[1] & self::A_MIDDLE) {
                        $align .= ' vertical="center"';
                    } elseif ($xf[1] & self::A_BOTTOM) {
                        $align .= ' vertical="bottom"';
                    }

if ($xf[1] & self::A_WRAPTEXT) {
                        $align .= ' wrapText="1"';
                    }

if ($xf[1] & self::A_ROTATION_90) {
                        $align .= ' textRotation="90"';
                    } $BR_ID = 0;

                    if ($xf[6] !== '') {
                        $b = $xf[6];

                        if (isset($BR_KEYS[$b])) {
                            $BR_ID = $BR_KEYS[$b];
                        } else {
                            $BR_ID = count($BR_KEYS);
                            $BR_KEYS[$b] = $BR_ID;
                            $border = '<border>';
                            $ba = explode(' ', $b);

                            if (!isset($ba[1])) {
                                $ba[] = $ba[0];
                                $ba[] = $ba[0];
                                $ba[] = $ba[0];
                            }

if (!isset($ba[4])) {
                                $ba[] = 'none';
                            } $sides = ['left' => 3, 'right' => 1, 'top' => 0, 'bottom' => 2, 'diagonal' => 4];

                            foreach ($sides as $side => $idx) {
                                $s = 'thin';
                                $c = '';
                                $va = explode('#', $ba[$idx]);

                                if (isset($va[1])) {
                                    $s = $va[0] === '' ? 'thin' : $va[0];
                                    $c = $va[1];
                                } elseif (in_array($va[0], $this->BR_STYLE, true)) {
                                    $s = $va[0];
                                } else {
                                    $c = $va[0];
                                }

if (strlen($c) === 6) {
                                    $c = 'FF' . $c;
                                }

if ($s && $s !== 'none') {
                                    $border .= '<' . $side . ' style="' . $s . '">' . '<color ' . ($c === '' ? 'auto="1"' : 'rgb="' . $c . '"') . '/>' . '</' . $side . '>';
                                } else {
                                    $border .= '<' . $side . '/>';
                                }
                            } $border .= '</border>';
                            $BR[] = $border;
                        }
                    } $XF[] = '<xf numFmtId="' . $xf[0] . '" fontId="' . $F_ID . '" fillId="' . $FL_ID . '" borderId="' . $BR_ID . '" xfId="0"' . ($xf[0] > 0 ? ' applyNumberFormat="1"' : '') . ($F_ID > 0 ? ' applyFont="1"' : '') . ($FL_ID > 0 ? ' applyFill="1"' : '') . ($BR_ID > 0 ? ' applyBorder="1"' : '') . ($align ? ' applyAlignment="1"><alignment' . $align . '/></xf>' : '/>');
                } array_unshift($NF, '<numFmts count="' . count($NF) . '">');
                $NF[] = '</numFmts>';
                array_unshift($XF, '<cellXfs count="' . count($XF) . '">');
                $XF[] = '</cellXfs>';
                array_unshift($FONTS, '<fonts count="' . count($FONTS) . '">');
                $FONTS[] = '</fonts>';
                array_unshift($FILLS, '<fills count="' . count($FILLS) . '">');
                $FILLS[] = '</fills>';
                array_unshift($BR, '<borders count="' . count($BR) . '">');
                $BR[] = '</borders>';
                $template = str_replace(['{NUMFMTS}', '{FONTS}', '{XF}', '{FILLS}', '{BORDERS}'], [implode("\r\n", $NF), implode("\r\n", $FONTS), implode("\r\n", $XF), implode("\r\n", $FILLS), implode("\r\n", $BR)], $template);
                $this->_writeEntry($fh, $cdrec, $cfilename, $template);
                $entries++;
            } else {
                $this->_writeEntry($fh, $cdrec, $cfilename, $template);
                $entries++;
            }
        } $before_cd = ftell($fh);
        fwrite($fh, $cdrec);
        fwrite($fh, $dirSignatureE);
        fwrite($fh, pack('v', 0));
        fwrite($fh, pack('v', 0));
        fwrite($fh, pack('v', $entries));
        fwrite($fh, pack('v', $entries));
        fwrite($fh, pack('V', mb_strlen($cdrec, '8bit')));
        fwrite($fh, pack('V', $before_cd));
        fwrite($fh, pack('v', mb_strlen($zipComments, '8bit')));
        fwrite($fh, $zipComments);

        return true;
    }

    protected function _writeEntry($fh, &$cdrec, $cfilename, $data)
    {
        $zipSignature = "\x50\x4b\x03\x04";
        $dirSignature = "\x50\x4b\x01\x02";
        $e = [];
        $e['uncsize'] = mb_strlen($data, '8bit');

        if ($e['uncsize'] < 256) {
            $e['comsize'] = $e['uncsize'];
            $e['vneeded'] = 10;
            $e['cmethod'] = 0;
            $zdata = $data;
        } else {
            $zdata = gzcompress($data);
            $zdata = substr(substr($zdata, 0, -4), 2);
            $e['comsize'] = mb_strlen($zdata, '8bit');
            $e['vneeded'] = 10;
            $e['cmethod'] = 8;
        } $e['bitflag'] = 0;
        $e['crc_32'] = crc32($data);
        $date = getdate();
        $e['dostime'] = ((($date['year'] - 1980) << 25) | ($date['mon'] << 21) | ($date['mday'] << 16) | ($date['hours'] << 11) | ($date['minutes'] << 5) | ($date['seconds'] >> 1));
        $e['offset'] = ftell($fh);
        fwrite($fh, $zipSignature);
        fwrite($fh, pack('v', $e['vneeded']));
        fwrite($fh, pack('v', $e['bitflag']));
        fwrite($fh, pack('v', $e['cmethod']));
        fwrite($fh, pack('V', $e['dostime']));
        fwrite($fh, pack('V', $e['crc_32']));
        fwrite($fh, pack('V', $e['comsize']));
        fwrite($fh, pack('V', $e['uncsize']));
        fwrite($fh, pack('v', mb_strlen($cfilename, '8bit')));
        fwrite($fh, pack('v', 0));
        fwrite($fh, $cfilename);
        fwrite($fh, $zdata);
        $e['external_attributes'] = (substr($cfilename, -1) === '/' && !$zdata) ? 16 : 32;
        $e['comments'] = '';
        $cdrec .= $dirSignature;
        $cdrec .= "\x0\x0";
        $cdrec .= pack('v', $e['vneeded']);
        $cdrec .= "\x0\x0";
        $cdrec .= pack('v', $e['cmethod']);
        $cdrec .= pack('V', $e['dostime']);
        $cdrec .= pack('V', $e['crc_32']);
        $cdrec .= pack('V', $e['comsize']);
        $cdrec .= pack('V', $e['uncsize']);
        $cdrec .= pack('v', mb_strlen($cfilename, '8bit'));
        $cdrec .= pack('v', 0);
        $cdrec .= pack('v', mb_strlen($e['comments'], '8bit'));
        $cdrec .= pack('v', 0);
        $cdrec .= pack('v', 0);
        $cdrec .= pack('V', $e['external_attributes']);
        $cdrec .= pack('V', $e['offset']);
        $cdrec .= $cfilename;
        $cdrec .= $e['comments'];
    }

    protected function _sheetToXML($idx, $template)
    {
        $_loc = setlocale(LC_NUMERIC, 0);
        setlocale(LC_NUMERIC, 'C');
        $COLS = [];
        $ROWS = [];
        $VML = [];
        $SHEETVIEWS = '';
        $PANE = '';

        if (count($this->sheets[$idx]['rows'])) {
            $ROWS[] = '<sheetData>';

            if ($this->sheets[$idx]['frozen'] !== '' || isset($this->sheets[$idx]['frozen'][0]) || isset($this->sheets[$idx]['frozen'][1])) {
                $x = $y = 0;

                if (is_string($this->sheets[$idx]['frozen'])) {
                    $AC = $this->sheets[$idx]['frozen'];
                    self::cell2coord($AC, $x, $y);
                } else {
                    if (isset($this->sheets[$idx]['frozen'][0])) {
                        $x = $this->sheets[$idx]['frozen'][0];
                    }

if (isset($this->sheets[$idx]['frozen'][1])) {
                        $y = $this->sheets[$idx]['frozen'][1];
                    } $AC = self::coord2cell($x, $y);
                }

if ($x > 0 || $y > 0) {
                    $split = '';

                    if ($x > 0) {
                        $split .= ' xSplit="' . $x . '"';
                    }

if ($y > 0) {
                        $split .= ' ySplit="' . $y . '"';
                    } $activepane = 'bottomRight';

                    if ($x > 0 && $y === 0) {
                        $activepane = 'topRight';
                    }

if ($x === 0 && $y > 0) {
                        $activepane = 'bottomLeft';
                    } $PANE .= '<pane' . $split . ' topLeftCell="' . $AC . '" activePane="' . $activepane . '" state="frozen"/>';
                    $PANE .= '<selection activeCell="' . $AC . '" sqref="' . $AC . '"/>';
                }
            }

if ($this->rtl || $PANE) {
                $SHEETVIEWS .= '<sheetViews>
<sheetView workbookViewId="0"' . ($this->rtl ? ' rightToLeft="1"' : '');
                $SHEETVIEWS .= $PANE ? ">\r\n" . $PANE . "\r\n</sheetView>" : ' />';
                $SHEETVIEWS .= "\r\n</sheetViews>";
            } $CUR_ROW = 0;
            $COL = [];

            foreach ($this->sheets[$idx]['rows'] as $r) {
                $CUR_ROW++;
                $row = '';
                $CUR_COL = 0;
                $RH = 0;

                foreach ($r as $v) {
                    $CUR_COL++;

                    if (!isset($COL[$CUR_COL])) {
                        $COL[$CUR_COL] = 0;
                    } $cname = self::coord2cell($CUR_COL - 1) . $CUR_ROW;

                    if ($v === null || $v === '') {
                        $row .= '<c r="' . $cname . '"/>';
                        continue;
                    } $ct = $cv = $cf = null;
                    $N = $NF = $A = $F = $FL = $C = $BG = $FS = $FR = 0;
                    $m = $m2 = null;
                    $BR = '';

                    if (is_string($v)) {
                        if (strpos($v, "\0") === 0) {
                            $v = substr($v, 1);
                            $vl = mb_strlen($v);
                        } else {
                            if (strpos($v, '<') !== false) {
                                if (preg_match('/<!--(.*?)-->/s', $v, $m)) {
                                    $com = trim($m[1]);
                                    $a = $this->author;

                                    if (preg_match('/^([^:]+): (.*)$/us', $com, $m2)) {
                                        $a = $m2[1];
                                        $com = $m2[2];
                                    } $this->sheets[$idx]['comments'][] = ['C' => $CUR_COL, 'R' => $CUR_ROW, 'N' => $cname, 'T' => $com, 'A' => $a];
                                    $v = str_replace($m[0], '', $v);
                                }

if (strpos($v, '<b>') !== false) {
                                    $F += self::F_BOLD;
                                }

if (strpos($v, '<i>') !== false) {
                                    $F += self::F_ITALIC;
                                }

if (strpos($v, '<u>') !== false) {
                                    $F += self::F_UNDERLINE;
                                }

if (strpos($v, '<s>') !== false) {
                                    $F += self::F_STRIKE;
                                }

if (preg_match('/<style([^>]+)>/', $v, $m)) {
                                    if (preg_match('/ color="([^"]+)"/', $m[1], $m2)) {
                                        $F += self::F_COLOR;
                                        $c = ltrim($m2[1], '#');
                                        $C = strlen($c) === 8 ? $c : ('FF' . $c);
                                    }

if (preg_match('/ bgcolor="([^"]+)"/', $m[1], $m2)) {
                                        $FL += self::FL_COLOR;
                                        $c = ltrim($m2[1], '#');
                                        $BG = strlen($c) === 8 ? $c : ('FF' . $c);
                                    }

if (preg_match('/ height="([^"]+)"/', $m[1], $m2)) {
                                        $RH = $m2[1];
                                    }

if (preg_match('/ nf="([^"]+)"/', $m[1], $m2)) {
                                        $c = htmlspecialchars_decode($m2[1], ENT_QUOTES);
                                        $NF = $this->getNumFmtId($c);
                                    }

if (preg_match('/ border="([^"]+)"/', $m[1], $m2)) {
                                        $b = htmlspecialchars_decode($m2[1], ENT_QUOTES);

                                        if ($b && $b !== 'none') {
                                            $BR = $b;
                                        }
                                    }

if (preg_match('/ font-size="([^"]+)"/', $m[1], $m2)) {
                                        $FS = (int)$m2[1];

                                        if ($RH === 0) {
                                            $RH = ($FS > $this->defaultFontSize) ? round($FS * 1.50, 1, PHP_ROUND_HALF_UP) : 0;
                                        }
                                    }
                                }

if (strpos($v, '<left>') !== false) {
                                    $A += self::A_LEFT;
                                }

if (strpos($v, '<center>') !== false) {
                                    $A += self::A_CENTER;
                                }

if (strpos($v, '<right>') !== false) {
                                    $A += self::A_RIGHT;
                                }

if (strpos($v, '<top>') !== false) {
                                    $A += self::A_TOP;
                                }

if (strpos($v, '<middle>') !== false) {
                                    $A += self::A_MIDDLE;
                                }

if (strpos($v, '<bottom>') !== false) {
                                    $A += self::A_BOTTOM;
                                }

if (strpos($v, '<wraptext>') !== false) {
                                    $A += self::A_WRAPTEXT;
                                }

if (strpos($v, '<rotation90>') !== false) {
                                    $A += self::A_ROTATION_90;
                                }

if (preg_match('/<a href="([^"]+)">(.*?)<\/a>/i', $v, $m)) {
                                    $F += self::F_HYPERLINK;
                                    $h = explode('#', $m[1]);

                                    if (count($h) === 1) {
                                        if (strpos($h[0], '!') > 0) {
                                            $this->sheets[$idx]['hyperlinks'][] = ['ID' => null, 'R' => $cname, 'H' => null, 'L' => $m[1]];
                                        } else {
                                            $this->sheets[$idx]['relidx']++;
                                            $this->sheets[$idx]['hyperlinks'][] = ['ID' => 'rId' . $this->sheets[$idx]['relidx'], 'R' => $cname, 'H' => $m[1], 'L' => ''];
                                        }
                                    } else {
                                        $this->sheets[$idx]['relidx']++;
                                        $this->sheets[$idx]['hyperlinks'][] = ['ID' => 'rId' . $this->sheets[$idx]['relidx'], 'R' => $cname, 'H' => $h[0], 'L' => $h[1]];
                                    }
                                }

if (preg_match('/<raw>(.*)<\/raw>/', $v, $m)) {
                                    $FR = 1;
                                    $v = $m[1];
                                } elseif (preg_match('/<f([^>]*)>/', $v, $m)) {
                                    $cf = strip_tags($v);
                                    $v = 0;

                                    if (preg_match('/ v="([^"]+)"/', $m[1], $m2)) {
                                        $v = $m2[1];
                                    }
                                } else {
                                    $v = strip_tags($v);
                                }
                            } $vl = mb_strlen($v);

                            if ($FR) {
                                $v = htmlspecialchars_decode($v);
                                $vl = mb_strlen($v);
                            } elseif ($NF) {
                                $cv = ltrim($v, '+');
                            } elseif ($v === '0' || preg_match('/^[-+]?[1-9]\d{0,14}$/', $v)) {
                                $cv = ltrim($v, '+');

                                if ($vl > 10) {
                                    $N = self::N_INT;
                                }
                            } elseif (preg_match('/^[-+]?(0|[1-9]\d*)\.\d\d?$/', $v)) {
                                $cv = ltrim($v, '+');
                                $N = self::N_DEC;
                            } elseif (preg_match('/^[-+]?(0|[1-9]{1,8})\.\d\d{0,8}$/', $v)) {
                                $cv = ltrim($v, '+');
                            } elseif (preg_match('/^\$[-+]?[0-9\.]+$/', $v)) {
                                $N = self::N_DOLLAR;
                                $cv = ltrim($v, '+$');
                            } elseif (preg_match('/^[-+]?[0-9\.]+( ₽| €)$/u', $v, $m)) {
                                if ($m[1] === ' ₽') {
                                    $N = self::N_RUB;
                                } elseif ($m[1] === ' €') {
                                    $N = self::N_EURO;
                                } $cv = trim($v, ' +₽€');
                            } elseif (preg_match('/^([-+]?\d+)%$/', $v, $m)) {
                                $cv = round($m[1] / 100, 2, PHP_ROUND_HALF_UP);
                                $N = self::N_PERCENT_INT;
                            } elseif (preg_match('/^([-+]?\d+\.\d+)%$/', $v, $m)) {
                                $cv = round($m[1] / 100, 4, PHP_ROUND_HALF_UP);
                                $N = self::N_PRECENT_DEC;
                            } elseif (preg_match('/^(\d\d\d\d)-(\d\d)-(\d\d)$/', $v, $m)) {
                                $cv = self::date2excel($m[1], $m[2], $m[3]);
                                $N = self::N_DATE;
                            } elseif (preg_match('/^(\d\d)\/(\d\d)\/(\d\d\d\d)$/', $v, $m)) {
                                $cv = self::date2excel($m[3], $m[2], $m[1]);
                                $N = self::N_DATE;
                            } elseif (preg_match('/^(\d\d):(\d\d):(\d\d)$/', $v, $m)) {
                                $cv = self::date2excel(0, 0, 0, $m[1], $m[2], $m[3]);
                                $N = self::N_TIME;
                            } elseif (preg_match('/^(\d\d\d\d)-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)$/', $v, $m)) {
                                $cv = self::date2excel($m[1], $m[2], $m[3], $m[4], $m[5], $m[6]);
                                $N = ((int)$m[1] === 0) ? self::N_TIME : self::N_DATETIME;
                            } elseif (preg_match('/^(\d\d)\/(\d\d)\/(\d\d\d\d) (\d\d):(\d\d):(\d\d)$/', $v, $m)) {
                                $cv = self::date2excel($m[3], $m[2], $m[1], $m[4], $m[5], $m[6]);
                                $N = self::N_DATETIME;
                            } elseif (preg_match('/^[0-9\.\-\+]+$/', $v)) {
                                $A += ($A & (self::A_LEFT | self::A_CENTER)) ? 0 : self::A_RIGHT;
                            } elseif (preg_match('/^https?:\/\/\S+$/i', $v)) {
                                $h = explode('#', $v);
                                $this->sheets[$idx]['relidx']++;
                                $this->sheets[$idx]['hyperlinks'][] = ['ID' => 'rId' . $this->sheets[$idx]['relidx'], 'R' => $cname, 'H' => $h[0], 'L' => isset($h[1]) ? $h[1] : ''];
                                $F += self::F_HYPERLINK;
                            } elseif (preg_match("/^[a-zA-Z0-9_\.\-]+@([a-zA-Z0-9][a-zA-Z0-9\-]*\.)+[a-zA-Z]{2,}$/", $v)) {
                                $this->sheets[$idx]['relidx']++;
                                $this->sheets[$idx]['hyperlinks'][] = ['ID' => 'rId' . $this->sheets[$idx]['relidx'], 'R' => $cname, 'H' => 'mailto:' . $v, 'L' => ''];
                                $F += self::F_HYPERLINK;
                            } elseif (strpos($v, "\n") !== false) {
                                $A |= self::A_WRAPTEXT;
                            }

if (($N === self::N_DATE || $N === self::N_DATETIME) && $cv < 0) {
                                $cv = null;
                                $N = 0;
                            }
                        }

if ($cv === null) {
                            $v = self::esc($v);

                            if ($cf) {
                                $ct = 'str';
                                $cv = $v;
                            } elseif ($v === '') {
                                $ct = 's';
                            } elseif (mb_strlen($v) > 160) {
                                $ct = 'inlineStr';
                                $cv = $v;
                            } else {
                                $ct = 's';
                                $cv = false;
                                $skey = '~' . $v;

                                if (isset($this->SI_KEYS[$skey])) {
                                    $cv = $this->SI_KEYS[$skey];
                                }

if ($cv === false) {
                                    $this->SI[] = $v;
                                    $cv = count($this->SI) - 1;
                                    $this->SI_KEYS[$skey] = $cv;
                                }
                            }
                        }
                    } elseif (is_int($v)) {
                        $vl = mb_strlen((string)$v);
                        $cv = $v;
                    } elseif (is_float($v)) {
                        $vl = mb_strlen((string)$v);
                        $cv = $v;
                    } elseif ($v instanceof \DateTime) {
                        $vl = 16;
                        $cv = self::date2excel($v->format('Y'), $v->format('m'), $v->format('d'), $v->format('H'), $v->format('i'), $v->format('s'));
                        $N = self::N_DATETIME;
                    } else {
                        continue;
                    }

if ($NF) {
                        $N = $NF;
                    } $COL[$CUR_COL] = max($vl, $COL[$CUR_COL]);
                    $cs = 0;

                    if (($N + $A + $F + $FL + $FS > 0) || $BR !== '') {
                        if ($FL === self::FL_COLOR) {
                            $FL += self::FL_SOLID;
                        }

if (($F & self::F_HYPERLINK) && !($F & self::F_COLOR)) {
                            $F += self::F_COLOR;
                            $C = 'FF0563C1';
                        } $XF_KEY = $N . '-' . $A . '-' . $F . '-' . $FL . '-' . $C . '-' . $BG . '-' . $BR . '-' . $FS;

                        if (isset($this->XF_KEYS[$XF_KEY])) {
                            $cs = $this->XF_KEYS[$XF_KEY];
                        }

if ($cs === 0) {
                            $cs = count($this->XF);
                            $this->XF_KEYS[$XF_KEY] = $cs;
                            $this->XF[] = [$N, $A, $F, $FL, $C, $BG, $BR, $FS];
                        }
                    } $row .= '<c r="' . $cname . '"' . ($ct ? ' t="' . $ct . '"' : '') . ($cs ? ' s="' . $cs . '"' : '');

                    if ($ct === 's' && $cv === null) {
                        $row .= '/>';
                    } else {
                        $row .= '>' . ($cf ? '<f>' . $cf . '</f>' : '') . ($ct === 'inlineStr' ? '<is><t>' . $cv . '</t></is>' : '<v>' . $cv . '</v>') . "</c>\r\n";
                    }
                } $ROWS[] = '<row r="' . $CUR_ROW . '"' . ($RH ? ' customHeight="1" ht="' . $RH . '"' : '') . '>' . $row . "</row>";
            } $COLS[] = '<cols>';

            foreach ($COL as $k => $max) {
                if ($max === 0) {
                    continue;
                } $w = isset($this->sheets[$idx]['colwidth'][$k]) ? $this->sheets[$idx]['colwidth'][$k] : min($max + 1, 60);
                $COLS[] = '<col min="' . $k . '" max="' . $k . '" width="' . $w . '" customWidth="1" />';
            } $COLS[] = '</cols>';

            if (count($COLS) === 2) {
                $COLS = [];
            } $ROWS[] = '</sheetData>';
            $REF = 'A1:' . self::coord2cell(count($COL) - 1) . $CUR_ROW;
        } else {
            $ROWS[] = '<sheetData/>';
            $REF = 'A1:A1';
        } $AUTOFILTER = '';

        if ($this->sheets[$idx]['autofilter']) {
            $AUTOFILTER = '<autoFilter ref="' . $this->sheets[$idx]['autofilter'] . '" />';
        } $MERGECELLS = [];

        if (count($this->sheets[$idx]['mergecells'])) {
            $MERGECELLS[] = '';
            $MERGECELLS[] = '<mergeCells count="' . count($this->sheets[$idx]['mergecells']) . '">';

            foreach ($this->sheets[$idx]['mergecells'] as $m) {
                $MERGECELLS[] = '<mergeCell ref="' . $m . '"/>';
            } $MERGECELLS[] = '</mergeCells>';
        } $HYPERLINKS = [];

        if (count($this->sheets[$idx]['hyperlinks'])) {
            $HYPERLINKS[] = '<hyperlinks>';

            foreach ($this->sheets[$idx]['hyperlinks'] as $h) {
                $HYPERLINKS[] = '<hyperlink ref="' . $h['R'] . '"' . ($h['ID'] ? ' r:id="' . $h['ID'] . '"' : '') . ' location="' . self::esc($h['L']) . '" display="' . self::esc($h['H'] . ($h['L'] ? ' - ' . $h['L'] : '')) . '" />';
            } $HYPERLINKS[] = '</hyperlinks>';
        }

if (count($this->sheets[$idx]['comments'])) {
            $VML[] = '<legacyDrawing r:id="rVMLCom" />';
        } setlocale(LC_NUMERIC, $_loc);

        return str_replace(['{REF}', '{COLS}', '{ROWS}', '{AUTOFILTER}', '{MERGECELLS}', '{HYPERLINKS}', '{SHEETVIEWS}', '{VML}'], [$REF, implode("\r\n", $COLS), implode("\r\n", $ROWS), $AUTOFILTER, implode("\r\n", $MERGECELLS), implode("\r\n", $HYPERLINKS), $SHEETVIEWS, implode("\r\n", $VML)], $template);
    }

    public function setDefaultFont($name)
    {
        $this->defaultFont = $name;

        return $this;
    }

    public function setDefaultFontSize($size)
    {
        $this->defaultFontSize = $size;

        return $this;
    }

    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    public function setSubject($subject)
    {
        $this->subject = $subject;

        return $this;
    }

    public function setAuthor($author)
    {
        $this->author = $author;

        return $this;
    }

    public function setCompany($company)
    {
        $this->company = $company;

        return $this;
    }

    public function setManager($manager)
    {
        $this->manager = $manager;

        return $this;
    }

    public function setKeywords($keywords)
    {
        $this->keywords = $keywords;

        return $this;
    }

    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    public function setCategory($category)
    {
        $this->category = $category;

        return $this;
    }

    public function setLanguage($language)
    {
        $this->language = $language;

        return $this;
    }

    public function setApplication($application)
    {
        $this->application = $application;

        return $this;
    }

    public function setLastModifiedBy($lastModifiedBy)
    {
        $this->lastModifiedBy = $lastModifiedBy;

        return $this;
    }

    public function autoFilter($range)
    {
        $this->sheets[$this->curSheet]['autofilter'] = $range;

        return $this;
    }

    public function mergeCells($range)
    {
        $this->sheets[$this->curSheet]['mergecells'][] = $range;

        return $this;
    }

    public function setColWidth($col, $width)
    {
        if (is_string($col) && strpos($col, ':') !== false) {
            list($from, $to) = explode(':', $col, 2);
            $from = self::col2index($from);
            $to = self::col2index($to);
            for ($i = $from; $i <= $to; $i++) {
                $this->sheets[$this->curSheet]['colwidth'][$i] = $width;
            }

return $this;
        } $this->sheets[$this->curSheet]['colwidth'][self::col2index($col)] = $width;

        return $this;
    }

    public function rightToLeft($value = true)
    {
        $this->rtl = $value;

        return $this;
    }

    public function freezePanes($cell)
    {
        $this->sheets[$this->curSheet]['frozen'] = $cell;

        return $this;
    }

    public function getNumFmtId($code)
    {
        if (isset($this->NF[$code])) {
            return (int)$code;
        }

if (isset($this->NF_KEYS[$code])) {
            return $this->NF_KEYS[$code];
        } $id = self::N_CUSTOM + count($this->NF);
        $this->NF[$id] = $code;
        $this->NF_KEYS[$code] = $id;

        return $id;
    }

    public static function date2excel($year, $month, $day, $hours = 0, $minutes = 0, $seconds = 0)
    {
        $excelTime = (($hours * 3600) + ($minutes * 60) + $seconds) / 86400;
        $year = (int) $year;
        $month = (int) $month;
        $day = (int) $day;

        if ($year === 0) {
            return $excelTime;
        } $excel1900isLeapYear = 1;

        if (($year === 1900) && ($month <= 2)) {
            $excel1900isLeapYear = 0;
        } $myExcelBaseDate = 2415020;

        if ($month > 2) {
            $month -= 3;
        } else {
            $month += 9;
            --$year;
        } $century = floor($year / 100);
        $decade = $year - floor($year / 100) * 100;
        $excelDate = floor((146097 * $century) / 4) + floor((1461 * $decade) / 4) + floor((153 * $month + 2) / 5) + $day + 1721119 - $myExcelBaseDate + $excel1900isLeapYear;

        return (float)$excelDate + $excelTime;
    }

    public static function esc($str)
    {
        return str_replace(['&', '<', '>', "\x00", "\x03", "\x0B"], ['&amp;', '&lt;', '&gt;', '', '', ''], $str);
    }

    public static function raw($value)
    {
        return "\0" . $value;
    }

    public static function rawArray($array)
    {
        foreach ($array as &$row) {
            foreach ($row as &$value) {
                $value = "\0" . $value;
            }
        }

return $array;
    }

    private static function col2index($col)
    {
        if (is_int($col)) {
            return $col;
        } $m = null;

        if (preg_match('/^([A-Za-z]+)$/', $col, $m)) {
            $letters = strtoupper($m[1]);
            $len = strlen($letters);
            $x = 0;
            for ($i = 0; $i < $len; $i++) {
                $int = ord($letters[$i]) - 65;
                $int += ($i === $len - 1) ? 0 : 1;
                $x += $int * pow(26, $len - $i - 1);
            }

return $x + 1;
        }

return (int)$col;
    }

    public static function cell2coord($cell, &$x, &$y)
    {
        $x = $y = 0;
        $m = null;

        if (preg_match('/^([A-Z]+)(\d+)$/', $cell, $m)) {
            $len = strlen($m[1]);
            for ($i = 0; $i < $len; $i++) {
                $int = ord($m[1][$i]) - 65;
                $int += ($i === $len - 1) ? 0 : 1;
                $x += $int * pow(26, $len - $i - 1);
            } $y = ((int)$m[2]) - 1;
        }
    }

    public static function coord2cell($x, $y = null)
    {
        $c = '';
        for ($i = $x; $i >= 0; $i = ((int)($i / 26)) - 1) {
            $c = chr(65 + $i % 26) . $c;
        }

return $c . ($y === null ? '' : ($y + 1));
    }
}
