解决方式就是重置Excel的run方法
<?php
namespace common\services\common;
use moonland\phpexcel\Excel;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use yii\base\InvalidConfigException;
class ErpExcel extends Excel
{
/**
* 重置run方法,解决创建多个sheet时,会产生一个额外的空的worksheet
* @return array|\moonland\phpexcel\multitype|string|void
* @throws InvalidConfigException
* @throws \PhpOffice\PhpSpreadsheet\Exception
*/
public function run()
{
if ('export' == $this->mode) {
$sheet = new Spreadsheet();
if (!isset($this->models)) {
throw new InvalidConfigException('Config models must be set');
}
if (isset($this->properties)) {
$this->properties($sheet, $this->properties);
}
if ($this->isMultipleSheet) {
$index = 0;
$worksheet = [];
foreach ($this->models as $title => $models) {
if (0 === $index) {
$activeSheet = $sheet->getActiveSheet();
$activeSheet->setTitle($title);
$worksheet[$index] = $activeSheet;
} else {
$sheet->createSheet($index);
$sheet->getSheet($index)->setTitle($title);
$worksheet[$index] = $sheet->getSheet($index);
}
$columns = $this->columns[$title] ?? [];
$headers = $this->headers[$title] ?? [];
$this->executeColumns($worksheet[$index], $models, $this->populateColumns($columns), $headers);
++$index;
}
} else {
$worksheet = $sheet->getActiveSheet();
$this->executeColumns($worksheet, $this->models, isset($this->columns) ? $this->populateColumns($this->columns) : [], $this->headers ?? []);
}
if ($this->asAttachment) {
$this->setHeaders();
}
$this->writeFile($sheet);
$sheet->disconnectWorksheets();
unset($sheet);
} elseif ('import' == $this->mode) {
if (is_array($this->fileName)) {
$datas = [];
foreach ($this->fileName as $key => $filename) {
$datas[$key] = $this->readFile($filename);
}
return $datas;
} else {
return $this->readFile($this->fileName);
}
}
}
}