1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace app\common\server;
- //use think\Log;
- use app\common\server\MyPhpOffacePsr16Implementation;
- //use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use PhpOffice\PhpSpreadsheet\Settings;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- /**
- * excel导出
- *
- * @Author wj
- * @DateTime 2022-10-02
- * @Describe
- */
- class PhpExcel
- {
- private $title;
- private $body;
- private $sheetobj;
- private $spreadsheet;
- public function __construct()
- {
- $this->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);
- }
- }
|