Code Snippet: PhpSpreadsheet output

catatan segala bentuk output PhpSpreadsheet biar tidak lupa. hhhe.

Excel (xlsx)

//Create file excel.xlsx
$writer = new Xlsx($spreadsheet);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Laporan_' . $fileName . '.xlsx"');
header('Cache-Control: max-age=0');             
$writer->save('php://output');

PDF

$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($spreadsheet);

// Redirect output to a client’s web browser (PDF)
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="Laporan_' . $fileName . '.pdf"');
header('Cache-Control: max-age=0');
$writer->save('php://output');
exit;

HTML

$writer = new \PhpOffice\PhpSpreadsheet\Writer\Html($spreadsheet);
$hdr = $writer->generateHTMLHeader();
$sty = $writer->generateStyles(false); // do not write <style> and </style>
$newstyle = <<<EOF
<style type='text/css'>
    $sty
    body {
       background-color: yellow;
    }
    td {
       padding: 0 5px 0 5px;
    }
</style>
EOF;
echo preg_replace('@</head>@', "$newstyle\n</head>", $hdr);
echo $writer->generateSheetData();
echo $writer->generateHTMLFooter();
exit;

Leave a Comment

Your email address will not be published. Required fields are marked *