Commit 78d2df4d authored by 王源's avatar 王源
Browse files

优化

parent 8306aedf
Loading
Loading
Loading
Loading
+21 −14
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@
namespace Meibuyu\Micro\Tools;

use Hyperf\Contract\ConfigInterface;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
@@ -19,6 +20,7 @@ use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class Exporter
{

    /**
     * 以字节流的形式下载
     */
@@ -57,8 +59,7 @@ class Exporter
     * @param string $tempFilePath 模板文件地址
     * @param string $name 名称
     * @param int $sheetIndex
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
     * @throws PhpSpreadsheetException
     * @throws \Exception
     */
    public function __construct(int $export_type, $tempFilePath = "", $name = "export_data", $sheetIndex = 0)
@@ -67,7 +68,6 @@ class Exporter
        $this->rootPath = $this->config->get('server.settings.document_root', BASE_PATH . '/public');
        if ($tempFilePath) {
            $tempFilePath = $this->rootPath . "/" . $tempFilePath;
            //print_r($tempFilePath);
            if (file_exists($tempFilePath)) {
                $this->fileType = ucfirst(strtolower(pathinfo($tempFilePath, PATHINFO_EXTENSION)));
                $reader = IOFactory::createReader($this->fileType)->load($tempFilePath);
@@ -100,8 +100,8 @@ class Exporter

    /**
     * 设置非保护区间,调用此方法全表将自动保护
     * @param string $unprotectRange 例如"A1:B1";从A列B列第一行到数据结束行不需要保护,
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @param string $unprotectRange 例如"A1:B1";从A列B列第一行到数据结束行不需要保护
     * @return $this
     */
    public function setUnprotectRange($unprotectRange = "")
    {
@@ -119,7 +119,8 @@ class Exporter
     * 往Excel里面新增一张sheet表
     * @param string $name sheet的名字
     * @param bool $activeSheet 是否激货并切换到当前sheet 默认否
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @return $this
     * @throws PhpSpreadsheetException
     */
    public function addSheet($name, $activeSheet = false)
    {
@@ -131,9 +132,11 @@ class Exporter
        return $this;
    }

    /** 根据名字sheet表名激化切换到 该表
    /**
     * 根据名字sheet表名激化切换到该表
     * @param $name
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @return $this
     * @throws PhpSpreadsheetException
     */
    public function setSheetByName($name)
    {
@@ -161,8 +164,10 @@ class Exporter
        return $this->sheet->getHighestRow();
    }

    /**设置从哪一列开始填充数据
    /**
     * 设置从哪一列开始填充数据
     * @param string $char 默认 A列
     * @return Exporter
     */
    public function setBeginColumnChar($char = "A")
    {
@@ -170,8 +175,10 @@ class Exporter
        return $this;
    }

    /**设置从哪一行开始填充数据
    /**
     * 设置从哪一行开始填充数据
     * @param int $index 默认第一行
     * @return Exporter
     */
    public function setBeginRowIndex($index = 1)
    {
@@ -182,7 +189,8 @@ class Exporter
    /**
     * 根据sheet索引切换到对应sheet
     * @param int $sheetIndex
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @return Exporter
     * @throws PhpSpreadsheetException
     */
    public function setSheetByIndex(int $sheetIndex)
    {
@@ -198,7 +206,8 @@ class Exporter
     * @param array $keys 如果$data是原生数组支持直接根据$keys
     * 提取数据,可多层提取例如 ['product_name','color.name','creator.user_status.name']
     * 支持默认值,为空时显示默认值 例如['product_name|默认值','color.name|无颜色']
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @return Exporter
     * @throws PhpSpreadsheetException
     */
    public function append(array $data, $keys = [])
    {
@@ -264,7 +273,6 @@ class Exporter
    /**
     * 给表格加样式
     * @param $pRange
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     */
    public function applyStyle($pRange)
    {
@@ -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 string $filename 下载的文件名
     * @return mixed | void
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     * @throws \Exception
     */
    public function download(int $downloadType, $filename = "")