Export::generate()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 13
rs 10
1
<?php
2
/**
3
 * @name: 报表导出查询
4
 * @Created by IntelliJ IDEA
5
 * @author: tinymeng
6
 * @file: Export.php
7
 * @Date: 2018/7/4 10:15
8
 */
9
namespace tinymeng\spreadsheet\Gateways;
10
11
use PhpOffice\PhpSpreadsheet\IOFactory;
12
use PhpOffice\PhpSpreadsheet\Spreadsheet;
13
use PhpOffice\PhpSpreadsheet\Style\Alignment;
14
use PhpOffice\PhpSpreadsheet\Writer\Exception as ExceptionAlias;
15
use tinymeng\spreadsheet\Connector\Gateway;
16
use tinymeng\tools\FileTool;
17
use tinymeng\spreadsheet\Excel\TWorkSheet;
18
use tinymeng\spreadsheet\Util\TConfig;
19
20
class Export extends Gateway {
21
22
    /**
23
     * TConfig
24
     */
25
    use TConfig;
26
27
    /**
28
     * TWorkSheet
29
     */
30
    use TWorkSheet;
31
32
33
    /**
34
     * __construct
35
     */
36
    public function __construct($config=[]){
37
        $this->setConfig($config);
38
39
        $this->spreadSheet = new Spreadsheet();
40
        //初始化表格格式
41
        $this->initSpreadSheet();
42
        return $this;
43
    }
44
45
    /**
46
     * @param $config
47
     * @return $this
48
     */
49
    public function setConfig($config){
50
        foreach ($config as $key => $value) {
51
            if (property_exists($this, $key)) {
52
                $this->$key = $value;
53
            }
54
        }
55
        return $this;
56
    }
57
58
    /**
59
     * 初始化表格格式
60
     * initSpreadSheet
61
     * @return void
62
     */
63
    public function initSpreadSheet()
64
    {
65
        /** 实例化定义默认excel **/
66
        $this->spreadSheet->getProperties()->setCreator($this->creator)->setLastModifiedBy($this->creator);
67
        if($this->horizontalCenter){
68
            $this->spreadSheet->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); //默认水平居中
69
            $this->spreadSheet->getDefaultStyle()->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); //默认垂直居中
70
            $this->spreadSheet->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::VERTICAL_CENTER); //默认垂直居中
71
        }
72
    }
73
74
    /**
75
     * 创建新的sheet
76
     * @param $sheetName
77
     * @return $this
78
     * @throws \PhpOffice\PhpSpreadsheet\Exception
79
     */
80
    public function createWorkSheet($sheetName="Worksheet")
81
    {
82
        $this->sheetName = $sheetName;
83
        /** @var workSheet */
84
        $this->workSheet = $this->greateWorkSheet($sheetName);
85
        if($this->workSheet == null){
86
            if($this->sheetCount==1){
87
                $this->workSheet = $this->spreadSheet->getActiveSheet();
88
            }else{
89
                $this->workSheet = $this->spreadSheet->createSheet();
90
            }
91
            $this->sheetCount += 1;//总sheet数量
92
            $this->workSheet->setTitle($sheetName);//设置sheet名称
93
        }
94
        if(empty($this->mainTitle)){
95
            $this->mainTitle = $sheetName;
96
        }
97
98
        /** 初始化当前workSheet */
99
        $this->initWorkSheet();
100
101
        return $this;
102
    }
103
104
    /**
105
     * @param $sheetName
106
     * @return workSheet
0 ignored issues
show
Bug introduced by
The type tinymeng\spreadsheet\Gateways\workSheet was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
107
     */
108
    public function greateWorkSheet($sheetName)
109
    {
110
        /** @var workSheet */
111
        return $this->spreadSheet->getSheetByName($sheetName);
112
    }
113
114
    /**
115
     * 生成excel表格
116
     * @return $this
117
     */
118
    public function generate()
119
    {
120
        /** 开启自动筛选 **/
121
        if($this->autoFilter){
122
            $this->spreadSheet->getActiveSheet()->setAutoFilter(
123
                $this->spreadSheet->getActiveSheet()->calculateWorksheetDimension()
124
            );
125
        }
126
        //文件存储
127
        if(empty($this->fileName)){
128
            $this->getFileName($this->sheetName);
129
        }
130
        return $this;
131
    }
132
133
134
    /**
135
     * @param $file_name
136
     * @return string
137
     */
138
    private function getFileName($sheetName){
139
        $this->fileName = $sheetName.'_'.date('Y-m-d').'_'.rand(111,999).'.xlsx';
140
        return $this->fileName;
141
    }
142
143
    /**
144
     * 文件下载
145
     * @param $filename
146
     * @return void
147
     * @throws ExceptionAlias
148
     */
149
    public function download($filename=''){
150
        if(empty($filename)){
151
            $filename = $this->fileName;
152
        }else{
153
            $filename = $this->getFileName($filename);
154
        }
155
156
        /** 输出下载 **/
157
        ob_end_clean();//清除缓冲区,避免乱码
158
        header( 'Access-Control-Allow-Headers:responsetype,content-type,usertoken');
159
        header( 'Access-Control-Allow-Methods:GET,HEAD,PUT,POST,DELETE,PATCH');
160
        header( 'Access-Control-Allow-Origin:*');
161
        header('Content-Type: application/vnd.ms-excel');
162
        header('Content-Disposition: attachment;filename="'.$filename);
163
        header('Cache-Control: max-age=0');
164
165
        $objWrite = IOFactory::createWriter($this->spreadSheet, 'Xlsx');
166
        $objWrite->save('php://output');
167
        exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
168
    }
169
170
    /**
171
     * 文件存储
172
     * @param $filename
173
     * @param $pathName
174
     * @return string
175
     * @throws ExceptionAlias
176
     */
177
    public function save($filename='',$pathName=''): string
178
    {
179
        $pathName = $this->getPathName($pathName);
180
        if(empty($filename)){
181
            $filename = $this->fileName;
182
        }else{
183
            $filename = $this->getFileName($filename);
184
        }
185
        FileTool::mkdir($pathName);
0 ignored issues
show
Bug introduced by
It seems like $pathName can also be of type boolean; however, parameter $dir_name of tinymeng\tools\FileTool::mkdir() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

185
        FileTool::mkdir(/** @scrutinizer ignore-type */ $pathName);
Loading history...
186
        $objWrite = IOFactory::createWriter($this->spreadSheet, 'Xlsx');
187
        $objWrite->save($pathName.$filename);
188
        return $pathName.$filename;
189
    }
190
191
}
192