From bae43ad382df420d1f29255701170fba9fd984ed Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Mon, 25 May 2020 22:50:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96excel=E5=AF=BC=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extend/catcher/library/excel/Excel.php | 19 +++++++++++--- extend/catcher/library/excel/Factory.php | 29 +++++++++++++++++++++ extend/catcher/library/excel/MacroExcel.php | 20 +------------- 3 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 extend/catcher/library/excel/Factory.php diff --git a/extend/catcher/library/excel/Excel.php b/extend/catcher/library/excel/Excel.php index 67df9ea..cbdebc4 100644 --- a/extend/catcher/library/excel/Excel.php +++ b/extend/catcher/library/excel/Excel.php @@ -2,9 +2,11 @@ namespace catcher\library\excel; use catcher\CatchUpload; +use catcher\exceptions\FailedException; use PhpOffice\PhpSpreadsheet\Exception; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; +use PhpOffice\PhpSpreadsheet\Writer\Xls; use think\file\UploadedFile; class Excel @@ -27,9 +29,8 @@ class Excel * @param ExcelContract $excel * @param $path * @param null $disk + * @return bool * @throws Exception - * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception - * @return void */ public function save(ExcelContract $excel, $path, $disk = null) { @@ -37,9 +38,19 @@ class Excel $this->init(); - (new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($this->spreadsheet))->save($path); + Factory::make(pathinfo($path, PATHINFO_EXTENSION)) + ->setSpreadsheet($this->spreadsheet) + ->save($path); - // $this->upload($disk, $path); + if (!file_exists($path)) { + throw new FailedException($path . ' generate failed'); + } + + if ($disk) { + $path = $this->upload($disk, $path); + } + + return $path; } /** diff --git a/extend/catcher/library/excel/Factory.php b/extend/catcher/library/excel/Factory.php new file mode 100644 index 0000000..5b63038 --- /dev/null +++ b/extend/catcher/library/excel/Factory.php @@ -0,0 +1,29 @@ +excel, 'start')) { - return $this->excel->start(); + $this->start = $this->excel->start(); } return $this->start; @@ -74,11 +74,6 @@ trait MacroExcel protected function getSheetColumns() { if (empty($this->columns)) { - if (method_exists($this->excel, 'sheetColumns')) { - $this->columns = $this->excel->sheetColumns(); - return $this->columns; - } - $start = $this->getStartSheet(); $columns = []; @@ -145,19 +140,6 @@ trait MacroExcel } } - /** - * 设置其他信息 - * - * @time 2020年05月25日 - * @return void - */ - protected function setOther() - { - if (method_exists($this->excel, 'setOther')) { - $this->excel->setOther($this->getWorksheet()); - } - } - /** * register worksheet for excel *