* * @package Webapp\Data */ final class Xlsx { private Options $options; private Excel $excel; private Sheet $sheet; private array $webapp; private array $webuser; private array $meta = []; private array $titles = []; private array $subtitles = []; private array $atabel = []; private int $baris = 1; /** * @param Engine $app */ public function __construct(Engine $app) { $this->webapp = (array) $app->session()->get('WEB_APP', ['REG_NAME'=>'', 'APP_NAME'=>'', 'APP_VER'=>'']); $this->webuser = (array) $app->session()->get('WEB_USER', ['UserID'=>0, 'Username'=>'']); $this->meta = [ 'company' => $this->webapp['REG_NAME'], 'subject' => $this->webapp['REG_NAME'], 'author' => $this->webapp['APP_NAME'].' v'.$this->webapp['APP_VER'], ]; $this->options = Options::create() ->DefaultFont([ Style::FONT_NAME => "Calibri", Style::FONT_SIZE => 11 ]) ->defaultDateFormat('DD-MM-YYYY') ->defaultTimeFormat('HH:MM') ->defaultDateTimeFormat('DD-MM-YYYY HH:MM'); } public function set_meta(array $meta): void { if (!empty($meta)) $this->meta = array_merge($this->meta, $meta); } public function add_title(string|array $judul): void { if (empty($judul)) return; if (is_array($judul)) { $this->titles = $judul; } else array_push($this->titles, $judul); } public function add_sub_title(string|array $judul): void { if (empty($judul)) return; if (is_array($judul)) { $this->subtitles = $judul; } else array_push($this->subtitles, $judul); } public function add_field(string $nama="", int $lebar=0, string $style="@text", ?callable $func=null): void { $this->atabel[] = ['n' => $nama, 'l' => $lebar, 's' => $style, 'f' => $func]; } public function add_sheet(string $nama_sheet="BARU", bool $show_header=false): void { if (empty($this->excel)) { $this->excel = Excel::create([$nama_sheet], $this->options) ->setCompany($this->meta['company'] ?? "") ->setAuthor($this->meta['author'] ?? "") ->setTitle($this->meta['title'] ?? "") ->setDescription($this->meta['desc'] ?? "") ->setSubject($this->meta['subject'] ?? ""); $this->sheet = $this->excel->sheet(); //getSheet(); } else { $this->sheet = $this->excel->makeSheet($nama_sheet); } if ($show_header === true) { $this->show_titles(); $this->show_headers(); } } public function add_row(array $row): void { if (is_array($row) && !empty($row)) $this->sheet->writeRow($row); } public function add_row_bold(array $row): void { if (is_array($row) && !empty($row)) $this->sheet->writeRow($row)->applyFontStyleBold(); } public function show_titles(): void { $this->baris = $this->sheet->getCurrentRow(); $jum_kolom = count($this->atabel); foreach ($this->titles as $title) { $this->sheet->writeTo('A'.(string) $this->baris.':'.$this->excel->colLetter($jum_kolom).(string) $this->baris, $title) ->applyFontStyleBold() ->applyTextCenter(); $this->baris++; } if (count($this->subtitles) > 0) { $this->baris++; foreach ($this->subtitles as $title) { $this->sheet->writeTo('A'.(string) $this->baris++, $title); } } else $this->sheet->writeTo('A'.(string) $this->baris, ''); } public function show_headers(bool $is_hide=false): void { if (!$is_hide) $this->sheet->writeHeader(array_column($this->atabel, 'n')) ->applyFontStyleBold() ->applyTextCenter() ->applyBorder(Style::BORDER_STYLE_THIN); $this->sheet->setColFormats(array_column($this->atabel, 's')) ->setColWidths(array_column($this->atabel, 'l')); } public function add_stamp(int $ctk_ke=1, bool $tampil_nama=false): void { $this->sheet->nextRow(); $this->sheet->writeCell($this->get_stamp($ctk_ke, $tampil_nama), ['font-size' => 8, 'font-style' => 'italic']); } public function get_stamp(int $ctk_ke=1, bool $tampil_nama=false): string { return $this->webapp['APP_NAME']."v".$this->webapp['APP_VER'].date("\@ymd.His")."-".isi_nol($this->webuser['UserID'], 3).(($tampil_nama)?"/".strtoupper($this->webuser['Username']):"").(($ctk_ke>0)?"#".isi_nol($ctk_ke,2):""); } public function download(string $nama_file="File_Excel.xlsx"): void { $this->excel->download($nama_file); } }