<?php
namespace lib;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
//use Symfony\Component\Cache\Adapter\FilesystemAdapter;
//use Symfony\Component\Cache\Psr16Cache;
/**
 * exportXlsx
 * composer require phpoffice/phpspreadsheet
 * @author HzqGhost admin@whiledo.com QQ:313143468
 * @version 1.2.0
 *
 */
class exportXlsx
{
    protected $fp; //流
    protected $br; //换行符
    private $isoutput; //是否输出到客户端
    private $spreadsheet;
    private $writer;
    private $sheets = [];
    private $sheet;
    private $rowI;
	public function __construct($fp,string $cacheDir='',string $cateTag='excel')
    {
        $this->fp = $fp;
        $this->br = "\r\n";
        //UTF8 csv文件头前需添加BOM，不然会是乱码
        //fwrite($this->fp, chr(0xEF).chr(0xBB).chr(0xBF));
        $this->spreadsheet = new Spreadsheet();
        $this->writer = new Xlsx($this->spreadsheet);
        if($cacheDir!=''){
            $adapter = new FilesystemAdapter($cateTag, 60*60*2, $cacheDir);//$namespace,$defaultLifetime(seconds),$directory
            $cache = new Psr16Cache($adapter);         
            \PhpOffice\PhpSpreadsheet\Settings::setCache($cache);
        }
    }
	public function header(string $filename='')
    {
        $this->isoutput = true;
        if($filename==''){
            $filename = time().'.xlsx';
        }
        ob_end_clean();
        header('Content-Description: File Transfer');
        header("Content-type:application/vnd.ms-excel");
        header('Content-Disposition: attachment; filename='.$filename);
        ob_start();
    }

	public function bookBegin()
    {
        $this->rowI = 0;
    }
	public function bookEnd()
    {
        $this->writer->save($this->fp);
        fclose($this->fp);
    }

	public function sheetBegin(string $sheetName,array $headerRow=[],array $headerRowWidths=[])
    {
        $this->rowI = 0;
        if(empty($this->sheets)){
            $this->sheets[0] = $sheetName;
            $this->sheet = $this->spreadsheet->getActiveSheet();
            $this->sheet->setTitle($sheetName);
        }else{
            $count = count($this->sheets);
            $this->sheets[$count] = $sheetName;
            $sheet = new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet($this->spreadsheet, $sheetName);
            $this->spreadsheet->addSheet($sheet, $count);
            $this->sheet = $this->spreadsheet->getSheet($count);
        }
        if(!empty($headerRow)){
            $colI=0;
            foreach($headerRow as $k=> $cell){
                $this->sheet->setCellValueByColumnAndRow($colI+1,$this->rowI+1,$this->specialchars($cell));
                $colI++;
            }
            $this->rowI++;
        }
    }

	public function sheetEnd()
    {
        ;
    }

	public function append(array $row=[])
    {
        if($row){
            $colI=0;
            foreach($row as $k=>$cell){
                $cell = $this->specialchars($cell);
                $this->sheet->setCellValueByColumnAndRow($colI+1,$this->rowI+1,$cell);
                $colI++;
            }
            //$this->sheet->fromArray($row,null,'A'.$this->rowI);
            $this->rowI++;
        }
        if($this->isoutput){
            ob_flush();
            flush();
        }
    }

    protected function specialchars($value){
        return $value;
    }
}