Skip to content

Commit

Permalink
Excel拓展类
Browse files Browse the repository at this point in the history
  • Loading branch information
dashingunique committed Jun 23, 2020
1 parent 436cfbd commit 133af06
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/traits/Exportable.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ trait Exportable
*/
private $headerStyle;

/**
* @var array 备注信息 => ['备注:备注']
*/
protected $remark;

/**
* 获取文件类型
* @param string $path
Expand Down Expand Up @@ -80,6 +85,17 @@ public function download($path, callable $callback = null)
return '';
}

/**
* 设置备注信息
* @param string $remark
* @return $this
*/
public function remark(string $remark)
{
$this->remark = $remark;
return $this;
}


/**
* 导出并下载Excel(csv,odb)
Expand Down Expand Up @@ -122,12 +138,13 @@ private function exportOrDownload($path, $function, callable $callback = null)
$writer->addNewSheetAndMakeItCurrent();
}
}
$this->writeRemark($writer);
$writer->close();
}

/**
* 写入表格信息(通过集合)
* @param $writer
* @param WriterInterface $writer
* @param Collection $collection
* @param callable|null $callback
*/
Expand All @@ -149,6 +166,18 @@ private function writeRowsFromCollection($writer, Collection $collection, ?calla
$writer->addRows($collection->toArray());
}

/**
* 写入备注信息
* @param $writer
*/
private function writeRemark($writer)
{
if (!empty($this->remark)) {
// Write all rows
$writer->addRow($this->remark);
}
}

/**
* 写入数据(通过生成器)
* @param $writer
Expand All @@ -175,6 +204,8 @@ private function writeRowsFromGenerator($writer, Generator $generator, ?callable
}
}



/**
* 写入信息(通过数组)
* @param $writer
Expand All @@ -191,6 +222,8 @@ private function writeRowsFromArray($writer, array $array, ?callable $callback =
}
}



/**
* 写入header
* @param $writer
Expand Down

0 comments on commit 133af06

Please sign in to comment.