Commit 78d2df4d authored by 王源's avatar 王源 🎧

优化

parent 8306aedf
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
namespace Meibuyu\Micro\Tools; namespace Meibuyu\Micro\Tools;
use Hyperf\Contract\ConfigInterface; use Hyperf\Contract\ConfigInterface;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment; use PhpOffice\PhpSpreadsheet\Style\Alignment;
...@@ -19,6 +20,7 @@ use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; ...@@ -19,6 +20,7 @@ use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class Exporter class Exporter
{ {
/** /**
* 以字节流的形式下载 * 以字节流的形式下载
*/ */
...@@ -57,8 +59,7 @@ class Exporter ...@@ -57,8 +59,7 @@ class Exporter
* @param string $tempFilePath 模板文件地址 * @param string $tempFilePath 模板文件地址
* @param string $name 名称 * @param string $name 名称
* @param int $sheetIndex * @param int $sheetIndex
* @throws \PhpOffice\PhpSpreadsheet\Exception * @throws PhpSpreadsheetException
* @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
* @throws \Exception * @throws \Exception
*/ */
public function __construct(int $export_type, $tempFilePath = "", $name = "export_data", $sheetIndex = 0) public function __construct(int $export_type, $tempFilePath = "", $name = "export_data", $sheetIndex = 0)
...@@ -67,7 +68,6 @@ class Exporter ...@@ -67,7 +68,6 @@ class Exporter
$this->rootPath = $this->config->get('server.settings.document_root', BASE_PATH . '/public'); $this->rootPath = $this->config->get('server.settings.document_root', BASE_PATH . '/public');
if ($tempFilePath) { if ($tempFilePath) {
$tempFilePath = $this->rootPath . "/" . $tempFilePath; $tempFilePath = $this->rootPath . "/" . $tempFilePath;
//print_r($tempFilePath);
if (file_exists($tempFilePath)) { if (file_exists($tempFilePath)) {
$this->fileType = ucfirst(strtolower(pathinfo($tempFilePath, PATHINFO_EXTENSION))); $this->fileType = ucfirst(strtolower(pathinfo($tempFilePath, PATHINFO_EXTENSION)));
$reader = IOFactory::createReader($this->fileType)->load($tempFilePath); $reader = IOFactory::createReader($this->fileType)->load($tempFilePath);
...@@ -100,8 +100,8 @@ class Exporter ...@@ -100,8 +100,8 @@ class Exporter
/** /**
* 设置非保护区间,调用此方法全表将自动保护 * 设置非保护区间,调用此方法全表将自动保护
* @param string $unprotectRange 例如"A1:B1";从A列B列第一行到数据结束行不需要保护, * @param string $unprotectRange 例如"A1:B1";从A列B列第一行到数据结束行不需要保护
* @throws \PhpOffice\PhpSpreadsheet\Exception * @return $this
*/ */
public function setUnprotectRange($unprotectRange = "") public function setUnprotectRange($unprotectRange = "")
{ {
...@@ -119,7 +119,8 @@ class Exporter ...@@ -119,7 +119,8 @@ class Exporter
* 往Excel里面新增一张sheet表 * 往Excel里面新增一张sheet表
* @param string $name sheet的名字 * @param string $name sheet的名字
* @param bool $activeSheet 是否激货并切换到当前sheet 默认否 * @param bool $activeSheet 是否激货并切换到当前sheet 默认否
* @throws \PhpOffice\PhpSpreadsheet\Exception * @return $this
* @throws PhpSpreadsheetException
*/ */
public function addSheet($name, $activeSheet = false) public function addSheet($name, $activeSheet = false)
{ {
...@@ -131,9 +132,11 @@ class Exporter ...@@ -131,9 +132,11 @@ class Exporter
return $this; return $this;
} }
/** 根据名字sheet表名激化切换到 该表 /**
* 根据名字sheet表名激化切换到该表
* @param $name * @param $name
* @throws \PhpOffice\PhpSpreadsheet\Exception * @return $this
* @throws PhpSpreadsheetException
*/ */
public function setSheetByName($name) public function setSheetByName($name)
{ {
...@@ -161,8 +164,10 @@ class Exporter ...@@ -161,8 +164,10 @@ class Exporter
return $this->sheet->getHighestRow(); return $this->sheet->getHighestRow();
} }
/**设置从哪一列开始填充数据 /**
* 设置从哪一列开始填充数据
* @param string $char 默认 A列 * @param string $char 默认 A列
* @return Exporter
*/ */
public function setBeginColumnChar($char = "A") public function setBeginColumnChar($char = "A")
{ {
...@@ -170,8 +175,10 @@ class Exporter ...@@ -170,8 +175,10 @@ class Exporter
return $this; return $this;
} }
/**设置从哪一行开始填充数据 /**
* 设置从哪一行开始填充数据
* @param int $index 默认第一行 * @param int $index 默认第一行
* @return Exporter
*/ */
public function setBeginRowIndex($index = 1) public function setBeginRowIndex($index = 1)
{ {
...@@ -182,7 +189,8 @@ class Exporter ...@@ -182,7 +189,8 @@ class Exporter
/** /**
* 根据sheet索引切换到对应sheet * 根据sheet索引切换到对应sheet
* @param int $sheetIndex * @param int $sheetIndex
* @throws \PhpOffice\PhpSpreadsheet\Exception * @return Exporter
* @throws PhpSpreadsheetException
*/ */
public function setSheetByIndex(int $sheetIndex) public function setSheetByIndex(int $sheetIndex)
{ {
...@@ -198,7 +206,8 @@ class Exporter ...@@ -198,7 +206,8 @@ class Exporter
* @param array $keys 如果$data是原生数组支持直接根据$keys * @param array $keys 如果$data是原生数组支持直接根据$keys
* 提取数据,可多层提取例如 ['product_name','color.name','creator.user_status.name'] * 提取数据,可多层提取例如 ['product_name','color.name','creator.user_status.name']
* 支持默认值,为空时显示默认值 例如['product_name|默认值','color.name|无颜色'] * 支持默认值,为空时显示默认值 例如['product_name|默认值','color.name|无颜色']
* @throws \PhpOffice\PhpSpreadsheet\Exception * @return Exporter
* @throws PhpSpreadsheetException
*/ */
public function append(array $data, $keys = []) public function append(array $data, $keys = [])
{ {
...@@ -264,7 +273,6 @@ class Exporter ...@@ -264,7 +273,6 @@ class Exporter
/** /**
* 给表格加样式 * 给表格加样式
* @param $pRange * @param $pRange
* @throws \PhpOffice\PhpSpreadsheet\Exception
*/ */
public function applyStyle($pRange) public function applyStyle($pRange)
{ {
...@@ -308,7 +316,6 @@ class Exporter ...@@ -308,7 +316,6 @@ class Exporter
* @param int $downloadType 下载形式 支持 Exporter::DOWNLOAD_TYPE_STREAM Exporter::DOWNLOAD_TYPE_RETURN_FILE_PATH Exporter::DOWNLOAD_TYPE_SAVE_AND_DOWNLOAD * @param int $downloadType 下载形式 支持 Exporter::DOWNLOAD_TYPE_STREAM Exporter::DOWNLOAD_TYPE_RETURN_FILE_PATH Exporter::DOWNLOAD_TYPE_SAVE_AND_DOWNLOAD
* @param string $filename 下载的文件名 * @param string $filename 下载的文件名
* @return mixed | void * @return mixed | void
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
* @throws \Exception * @throws \Exception
*/ */
public function download(int $downloadType, $filename = "") public function download(int $downloadType, $filename = "")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment