majiameng /
spreadsheet-php
| 1 | <?php |
||
| 2 | namespace tinymeng\spreadsheet\Connector; |
||
| 3 | |||
| 4 | use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
||
| 5 | use PhpOffice\PhpSpreadsheet\IOFactory; |
||
| 6 | use PhpOffice\PhpSpreadsheet\Spreadsheet; |
||
| 7 | use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Gateway |
||
| 11 | */ |
||
| 12 | abstract class Gateway implements GatewayInterface |
||
| 13 | { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var Spreadsheet |
||
| 17 | */ |
||
| 18 | public $spreadSheet; |
||
| 19 | /** |
||
| 20 | * @var Worksheet |
||
| 21 | */ |
||
| 22 | public $workSheet; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * 是否格式化内容 |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | public $format = true; |
||
| 29 | /** |
||
| 30 | * 是否格式化内容 |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | public $format_date = 'Y-m-d H:i:s'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var |
||
| 37 | */ |
||
| 38 | public $params; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * 数字转英文列 |
||
| 42 | * @param $columnIndex |
||
| 43 | * @return string |
||
| 44 | * @author: Tinymeng <[email protected]> |
||
| 45 | * @time: 2022/4/24 17:35 |
||
| 46 | */ |
||
| 47 | protected function cellName($columnIndex){ |
||
| 48 | $columnIndex =(int)$columnIndex+1; |
||
| 49 | static $indexCache = []; |
||
| 50 | |||
| 51 | if (!isset($indexCache[$columnIndex])) { |
||
| 52 | $indexValue = $columnIndex; |
||
| 53 | $base26 = null; |
||
| 54 | do { |
||
| 55 | $characterValue = ($indexValue % 26) ?: 26; |
||
| 56 | $indexValue = ($indexValue - $characterValue) / 26; |
||
| 57 | $base26 = chr($characterValue + 64) . ($base26 ?: ''); |
||
| 58 | } while ($indexValue > 0); |
||
| 59 | $indexCache[$columnIndex] = $base26; |
||
| 60 | } |
||
| 61 | |||
| 62 | return $indexCache[$columnIndex]; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * 格式化value |
||
| 67 | * @param string |
||
| 68 | * @return mixed |
||
| 69 | */ |
||
| 70 | protected function formatValue($v){ |
||
| 71 | if($this->format === false) return $v; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 72 | if(is_numeric($v) && strlen($v)===10){ |
||
| 73 | $v = date($this->format_date,$v);//时间戳转时间格式 |
||
| 74 | } |
||
| 75 | return $v; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * 根据最后一列获取所有列数组 |
||
| 80 | * @param $lastCell |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | protected function getCellName($lastCell){ |
||
| 84 | $cellName = array(); |
||
| 85 | for($i='A'; $i!=$lastCell; $i++) { |
||
| 86 | $cellName[] = $i; |
||
| 87 | } |
||
| 88 | $cellName[] = $i++; |
||
| 89 | return $cellName; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param $path |
||
| 94 | * @param bool $verifyFile |
||
| 95 | * @param null $zip |
||
|
0 ignored issues
–
show
|
|||
| 96 | * @return array|string|string[] |
||
| 97 | * @throws PhpSpreadsheetException |
||
| 98 | */ |
||
| 99 | protected function verifyFile($path, $verifyFile = true, $zip = null){ |
||
| 100 | if ($verifyFile && preg_match('~^data:image/[a-z]+;base64,~', $path) !== 1) { |
||
| 101 | // Check if a URL has been passed. https://stackoverflow.com/a/2058596/1252979 |
||
| 102 | if (filter_var($path, FILTER_VALIDATE_URL)) { |
||
| 103 | $this->path = $path; |
||
|
0 ignored issues
–
show
|
|||
| 104 | // Implicit that it is a URL, rather store info than running check above on value in other places. |
||
| 105 | $this->isUrl = true; |
||
|
0 ignored issues
–
show
|
|||
| 106 | $imageContents = file_get_contents($path); |
||
| 107 | $filePath = tempnam(sys_get_temp_dir(), 'Drawing'); |
||
| 108 | if ($filePath) { |
||
| 109 | file_put_contents($filePath, $imageContents); |
||
| 110 | if (file_exists($filePath)) { |
||
| 111 | return $filePath; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } elseif (file_exists($path)) { |
||
| 115 | return $path; |
||
| 116 | } elseif ($zip instanceof ZipArchive) { |
||
|
0 ignored issues
–
show
|
|||
| 117 | $zipPath = explode('#', $path)[1]; |
||
| 118 | if ($zip->locateName($zipPath) !== false) { |
||
| 119 | return $path; |
||
| 120 | } |
||
| 121 | } else { |
||
| 122 | throw new PhpSpreadsheetException("File $path not found!"); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | return $path; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * 加载文件 |
||
| 130 | * @param $fileName |
||
| 131 | * @return $this |
||
| 132 | */ |
||
| 133 | public function loadFile($fileName) |
||
| 134 | { |
||
| 135 | $this->spreadSheet = IOFactory::load($fileName); |
||
| 136 | return $this; |
||
| 137 | } |
||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * 选择sheet |
||
| 142 | * @param $sheetIndex |
||
| 143 | * @return $this |
||
| 144 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
| 145 | */ |
||
| 146 | public function selectWorkSheet($sheetIndex=0) |
||
| 147 | { |
||
| 148 | $this->workSheet = $this->spreadSheet->getSheet($sheetIndex); |
||
| 149 | return $this; |
||
| 150 | } |
||
| 151 | } |
||
| 152 |