majiameng /
spreadsheet-php
| 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; |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 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 | |||||
| 95 | /** 初始化当前workSheet */ |
||||
| 96 | $this->initWorkSheet(); |
||||
| 97 | |||||
| 98 | return $this; |
||||
| 99 | } |
||||
| 100 | |||||
| 101 | /** |
||||
| 102 | * @param $sheetName |
||||
| 103 | * @return workSheet |
||||
|
0 ignored issues
–
show
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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 104 | */ |
||||
| 105 | public function greateWorkSheet($sheetName) |
||||
| 106 | { |
||||
| 107 | /** @var workSheet */ |
||||
| 108 | return $this->spreadSheet->getSheetByName($sheetName); |
||||
|
0 ignored issues
–
show
|
|||||
| 109 | } |
||||
| 110 | |||||
| 111 | /** |
||||
| 112 | * 生成excel表格 |
||||
| 113 | * @return $this |
||||
| 114 | */ |
||||
| 115 | public function generate() |
||||
| 116 | { |
||||
| 117 | /** 开启自动筛选 **/ |
||||
| 118 | if($this->autoFilter){ |
||||
| 119 | $this->spreadSheet->getActiveSheet()->setAutoFilter( |
||||
| 120 | $this->spreadSheet->getActiveSheet()->calculateWorksheetDimension() |
||||
| 121 | ); |
||||
| 122 | } |
||||
| 123 | //文件存储 |
||||
| 124 | if(empty($this->fileName)){ |
||||
| 125 | $this->getFileName($this->sheetName); |
||||
| 126 | } |
||||
| 127 | return $this; |
||||
| 128 | } |
||||
| 129 | |||||
| 130 | |||||
| 131 | /** |
||||
| 132 | * @param $file_name |
||||
| 133 | * @return string |
||||
| 134 | */ |
||||
| 135 | private function getFileName($sheetName){ |
||||
| 136 | $this->fileName = $sheetName.'_'.date('Y-m-d').'_'.rand(111,999).'.xlsx'; |
||||
| 137 | return $this->fileName; |
||||
| 138 | } |
||||
| 139 | |||||
| 140 | /** |
||||
| 141 | * 文件下载 |
||||
| 142 | * @param $filename |
||||
| 143 | * @return void |
||||
| 144 | * @throws ExceptionAlias |
||||
| 145 | */ |
||||
| 146 | public function download($filename=''){ |
||||
| 147 | if(empty($filename)){ |
||||
| 148 | $filename = $this->fileName; |
||||
| 149 | }else{ |
||||
| 150 | $filename = $this->getFileName($filename); |
||||
| 151 | } |
||||
| 152 | |||||
| 153 | /** 输出下载 **/ |
||||
| 154 | ob_end_clean();//清除缓冲区,避免乱码 |
||||
| 155 | header( 'Access-Control-Allow-Headers:responsetype,content-type,usertoken'); |
||||
| 156 | header( 'Access-Control-Allow-Methods:GET,HEAD,PUT,POST,DELETE,PATCH'); |
||||
| 157 | header( 'Access-Control-Allow-Origin:*'); |
||||
| 158 | header('Content-Type: application/vnd.ms-excel'); |
||||
| 159 | header('Content-Disposition: attachment;filename="'.$filename); |
||||
| 160 | header('Cache-Control: max-age=0'); |
||||
| 161 | |||||
| 162 | $objWrite = IOFactory::createWriter($this->spreadSheet, 'Xlsx'); |
||||
| 163 | $objWrite->save('php://output'); |
||||
| 164 | exit(); |
||||
|
0 ignored issues
–
show
|
|||||
| 165 | } |
||||
| 166 | |||||
| 167 | /** |
||||
| 168 | * 文件存储 |
||||
| 169 | * @param $filename |
||||
| 170 | * @param $pathName |
||||
| 171 | * @return string |
||||
| 172 | * @throws ExceptionAlias |
||||
| 173 | */ |
||||
| 174 | public function save($filename='',$pathName=''): string |
||||
| 175 | { |
||||
| 176 | $pathName = $this->getPathName($pathName); |
||||
| 177 | if(empty($filename)){ |
||||
| 178 | $filename = $this->fileName; |
||||
| 179 | }else{ |
||||
| 180 | $filename = $this->getFileName($filename); |
||||
| 181 | } |
||||
| 182 | FileTool::mkdir($pathName); |
||||
|
0 ignored issues
–
show
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
Loading history...
|
|||||
| 183 | $objWrite = IOFactory::createWriter($this->spreadSheet, 'Xlsx'); |
||||
| 184 | $objWrite->save($pathName.$filename); |
||||
| 185 | return $pathName.$filename; |
||||
| 186 | } |
||||
| 187 | |||||
| 188 | } |
||||
| 189 |