@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | /** |
| 34 | 34 | * __construct |
| 35 | 35 | */ |
| 36 | - public function __construct($config=[]){ |
|
| 36 | + public function __construct($config = []) { |
|
| 37 | 37 | $this->setConfig($config); |
| 38 | 38 | |
| 39 | 39 | $this->spreadSheet = new Spreadsheet(); |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | * @param $config |
| 47 | 47 | * @return $this |
| 48 | 48 | */ |
| 49 | - public function setConfig($config){ |
|
| 49 | + public function setConfig($config) { |
|
| 50 | 50 | foreach ($config as $key => $value) { |
| 51 | 51 | if (property_exists($this, $key)) { |
| 52 | 52 | $this->$key = $value; |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | { |
| 65 | 65 | /** 实例化定义默认excel **/ |
| 66 | 66 | $this->spreadSheet->getProperties()->setCreator($this->creator)->setLastModifiedBy($this->creator); |
| 67 | - if($this->horizontalCenter){ |
|
| 67 | + if ($this->horizontalCenter) { |
|
| 68 | 68 | $this->spreadSheet->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); //默认水平居中 |
| 69 | 69 | $this->spreadSheet->getDefaultStyle()->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); //默认垂直居中 |
| 70 | 70 | $this->spreadSheet->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::VERTICAL_CENTER); //默认垂直居中 |
@@ -77,21 +77,21 @@ discard block |
||
| 77 | 77 | * @return $this |
| 78 | 78 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
| 79 | 79 | */ |
| 80 | - public function createWorkSheet($sheetName="Worksheet") |
|
| 80 | + public function createWorkSheet($sheetName = "Worksheet") |
|
| 81 | 81 | { |
| 82 | 82 | $this->sheetName = $sheetName; |
| 83 | 83 | /** @var workSheet */ |
| 84 | 84 | $this->workSheet = $this->greateWorkSheet($sheetName); |
| 85 | - if($this->workSheet == null){ |
|
| 86 | - if($this->sheetCount==1){ |
|
| 85 | + if ($this->workSheet == null) { |
|
| 86 | + if ($this->sheetCount == 1) { |
|
| 87 | 87 | $this->workSheet = $this->spreadSheet->getActiveSheet(); |
| 88 | - }else{ |
|
| 88 | + } else { |
|
| 89 | 89 | $this->workSheet = $this->spreadSheet->createSheet(); |
| 90 | 90 | } |
| 91 | - $this->sheetCount += 1;//总sheet数量 |
|
| 92 | - $this->workSheet->setTitle($sheetName);//设置sheet名称 |
|
| 91 | + $this->sheetCount += 1; //总sheet数量 |
|
| 92 | + $this->workSheet->setTitle($sheetName); //设置sheet名称 |
|
| 93 | 93 | } |
| 94 | - if(empty($this->mainTitle)){ |
|
| 94 | + if (empty($this->mainTitle)) { |
|
| 95 | 95 | $this->mainTitle = $sheetName; |
| 96 | 96 | } |
| 97 | 97 | |
@@ -118,13 +118,13 @@ discard block |
||
| 118 | 118 | public function generate() |
| 119 | 119 | { |
| 120 | 120 | /** 开启自动筛选 **/ |
| 121 | - if($this->autoFilter){ |
|
| 121 | + if ($this->autoFilter) { |
|
| 122 | 122 | $this->spreadSheet->getActiveSheet()->setAutoFilter( |
| 123 | 123 | $this->spreadSheet->getActiveSheet()->calculateWorksheetDimension() |
| 124 | 124 | ); |
| 125 | 125 | } |
| 126 | 126 | //文件存储 |
| 127 | - if(empty($this->fileName)){ |
|
| 127 | + if (empty($this->fileName)) { |
|
| 128 | 128 | $this->getFileName($this->sheetName); |
| 129 | 129 | } |
| 130 | 130 | return $this; |
@@ -135,8 +135,8 @@ discard block |
||
| 135 | 135 | * @param $file_name |
| 136 | 136 | * @return string |
| 137 | 137 | */ |
| 138 | - private function getFileName($sheetName){ |
|
| 139 | - $this->fileName = $sheetName.'_'.date('Y-m-d').'_'.rand(111,999).'.xlsx'; |
|
| 138 | + private function getFileName($sheetName) { |
|
| 139 | + $this->fileName = $sheetName.'_'.date('Y-m-d').'_'.rand(111, 999).'.xlsx'; |
|
| 140 | 140 | return $this->fileName; |
| 141 | 141 | } |
| 142 | 142 | |
@@ -146,18 +146,18 @@ discard block |
||
| 146 | 146 | * @return void |
| 147 | 147 | * @throws ExceptionAlias |
| 148 | 148 | */ |
| 149 | - public function download($filename=''){ |
|
| 150 | - if(empty($filename)){ |
|
| 149 | + public function download($filename = '') { |
|
| 150 | + if (empty($filename)) { |
|
| 151 | 151 | $filename = $this->fileName; |
| 152 | - }else{ |
|
| 152 | + } else { |
|
| 153 | 153 | $filename = $this->getFileName($filename); |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 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:*'); |
|
| 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 | 161 | header('Content-Type: application/vnd.ms-excel'); |
| 162 | 162 | header('Content-Disposition: attachment;filename="'.$filename); |
| 163 | 163 | header('Cache-Control: max-age=0'); |
@@ -174,12 +174,12 @@ discard block |
||
| 174 | 174 | * @return string |
| 175 | 175 | * @throws ExceptionAlias |
| 176 | 176 | */ |
| 177 | - public function save($filename='',$pathName=''): string |
|
| 177 | + public function save($filename = '', $pathName = ''): string |
|
| 178 | 178 | { |
| 179 | 179 | $pathName = $this->getPathName($pathName); |
| 180 | - if(empty($filename)){ |
|
| 180 | + if (empty($filename)) { |
|
| 181 | 181 | $filename = $this->fileName; |
| 182 | - }else{ |
|
| 182 | + } else { |
|
| 183 | 183 | $filename = $this->getFileName($filename); |
| 184 | 184 | } |
| 185 | 185 | FileTool::mkdir($pathName); |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | use tinymeng\tools\exception\StatusCode; |
| 13 | 13 | use tinymeng\tools\exception\TinymengException; |
| 14 | 14 | |
| 15 | -trait TWorkSheet{ |
|
| 15 | +trait TWorkSheet { |
|
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * sheet名称 |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | * 文件信息 |
| 46 | 46 | * @var array |
| 47 | 47 | */ |
| 48 | - private $fileTitle=[]; |
|
| 48 | + private $fileTitle = []; |
|
| 49 | 49 | |
| 50 | 50 | |
| 51 | 51 | /** |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | * @param $data |
| 72 | 72 | * @return $this |
| 73 | 73 | */ |
| 74 | - public function setData($data){ |
|
| 74 | + public function setData($data) { |
|
| 75 | 75 | $this->data = $data; |
| 76 | 76 | return $this; |
| 77 | 77 | } |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | * @param $data |
| 81 | 81 | * @return $this |
| 82 | 82 | */ |
| 83 | - public function getData(){ |
|
| 83 | + public function getData() { |
|
| 84 | 84 | return $this->data; |
| 85 | 85 | } |
| 86 | 86 | |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | $this->fileTitle = []; |
| 97 | 97 | $this->data = []; |
| 98 | 98 | $this->field = []; |
| 99 | - if($this->freezePane) $this->workSheet->freezePane($this->freezePane); //冻结窗格 |
|
| 99 | + if ($this->freezePane) $this->workSheet->freezePane($this->freezePane); //冻结窗格 |
|
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | /** |
@@ -106,9 +106,9 @@ discard block |
||
| 106 | 106 | * @throws TinymengException |
| 107 | 107 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
| 108 | 108 | */ |
| 109 | - public function setWorkSheetData($fileTitle,$data) |
|
| 109 | + public function setWorkSheetData($fileTitle, $data) |
|
| 110 | 110 | { |
| 111 | - if(isset($fileTitle['title_row']) || isset($fileTitle['group_left'])){ |
|
| 111 | + if (isset($fileTitle['title_row']) || isset($fileTitle['group_left'])) { |
|
| 112 | 112 | /** |
| 113 | 113 | * $fileTitle = [ |
| 114 | 114 | * 'title_row'=>1, |
@@ -118,10 +118,10 @@ discard block |
||
| 118 | 118 | * ], |
| 119 | 119 | * ]; |
| 120 | 120 | */ |
| 121 | - $this->title_row = $fileTitle['title_row']??1; |
|
| 122 | - $this->group_left = $fileTitle['group_left']??[]; |
|
| 123 | - $titleData = $fileTitle['title']??[]; |
|
| 124 | - }else{ |
|
| 121 | + $this->title_row = $fileTitle['title_row'] ?? 1; |
|
| 122 | + $this->group_left = $fileTitle['group_left'] ?? []; |
|
| 123 | + $titleData = $fileTitle['title'] ?? []; |
|
| 124 | + } else { |
|
| 125 | 125 | /** |
| 126 | 126 | * $fileTitle = [ |
| 127 | 127 | * '姓名'=>'name', |
@@ -131,14 +131,14 @@ discard block |
||
| 131 | 131 | } |
| 132 | 132 | // 根据字段映射方式处理 title |
| 133 | 133 | if ($this->fieldMappingMethod === ConstCode::FIELD_MAPPING_METHOD_FIELD_CORRESPONDING_NAME) { |
| 134 | - $this->fileTitle = array_flip($titleData);// 字段对应名称方式 - 需要将键值对调 |
|
| 135 | - }else{ |
|
| 136 | - $this->fileTitle = $titleData;// 名称对应字段方式 - 保持原样 |
|
| 134 | + $this->fileTitle = array_flip($titleData); // 字段对应名称方式 - 需要将键值对调 |
|
| 135 | + } else { |
|
| 136 | + $this->fileTitle = $titleData; // 名称对应字段方式 - 保持原样 |
|
| 137 | 137 | } |
| 138 | 138 | $this->data = $data; |
| 139 | 139 | |
| 140 | 140 | /** 设置第一行格式 */ |
| 141 | - if($this->mainTitleLine === true){ |
|
| 141 | + if ($this->mainTitleLine === true) { |
|
| 142 | 142 | $this->excelHeader(); |
| 143 | 143 | } |
| 144 | 144 | |
@@ -146,17 +146,17 @@ discard block |
||
| 146 | 146 | $this->excelTitle(); |
| 147 | 147 | |
| 148 | 148 | /** 获取列表里所有字段 **/ |
| 149 | - foreach ($this->fileTitle as $key => $val){ |
|
| 150 | - if(is_array($val)){ |
|
| 151 | - foreach ($val as $k => $v){ |
|
| 149 | + foreach ($this->fileTitle as $key => $val) { |
|
| 150 | + if (is_array($val)) { |
|
| 151 | + foreach ($val as $k => $v) { |
|
| 152 | 152 | $this->field[] = $v; |
| 153 | 153 | } |
| 154 | - }else{ |
|
| 154 | + } else { |
|
| 155 | 155 | $this->field[] = $val; |
| 156 | 156 | } |
| 157 | 157 | } |
| 158 | 158 | /** 查询结果赋值 **/ |
| 159 | - if(!empty($this->data)){ |
|
| 159 | + if (!empty($this->data)) { |
|
| 160 | 160 | $this->excelSetValue(); |
| 161 | 161 | } |
| 162 | 162 | return $this; |
@@ -167,33 +167,33 @@ discard block |
||
| 167 | 167 | * @author: Tinymeng <[email protected]> |
| 168 | 168 | * @time: 2022/2/22 11:43 |
| 169 | 169 | */ |
| 170 | - public function excelSetValue(){ |
|
| 171 | - if(empty($this->group_left)){ //判断左侧是否分组 |
|
| 172 | - foreach ($this->data as $key => $val){ |
|
| 170 | + public function excelSetValue() { |
|
| 171 | + if (empty($this->group_left)) { //判断左侧是否分组 |
|
| 172 | + foreach ($this->data as $key => $val) { |
|
| 173 | 173 | $this->excelSetCellValue($val); |
| 174 | 174 | } |
| 175 | - }else{ //根据设置分组字段进行分组 |
|
| 175 | + } else { //根据设置分组字段进行分组 |
|
| 176 | 176 | /** 数据分组 **/ |
| 177 | 177 | $data = []; |
| 178 | 178 | $group_left_count = count($this->group_left); |
| 179 | - if($group_left_count == 1){ |
|
| 180 | - foreach ($this->data as $k => $v){ |
|
| 179 | + if ($group_left_count == 1) { |
|
| 180 | + foreach ($this->data as $k => $v) { |
|
| 181 | 181 | $data[$v[$this->group_left[0]]][] = $v; |
| 182 | 182 | } |
| 183 | - foreach ($data as $k =>$v){ |
|
| 183 | + foreach ($data as $k =>$v) { |
|
| 184 | 184 | $data[$k] = [ |
| 185 | 185 | 'data' => $v, |
| 186 | 186 | 'count' => count($v) |
| 187 | 187 | ]; |
| 188 | 188 | } |
| 189 | 189 | $this->excelGroupLeft($data, $group_left_count); |
| 190 | - }elseif($group_left_count == 2){ |
|
| 190 | + }elseif ($group_left_count == 2) { |
|
| 191 | 191 | foreach ($this->data as $v) { |
| 192 | 192 | $data[$v[$this->group_left[0]]][$v[$this->group_left[1]]][] = $v; |
| 193 | 193 | } |
| 194 | 194 | $this->data = $this->arrayCount($data); |
| 195 | 195 | $this->excelGroupLeft($this->data, $group_left_count); |
| 196 | - }else{ |
|
| 196 | + } else { |
|
| 197 | 197 | throw new TinymengException(StatusCode::COMMON_PARAM_INVALID, |
| 198 | 198 | '左侧分组过多,导出失败!' |
| 199 | 199 | ); |
@@ -205,7 +205,7 @@ discard block |
||
| 205 | 205 | * @return void |
| 206 | 206 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
| 207 | 207 | */ |
| 208 | - public function excelHeader(){ |
|
| 208 | + public function excelHeader() { |
|
| 209 | 209 | $row = 1; |
| 210 | 210 | $this->workSheet->setCellValue('A'.$row, $this->mainTitle); |
| 211 | 211 | |
@@ -220,33 +220,33 @@ discard block |
||
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | // 使用实际的标题列数来合并单元格 |
| 223 | - $this->workSheet->mergeCells('A'.$row.':'.$this->cellName($titleCount-1).$row); |
|
| 223 | + $this->workSheet->mergeCells('A'.$row.':'.$this->cellName($titleCount - 1).$row); |
|
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | /** |
| 227 | 227 | * @return void |
| 228 | 228 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
| 229 | 229 | */ |
| 230 | - private function excelTitle(){ |
|
| 231 | - if($this->mainTitleLine === true){ |
|
| 232 | - $this->_row ++;//当前行数 |
|
| 230 | + private function excelTitle() { |
|
| 231 | + if ($this->mainTitleLine === true) { |
|
| 232 | + $this->_row++; //当前行数 |
|
| 233 | 233 | } |
| 234 | 234 | |
| 235 | 235 | $_merge = $this->cellName($this->_col); |
| 236 | 236 | foreach ($this->fileTitle as $key => $val) { |
| 237 | - if(!empty($this->titleHeight)) { |
|
| 238 | - $this->workSheet->getRowDimension($this->_col)->setRowHeight($this->titleHeight);//行高度 |
|
| 237 | + if (!empty($this->titleHeight)) { |
|
| 238 | + $this->workSheet->getRowDimension($this->_col)->setRowHeight($this->titleHeight); //行高度 |
|
| 239 | 239 | } |
| 240 | 240 | $rowName = $this->cellName($this->_col); |
| 241 | - $this->workSheet->getStyle($rowName . $this->_row)->getAlignment()->setWrapText(true);//自动换行 |
|
| 241 | + $this->workSheet->getStyle($rowName.$this->_row)->getAlignment()->setWrapText(true); //自动换行 |
|
| 242 | 242 | if (is_array($val)) { |
| 243 | 243 | $num = 1; |
| 244 | 244 | $_cols = $this->_col; |
| 245 | 245 | foreach ($val as $k => $v) { |
| 246 | - $this->workSheet->setCellValue($this->cellName($_cols) . ($this->_row+1), $k); |
|
| 247 | - if(!empty($this->titleWidth)) { |
|
| 246 | + $this->workSheet->setCellValue($this->cellName($_cols).($this->_row + 1), $k); |
|
| 247 | + if (!empty($this->titleWidth)) { |
|
| 248 | 248 | $this->workSheet->getColumnDimension($this->cellName($_cols))->setWidth($this->titleWidth); //列宽度 |
| 249 | - }else{ |
|
| 249 | + } else { |
|
| 250 | 250 | $this->workSheet->getColumnDimension($this->cellName($_cols))->setAutoSize(true); //自动计算宽度 |
| 251 | 251 | } |
| 252 | 252 | if ($num < count($val)) { |
@@ -255,23 +255,23 @@ discard block |
||
| 255 | 255 | } |
| 256 | 256 | $_cols++; |
| 257 | 257 | } |
| 258 | - $this->workSheet->mergeCells($_merge . $this->_row.':' . $this->cellName($this->_col) .$this->_row); |
|
| 259 | - $this->workSheet->setCellValue($_merge . $this->_row, $key);//设置值 |
|
| 258 | + $this->workSheet->mergeCells($_merge.$this->_row.':'.$this->cellName($this->_col).$this->_row); |
|
| 259 | + $this->workSheet->setCellValue($_merge.$this->_row, $key); //设置值 |
|
| 260 | 260 | } else { |
| 261 | 261 | if ($this->title_row != 1) { |
| 262 | - $this->workSheet->mergeCells($rowName . $this->_row.':' . $rowName . ($this->_row + $this->title_row - 1)); |
|
| 262 | + $this->workSheet->mergeCells($rowName.$this->_row.':'.$rowName.($this->_row + $this->title_row - 1)); |
|
| 263 | 263 | } |
| 264 | - $this->workSheet->setCellValue($rowName . $this->_row, $key);//设置值 |
|
| 265 | - if(!empty($this->titleWidth)){ |
|
| 264 | + $this->workSheet->setCellValue($rowName.$this->_row, $key); //设置值 |
|
| 265 | + if (!empty($this->titleWidth)) { |
|
| 266 | 266 | $this->workSheet->getColumnDimension($rowName)->setWidth($this->titleWidth); //列宽度 |
| 267 | - }else{ |
|
| 267 | + } else { |
|
| 268 | 268 | $this->workSheet->getColumnDimension($rowName)->setAutoSize(true); //自动计算宽度 |
| 269 | 269 | } |
| 270 | 270 | } |
| 271 | 271 | $this->_col++; |
| 272 | 272 | $_merge = $this->cellName($this->_col); |
| 273 | 273 | } |
| 274 | - $this->_row += $this->title_row;//当前行数 |
|
| 274 | + $this->_row += $this->title_row; //当前行数 |
|
| 275 | 275 | } |
| 276 | 276 | |
| 277 | 277 | /** |
@@ -282,60 +282,60 @@ discard block |
||
| 282 | 282 | private function excelSetCellValue($val) |
| 283 | 283 | { |
| 284 | 284 | //设置单元格行高 |
| 285 | - if(!empty($this->height)){ |
|
| 285 | + if (!empty($this->height)) { |
|
| 286 | 286 | $this->workSheet->getRowDimension($this->_row)->setRowHeight($this->height); |
| 287 | 287 | } |
| 288 | 288 | $_lie = 0; |
| 289 | - foreach ($this->field as $v){ |
|
| 289 | + foreach ($this->field as $v) { |
|
| 290 | 290 | $rowName = $this->cellName($_lie); |
| 291 | 291 | |
| 292 | - if(strpos($v,'.') !== false){ |
|
| 293 | - $v = explode('.',$v); |
|
| 292 | + if (strpos($v, '.') !== false) { |
|
| 293 | + $v = explode('.', $v); |
|
| 294 | 294 | $content = $val; |
| 295 | - for ($i=0;$i<count($v);$i++){ |
|
| 296 | - $content = $content[$v[$i]]??''; |
|
| 295 | + for ($i = 0; $i < count($v); $i++) { |
|
| 296 | + $content = $content[$v[$i]] ?? ''; |
|
| 297 | 297 | } |
| 298 | - }elseif($v == '_id'){ |
|
| 299 | - $content = $this->_row-$this->title_row;//自增序号列 |
|
| 300 | - }else{ |
|
| 301 | - $content = ($val[$v]??''); |
|
| 298 | + }elseif ($v == '_id') { |
|
| 299 | + $content = $this->_row - $this->title_row; //自增序号列 |
|
| 300 | + } else { |
|
| 301 | + $content = ($val[$v] ?? ''); |
|
| 302 | 302 | } |
| 303 | - if(is_array($content) && isset($content['type']) && isset($content['content'])){ |
|
| 304 | - if($content['type'] == 'image'){ |
|
| 303 | + if (is_array($content) && isset($content['type']) && isset($content['content'])) { |
|
| 304 | + if ($content['type'] == 'image') { |
|
| 305 | 305 | $path = $this->verifyFile($content['content']); |
| 306 | 306 | $drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing(); |
| 307 | 307 | $drawing->setPath($path); |
| 308 | - if(!empty($content['height'])) { |
|
| 308 | + if (!empty($content['height'])) { |
|
| 309 | 309 | $drawing->setHeight($content['height']); |
| 310 | 310 | } |
| 311 | - if(!empty($content['width'])) { |
|
| 312 | - $drawing->setWidth($content['width']);//只设置高,宽会自适应,如果设置宽后,高则失效 |
|
| 311 | + if (!empty($content['width'])) { |
|
| 312 | + $drawing->setWidth($content['width']); //只设置高,宽会自适应,如果设置宽后,高则失效 |
|
| 313 | 313 | } |
| 314 | - if(!empty($content['offsetX'])) { |
|
| 315 | - $drawing->setOffsetX($content['offsetX']);//设置X方向偏移量 |
|
| 314 | + if (!empty($content['offsetX'])) { |
|
| 315 | + $drawing->setOffsetX($content['offsetX']); //设置X方向偏移量 |
|
| 316 | 316 | } |
| 317 | - if(!empty($content['offsetY'])) { |
|
| 318 | - $drawing->setOffsetY($content['offsetY']);//设置Y方向偏移量 |
|
| 317 | + if (!empty($content['offsetY'])) { |
|
| 318 | + $drawing->setOffsetY($content['offsetY']); //设置Y方向偏移量 |
|
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | $drawing->setCoordinates($rowName.$this->_row); |
| 322 | 322 | $drawing->setWorksheet($this->workSheet); |
| 323 | 323 | } |
| 324 | - }else { |
|
| 325 | - $content = $this->formatValue($content);//格式化数据 |
|
| 326 | - if (is_numeric($content)){ |
|
| 327 | - if($this->autoDataType && strlen($content)<11){ |
|
| 328 | - $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_NUMERIC); |
|
| 329 | - }else{ |
|
| 330 | - $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_STRING2); |
|
| 324 | + } else { |
|
| 325 | + $content = $this->formatValue($content); //格式化数据 |
|
| 326 | + if (is_numeric($content)) { |
|
| 327 | + if ($this->autoDataType && strlen($content) < 11) { |
|
| 328 | + $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content, DataType::TYPE_NUMERIC); |
|
| 329 | + } else { |
|
| 330 | + $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content, DataType::TYPE_STRING2); |
|
| 331 | 331 | } |
| 332 | - }else{ |
|
| 333 | - $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content,DataType::TYPE_STRING2); |
|
| 332 | + } else { |
|
| 333 | + $this->workSheet->setCellValueExplicit($rowName.$this->_row, $content, DataType::TYPE_STRING2); |
|
| 334 | 334 | } |
| 335 | 335 | } |
| 336 | - $_lie ++; |
|
| 336 | + $_lie++; |
|
| 337 | 337 | } |
| 338 | - $this->_row ++; |
|
| 338 | + $this->_row++; |
|
| 339 | 339 | } |
| 340 | 340 | |
| 341 | 341 | /** |
@@ -348,39 +348,39 @@ discard block |
||
| 348 | 348 | { |
| 349 | 349 | // 获取分组字段在field中的实际位置 |
| 350 | 350 | $group_field_positions = []; |
| 351 | - foreach($this->group_left as $group_field){ |
|
| 351 | + foreach ($this->group_left as $group_field) { |
|
| 352 | 352 | $position = array_search($group_field, $this->field); |
| 353 | - if($position !== false){ |
|
| 353 | + if ($position !== false) { |
|
| 354 | 354 | $group_field_positions[] = $position; |
| 355 | 355 | } |
| 356 | 356 | } |
| 357 | 357 | |
| 358 | - if(empty($group_field_positions)){ |
|
| 358 | + if (empty($group_field_positions)) { |
|
| 359 | 359 | throw new TinymengException(StatusCode::COMMON_PARAM_INVALID, '分组字段未在标题中定义'); |
| 360 | 360 | } |
| 361 | 361 | |
| 362 | 362 | $group_start = $this->_row; |
| 363 | - foreach ($data as $key => $val){ |
|
| 363 | + foreach ($data as $key => $val) { |
|
| 364 | 364 | // 第一级分组的合并单元格 |
| 365 | 365 | $rowName = $this->cellName($group_field_positions[0]); // 使用第一个分组字段的实际位置 |
| 366 | - $coordinate = $rowName.$this->_row.':'.$rowName.($this->_row+$val['count']-1); |
|
| 366 | + $coordinate = $rowName.$this->_row.':'.$rowName.($this->_row + $val['count'] - 1); |
|
| 367 | 367 | $this->workSheet->mergeCells($coordinate); |
| 368 | 368 | $this->workSheet->setCellValue($rowName.$this->_row, $key); |
| 369 | 369 | |
| 370 | - if($group_left_count == 1){ |
|
| 371 | - foreach ($val['data'] as $data){ |
|
| 370 | + if ($group_left_count == 1) { |
|
| 371 | + foreach ($val['data'] as $data) { |
|
| 372 | 372 | $this->excelSetCellValue($data); |
| 373 | 373 | } |
| 374 | - }else{ |
|
| 374 | + } else { |
|
| 375 | 375 | $sub_group_start = $this->_row; |
| 376 | 376 | $rowName = $this->cellName($group_field_positions[1]); // 使用第二个分组字段的实际位置 |
| 377 | 377 | |
| 378 | - foreach ($val['data'] as $k => $v){ |
|
| 379 | - $coordinate = $rowName.$sub_group_start.':'.$rowName.($sub_group_start+$v['count']-1); |
|
| 378 | + foreach ($val['data'] as $k => $v) { |
|
| 379 | + $coordinate = $rowName.$sub_group_start.':'.$rowName.($sub_group_start + $v['count'] - 1); |
|
| 380 | 380 | $this->workSheet->mergeCells($coordinate); |
| 381 | 381 | $this->workSheet->setCellValue($rowName.$sub_group_start, $k); |
| 382 | 382 | |
| 383 | - foreach ($v['data'] as $data){ |
|
| 383 | + foreach ($v['data'] as $data) { |
|
| 384 | 384 | $this->excelSetCellValue($data); |
| 385 | 385 | } |
| 386 | 386 | |
@@ -399,13 +399,13 @@ discard block |
||
| 399 | 399 | * @param array $data 二维数组原始数据 |
| 400 | 400 | * @return array |
| 401 | 401 | */ |
| 402 | - private function arrayCount($data=[]) |
|
| 402 | + private function arrayCount($data = []) |
|
| 403 | 403 | { |
| 404 | - foreach ($data as $key => $val){ |
|
| 404 | + foreach ($data as $key => $val) { |
|
| 405 | 405 | $num = 0; |
| 406 | - foreach ($val as $k => $v){ |
|
| 406 | + foreach ($val as $k => $v) { |
|
| 407 | 407 | $sub_num = count($v); |
| 408 | - $num = $num+$sub_num; |
|
| 408 | + $num = $num + $sub_num; |
|
| 409 | 409 | $val[$k] = [ |
| 410 | 410 | 'count' => $sub_num, |
| 411 | 411 | 'data' => $v |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; |
| 13 | 13 | use tinymeng\tools\FileTool; |
| 14 | 14 | |
| 15 | -trait SpreadSheet{ |
|
| 15 | +trait SpreadSheet { |
|
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * sheet名称 |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | * 文件信息 |
| 30 | 30 | * @var array |
| 31 | 31 | */ |
| 32 | - private $fileTitle=[]; |
|
| 32 | + private $fileTitle = []; |
|
| 33 | 33 | |
| 34 | 34 | /** |
| 35 | 35 | * 标题占用行数 |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | */ |
| 91 | 91 | public $image_path = '/images'; |
| 92 | 92 | |
| 93 | - public function setTitle($title){ |
|
| 93 | + public function setTitle($title) { |
|
| 94 | 94 | $this->title = $title; |
| 95 | 95 | $this->getTitleFields(); |
| 96 | 96 | return $this; |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | * @param $value |
| 101 | 101 | * @return $this |
| 102 | 102 | */ |
| 103 | - public function setRelativePath($value){ |
|
| 103 | + public function setRelativePath($value) { |
|
| 104 | 104 | $this->relative_path = $value; |
| 105 | 105 | return $this; |
| 106 | 106 | } |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | * @param $value |
| 110 | 110 | * @return $this |
| 111 | 111 | */ |
| 112 | - public function setImagePath($value){ |
|
| 112 | + public function setImagePath($value) { |
|
| 113 | 113 | $this->image_path = $value; |
| 114 | 114 | return $this; |
| 115 | 115 | } |
@@ -121,40 +121,40 @@ discard block |
||
| 121 | 121 | * @author: Tinymeng <[email protected]> |
| 122 | 122 | * @time: 2022/2/22 11:30 |
| 123 | 123 | */ |
| 124 | - public function getExcelData(){ |
|
| 124 | + public function getExcelData() { |
|
| 125 | 125 | /* 循环读取每个单元格的数据 */ |
| 126 | 126 | $result = []; |
| 127 | - $dataRow = $this->titleFieldsRow+1; |
|
| 127 | + $dataRow = $this->titleFieldsRow + 1; |
|
| 128 | 128 | |
| 129 | 129 | //行数循环 |
| 130 | - for ($row = $dataRow; $row <= $this->rowCount; $row++){ |
|
| 131 | - $rowFlog = false;//行是否有内容(过滤空行) |
|
| 130 | + for ($row = $dataRow; $row <= $this->rowCount; $row++) { |
|
| 131 | + $rowFlog = false; //行是否有内容(过滤空行) |
|
| 132 | 132 | //列数循环 , 列数是以A列开始 |
| 133 | 133 | $data = []; |
| 134 | - foreach ($this->cellName as $column){ |
|
| 134 | + foreach ($this->cellName as $column) { |
|
| 135 | 135 | $cell = $this->workSheet->getCell($column.$row); |
| 136 | 136 | $value = trim($cell->getFormattedValue()); |
| 137 | - if(isset($this->title_fields[$column])){ |
|
| 137 | + if (isset($this->title_fields[$column])) { |
|
| 138 | 138 | $data[$this->title_fields[$column]] = $value; |
| 139 | - if(!empty($value)) $rowFlog = true;//有内容 |
|
| 139 | + if (!empty($value)) $rowFlog = true; //有内容 |
|
| 140 | 140 | } |
| 141 | 141 | } |
| 142 | - if($rowFlog) $result[] = $data; |
|
| 142 | + if ($rowFlog) $result[] = $data; |
|
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | /* |
| 146 | 146 | * 读取表格图片数据 |
| 147 | 147 | * (如果为空右击图片转为浮动图片) |
| 148 | 148 | */ |
| 149 | - $image_filename_prefix = time().rand(100,999).$this->sheet; |
|
| 149 | + $image_filename_prefix = time().rand(100, 999).$this->sheet; |
|
| 150 | 150 | foreach ($this->workSheet->getDrawingCollection() as $drawing) { |
| 151 | 151 | /**@var $drawing Drawing* */ |
| 152 | 152 | list($column, $row) = Coordinate::coordinateFromString($drawing->getCoordinates()); |
| 153 | - $image_filename = "/{$image_filename_prefix}-" . $drawing->getCoordinates(); |
|
| 153 | + $image_filename = "/{$image_filename_prefix}-".$drawing->getCoordinates(); |
|
| 154 | 154 | $image_suffix = $this->saveImage($drawing, $image_filename); |
| 155 | - $image_name = ltrim($this->relative_path, '/') . "{$image_filename}.{$image_suffix}"; |
|
| 156 | - if(isset($this->title_fields[$column])) { |
|
| 157 | - $result[$row-($this->titleFieldsRow+1)][$this->title_fields[$column]] = $image_name; |
|
| 155 | + $image_name = ltrim($this->relative_path, '/')."{$image_filename}.{$image_suffix}"; |
|
| 156 | + if (isset($this->title_fields[$column])) { |
|
| 157 | + $result[$row - ($this->titleFieldsRow + 1)][$this->title_fields[$column]] = $image_name; |
|
| 158 | 158 | } |
| 159 | 159 | } |
| 160 | 160 | return $result; |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | * @author: Tinymeng <[email protected]> |
| 167 | 167 | * @time: 2022/2/22 11:30 |
| 168 | 168 | */ |
| 169 | - public function getTitle(){ |
|
| 169 | + public function getTitle() { |
|
| 170 | 170 | return $this->title; |
| 171 | 171 | } |
| 172 | 172 | |
@@ -176,21 +176,21 @@ discard block |
||
| 176 | 176 | * @author: Tinymeng <[email protected]> |
| 177 | 177 | * @time: 2022/2/22 11:30 |
| 178 | 178 | */ |
| 179 | - public function getTitleFields(){ |
|
| 179 | + public function getTitleFields() { |
|
| 180 | 180 | $title = $this->getTitle(); |
| 181 | 181 | |
| 182 | 182 | $row = $this->titleFieldsRow; |
| 183 | 183 | $titleDataArr = []; |
| 184 | 184 | |
| 185 | - foreach ($this->cellName as $column){ |
|
| 185 | + foreach ($this->cellName as $column) { |
|
| 186 | 186 | $value = trim($this->workSheet->getCell($column.$row)->getValue()); |
| 187 | - if(!empty($value)){ |
|
| 187 | + if (!empty($value)) { |
|
| 188 | 188 | $titleDataArr[$value] = $column; |
| 189 | 189 | } |
| 190 | 190 | } |
| 191 | 191 | $title_fields = []; |
| 192 | 192 | foreach ($title as $key=>$value) { |
| 193 | - if(isset($titleDataArr[$key])){ |
|
| 193 | + if (isset($titleDataArr[$key])) { |
|
| 194 | 194 | $title_fields[$titleDataArr[$key]] = $value; |
| 195 | 195 | } |
| 196 | 196 | } |
@@ -220,13 +220,13 @@ discard block |
||
| 220 | 220 | $real_extension = 'jpg'; |
| 221 | 221 | $image_filename .= '.'.$real_extension; |
| 222 | 222 | $source = imagecreatefromjpeg($drawing->getPath()); |
| 223 | - imagejpeg($source, $this->image_path . $image_filename); |
|
| 223 | + imagejpeg($source, $this->image_path.$image_filename); |
|
| 224 | 224 | break; |
| 225 | 225 | case 'image/gif': |
| 226 | 226 | $real_extension = 'gif'; |
| 227 | 227 | $image_filename .= '.'.$real_extension; |
| 228 | 228 | $source = imagecreatefromgif($drawing->getPath()); |
| 229 | - imagegif($source, $this->image_path . $image_filename); |
|
| 229 | + imagegif($source, $this->image_path.$image_filename); |
|
| 230 | 230 | break; |
| 231 | 231 | case 'image/png': |
| 232 | 232 | $real_extension = 'png'; |
@@ -235,7 +235,7 @@ discard block |
||
| 235 | 235 | // 保持透明度设置 |
| 236 | 236 | imagealphablending($source, false); |
| 237 | 237 | imagesavealpha($source, true); |
| 238 | - imagepng($source, $this->image_path . $image_filename); |
|
| 238 | + imagepng($source, $this->image_path.$image_filename); |
|
| 239 | 239 | break; |
| 240 | 240 | default: |
| 241 | 241 | throw new Exception('image format error!'); |