spreadsheet = new Spreadsheet(); $sheet = $this->spreadsheet->getActiveSheet(); $this->sheetobj = $sheet; } public function settitle($title) { $this->title = $title; } public function setbody($body) { $this->body = $body; } /** * 自定义缓存 * * @return void * @author wj * @date 2023-01-17 */ public function setmararycache() { $cache = new MyPhpOffacePsr16Implementation(); Settings::setCache($cache); } public function getsheetobj($body, $title = [], $style = [], $newsheet = false) { if ($newsheet) { $sheetobj = $this->spreadsheet->createSheet(); } else { $sheetobj = $this->sheetobj; } if (!empty($title)) { foreach ($title as $key => $value) { $sheetobj->getCell($value['cell'])->setValue($value['value']); } } foreach ($body as $key => $value) { foreach ($value as $k => $v) { $sheetobj->getCell($v['cell'])->setValue($v['value']); } } if (!empty($style)) { foreach ($style as $key => $value) { $sheetobjstyle = $sheetobj->getColumnDimension($key); foreach ($value as $k => $v) { switch ($k) { case 'width': $sheetobjstyle->setWidth($v); break; } } } } $this->sheetobj = $sheetobj; } public function exportsheet($filename, $type = "Xlsx") { header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename=' . $filename); header('Cache-Control: max-age=0'); // If you're serving to IE 9, then the following may be needed header('Cache-Control: max-age=1'); header('Pragma: public'); // HTTP/1.0 $objwriter = IOFactory::createWriter($this->spreadsheet, $type); $objwriter->save('php://output'); } public function sevefile($filename, $type = "Xlsx") { $objwriter = IOFactory::createWriter($this->spreadsheet, $type); $objwriter->save($filename); } }