@@ -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,19 +77,19 @@ 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 | 94 | |
| 95 | 95 | /** 初始化当前workSheet */ |
@@ -115,13 +115,13 @@ discard block |
||
| 115 | 115 | public function generate() |
| 116 | 116 | { |
| 117 | 117 | /** 开启自动筛选 **/ |
| 118 | - if($this->autoFilter){ |
|
| 118 | + if ($this->autoFilter) { |
|
| 119 | 119 | $this->spreadSheet->getActiveSheet()->setAutoFilter( |
| 120 | 120 | $this->spreadSheet->getActiveSheet()->calculateWorksheetDimension() |
| 121 | 121 | ); |
| 122 | 122 | } |
| 123 | 123 | //文件存储 |
| 124 | - if(empty($this->fileName)){ |
|
| 124 | + if (empty($this->fileName)) { |
|
| 125 | 125 | $this->getFileName($this->sheetName); |
| 126 | 126 | } |
| 127 | 127 | return $this; |
@@ -132,8 +132,8 @@ discard block |
||
| 132 | 132 | * @param $file_name |
| 133 | 133 | * @return string |
| 134 | 134 | */ |
| 135 | - private function getFileName($sheetName){ |
|
| 136 | - $this->fileName = $sheetName.'_'.date('Y-m-d').'_'.rand(111,999).'.xlsx'; |
|
| 135 | + private function getFileName($sheetName) { |
|
| 136 | + $this->fileName = $sheetName.'_'.date('Y-m-d').'_'.rand(111, 999).'.xlsx'; |
|
| 137 | 137 | return $this->fileName; |
| 138 | 138 | } |
| 139 | 139 | |
@@ -143,18 +143,18 @@ discard block |
||
| 143 | 143 | * @return void |
| 144 | 144 | * @throws ExceptionAlias |
| 145 | 145 | */ |
| 146 | - public function download($filename=''){ |
|
| 147 | - if(empty($filename)){ |
|
| 146 | + public function download($filename = '') { |
|
| 147 | + if (empty($filename)) { |
|
| 148 | 148 | $filename = $this->fileName; |
| 149 | - }else{ |
|
| 149 | + } else { |
|
| 150 | 150 | $filename = $this->getFileName($filename); |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 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:*'); |
|
| 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 | 158 | header('Content-Type: application/vnd.ms-excel'); |
| 159 | 159 | header('Content-Disposition: attachment;filename="'.$filename); |
| 160 | 160 | header('Cache-Control: max-age=0'); |
@@ -171,12 +171,12 @@ discard block |
||
| 171 | 171 | * @return string |
| 172 | 172 | * @throws ExceptionAlias |
| 173 | 173 | */ |
| 174 | - public function save($filename='',$pathName=''): string |
|
| 174 | + public function save($filename = '', $pathName = ''): string |
|
| 175 | 175 | { |
| 176 | 176 | $pathName = $this->getPathName($pathName); |
| 177 | - if(empty($filename)){ |
|
| 177 | + if (empty($filename)) { |
|
| 178 | 178 | $filename = $this->fileName; |
| 179 | - }else{ |
|
| 179 | + } else { |
|
| 180 | 180 | $filename = $this->getFileName($filename); |
| 181 | 181 | } |
| 182 | 182 | FileTool::mkdir($pathName); |
@@ -7,7 +7,7 @@ discard block |
||
| 7 | 7 | */ |
| 8 | 8 | namespace tinymeng\spreadsheet\Util; |
| 9 | 9 | |
| 10 | -trait TConfig{ |
|
| 10 | +trait TConfig { |
|
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | 13 | * 文件创建者 |
@@ -244,13 +244,13 @@ discard block |
||
| 244 | 244 | */ |
| 245 | 245 | private function getPathName($pathName) |
| 246 | 246 | { |
| 247 | - if(!empty($pathName)){ |
|
| 247 | + if (!empty($pathName)) { |
|
| 248 | 248 | return $pathName; |
| 249 | 249 | } |
| 250 | - if($this->pathName){ |
|
| 250 | + if ($this->pathName) { |
|
| 251 | 251 | return $this->pathName; |
| 252 | 252 | } |
| 253 | - $pathName = dirname( dirname(dirname(SPREADSHEET_ROOT_PATH))).DIRECTORY_SEPARATOR."public".DIRECTORY_SEPARATOR."export".DIRECTORY_SEPARATOR.date('Ymd').DIRECTORY_SEPARATOR; |
|
| 253 | + $pathName = dirname(dirname(dirname(SPREADSHEET_ROOT_PATH))).DIRECTORY_SEPARATOR."public".DIRECTORY_SEPARATOR."export".DIRECTORY_SEPARATOR.date('Ymd').DIRECTORY_SEPARATOR; |
|
| 254 | 254 | return $pathName; |
| 255 | 255 | } |
| 256 | 256 | |
@@ -28,17 +28,17 @@ discard block |
||
| 28 | 28 | * @return mixed |
| 29 | 29 | * @throws \Exception |
| 30 | 30 | */ |
| 31 | - protected static function init($gateway, $config=[]) |
|
| 31 | + protected static function init($gateway, $config = []) |
|
| 32 | 32 | { |
| 33 | 33 | $gateway = StringTool::uFirst($gateway); |
| 34 | - $class = __NAMESPACE__ . '\\Gateways\\' . $gateway; |
|
| 34 | + $class = __NAMESPACE__.'\\Gateways\\'.$gateway; |
|
| 35 | 35 | if (class_exists($class)) { |
| 36 | 36 | $configFile = SPREADSHEET_ROOT_PATH."/config/TSpreadSheet.php"; |
| 37 | 37 | if (!file_exists($configFile)) { |
| 38 | 38 | return false; |
| 39 | 39 | } |
| 40 | 40 | $baseConfig = require $configFile; |
| 41 | - $app = new $class(array_replace_recursive($baseConfig,$config)); |
|
| 41 | + $app = new $class(array_replace_recursive($baseConfig, $config)); |
|
| 42 | 42 | if ($app instanceof GatewayInterface) { |
| 43 | 43 | return $app; |
| 44 | 44 | } |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | * @param $config |
| 56 | 56 | * @return mixed |
| 57 | 57 | */ |
| 58 | - public static function __callStatic($gateway, $config=[]) |
|
| 58 | + public static function __callStatic($gateway, $config = []) |
|
| 59 | 59 | { |
| 60 | 60 | return self::init($gateway, ...$config); |
| 61 | 61 | } |
@@ -26,7 +26,7 @@ |
||
| 26 | 26 | |
| 27 | 27 | public function testSaveFile() |
| 28 | 28 | { |
| 29 | - $pathName = __DIR__ . '/tmp/'; |
|
| 29 | + $pathName = __DIR__.'/tmp/'; |
|
| 30 | 30 | $fileName = $this->export->save('TestSheet', $pathName); |
| 31 | 31 | $this->assertTrue(file_exists($fileName)); |
| 32 | 32 | unlink($fileName); |
@@ -7,7 +7,7 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | namespace tinymeng\spreadsheet\Util; |
| 9 | 9 | |
| 10 | -class ConstCode{ |
|
| 11 | - const FIELD_MAPPING_METHOD_FIELD_CORRESPONDING_NAME = 1;//字段对应名称 |
|
| 12 | - const FIELD_MAPPING_METHOD_NAME_CORRESPONDING_FIELD = 2;//名称对应字段 |
|
| 10 | +class ConstCode { |
|
| 11 | + const FIELD_MAPPING_METHOD_FIELD_CORRESPONDING_NAME = 1; //字段对应名称 |
|
| 12 | + const FIELD_MAPPING_METHOD_NAME_CORRESPONDING_FIELD = 2; //名称对应字段 |
|
| 13 | 13 | } |
@@ -24,14 +24,14 @@ |
||
| 24 | 24 | // ->initWorkSheet($filename);//读取并初始化表格内容数据 |
| 25 | 25 | |
| 26 | 26 | //2. 读取带图片并初始化表格内容数据 |
| 27 | -$path = './uploads/imgs/'.date('Ymd', time());//excel中图片本地存储路径 |
|
| 27 | +$path = './uploads/imgs/'.date('Ymd', time()); //excel中图片本地存储路径 |
|
| 28 | 28 | $TSpreadSheet = TSpreadSheet::import() |
| 29 | 29 | ->initWorkSheet($filename)//读取并初始化表格内容数据 |
| 30 | - ->setRelativePath($path)->setImagePath($path);// 设置将excel中图片本地存储 |
|
| 30 | + ->setRelativePath($path)->setImagePath($path); // 设置将excel中图片本地存储 |
|
| 31 | 31 | |
| 32 | 32 | //3. 设置title对应字段,获取表格内容 |
| 33 | 33 | $data = $TSpreadSheet->setTitle($title)->getExcelData(); |
| 34 | -var_dump($data);die; |
|
| 34 | +var_dump($data); die; |
|
| 35 | 35 | /** |
| 36 | 36 | * array(3) { |
| 37 | 37 | * [0]=> |
@@ -45,11 +45,11 @@ discard block |
||
| 45 | 45 | 'create_time'=>'1687140376', |
| 46 | 46 | 'image'=>[ |
| 47 | 47 | 'type'=>'image', |
| 48 | - 'content'=>'http://static.majiameng.com/main/img/portrait.jpg',//网络图片确保存在 |
|
| 48 | + 'content'=>'http://static.majiameng.com/main/img/portrait.jpg', //网络图片确保存在 |
|
| 49 | 49 | 'height'=>100, |
| 50 | 50 | // 'width'=>100,//只设置高,宽会自适应,如果设置宽后,高则失效 |
| 51 | 51 | ], |
| 52 | - ],[ |
|
| 52 | + ], [ |
|
| 53 | 53 | 'id'=>'2', |
| 54 | 54 | 'order_sn'=>'20190101465464', |
| 55 | 55 | 'user_id'=>'1000', |
@@ -57,16 +57,16 @@ discard block |
||
| 57 | 57 | 'create_time'=>'1687140376', |
| 58 | 58 | 'image'=>[ |
| 59 | 59 | 'type'=>'image', |
| 60 | - 'content'=>'./text.png',//本地图片确保存在 |
|
| 60 | + 'content'=>'./text.png', //本地图片确保存在 |
|
| 61 | 61 | 'height'=>100, |
| 62 | 62 | ], |
| 63 | - ],[ |
|
| 63 | + ], [ |
|
| 64 | 64 | 'id'=>'3', |
| 65 | 65 | 'order_sn'=>'20200101465464', |
| 66 | 66 | 'user_id'=>'1000', |
| 67 | 67 | 'day'=>'20220101', |
| 68 | 68 | 'create_time'=>'1687140376', |
| 69 | - ],[ |
|
| 69 | + ], [ |
|
| 70 | 70 | 'id'=>'4', |
| 71 | 71 | 'order_sn'=>'20210101465464', |
| 72 | 72 | 'user_id'=>'1001', |
@@ -76,12 +76,12 @@ discard block |
||
| 76 | 76 | ]; |
| 77 | 77 | $TSpreadSheet = TSpreadSheet::export() |
| 78 | 78 | //创建一个sheet,设置sheet表头,并给表格赋值 |
| 79 | - ->createWorkSheet($sheetName)->setWorkSheetData($title,$data); |
|
| 79 | + ->createWorkSheet($sheetName)->setWorkSheetData($title, $data); |
|
| 80 | 80 | // ->createWorkSheet($sheetName1)->setWorkSheetData($title1,$data1);//如果多个sheet可多次创建 |
| 81 | 81 | |
| 82 | 82 | //文件存储本地 |
| 83 | 83 | $path = $TSpreadSheet->generate()->save($filename); |
| 84 | -echo '生成excel路径:'.$path;exit(); |
|
| 84 | +echo '生成excel路径:'.$path; exit(); |
|
| 85 | 85 | //生成excel路径:E:\spreadsheet-php\example\public\export\20240402\export_demo_2024-04-02_351.xlsx |
| 86 | 86 | |
| 87 | 87 | //这样直接输出到浏览器中下载 |
@@ -89,16 +89,16 @@ discard block |
||
| 89 | 89 | |
| 90 | 90 | //配置参数可以通过配置文件在初始化时传入 |
| 91 | 91 | $config = [ |
| 92 | - 'pathName'=>null, //文件存储位置 |
|
| 93 | - 'fileName'=>null, //文件名称 |
|
| 94 | - 'horizontalCenter'=>true, //是否居中 |
|
| 95 | - 'titleHeight'=>null, //定义表头行高,常用22 |
|
| 96 | - 'titleWidth'=>null, //定义表头列宽(未设置则自动计算宽度),常用20 |
|
| 97 | - 'height'=>null, //定义数据行高,常用22 |
|
| 98 | - 'autoFilter'=>false, //自动筛选(是否开启) |
|
| 99 | - 'autoDataType'=>true, //自动适应文本类型 |
|
| 100 | - 'freezePane'=>false, //冻结窗格(要冻结的首行首列"B2",false不开启) |
|
| 101 | - 'fieldMappingMethod'=>ConstCode::FIELD_MAPPING_METHOD_NAME_CORRESPONDING_FIELD,//字段映射方式 |
|
| 92 | + 'pathName'=>null, //文件存储位置 |
|
| 93 | + 'fileName'=>null, //文件名称 |
|
| 94 | + 'horizontalCenter'=>true, //是否居中 |
|
| 95 | + 'titleHeight'=>null, //定义表头行高,常用22 |
|
| 96 | + 'titleWidth'=>null, //定义表头列宽(未设置则自动计算宽度),常用20 |
|
| 97 | + 'height'=>null, //定义数据行高,常用22 |
|
| 98 | + 'autoFilter'=>false, //自动筛选(是否开启) |
|
| 99 | + 'autoDataType'=>true, //自动适应文本类型 |
|
| 100 | + 'freezePane'=>false, //冻结窗格(要冻结的首行首列"B2",false不开启) |
|
| 101 | + 'fieldMappingMethod'=>ConstCode::FIELD_MAPPING_METHOD_NAME_CORRESPONDING_FIELD, //字段映射方式 |
|
| 102 | 102 | ]; |
| 103 | 103 | $TSpreadSheet = TSpreadSheet::export($config); |
| 104 | 104 | //配置参数也可以后期赋值 |
@@ -1,22 +1,22 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | use \tinymeng\spreadsheet\Util\ConstCode; |
| 3 | 3 | return [ |
| 4 | - 'creator'=>'tinymeng', //文件创建者 |
|
| 5 | - 'pathName'=>null, //文件存储位置 |
|
| 6 | - 'fileName'=>null, //文件名称 |
|
| 7 | - 'horizontalCenter'=>true, //是否居中 |
|
| 8 | - 'titleHeight'=>null, //定义表头行高,常用22 |
|
| 9 | - 'titleWidth'=>null, //定义表头列宽(未设置则自动计算宽度),常用20 |
|
| 10 | - 'height'=>null, //定义数据行高,常用22 |
|
| 11 | - 'autoFilter'=>false, //自动筛选(是否开启) |
|
| 12 | - 'autoDataType'=>true, //自动适应文本类型 |
|
| 13 | - 'freezePane'=>false, //冻结窗格(要冻结的首行首列"B2",false不开启) |
|
| 4 | + 'creator'=>'tinymeng', //文件创建者 |
|
| 5 | + 'pathName'=>null, //文件存储位置 |
|
| 6 | + 'fileName'=>null, //文件名称 |
|
| 7 | + 'horizontalCenter'=>true, //是否居中 |
|
| 8 | + 'titleHeight'=>null, //定义表头行高,常用22 |
|
| 9 | + 'titleWidth'=>null, //定义表头列宽(未设置则自动计算宽度),常用20 |
|
| 10 | + 'height'=>null, //定义数据行高,常用22 |
|
| 11 | + 'autoFilter'=>false, //自动筛选(是否开启) |
|
| 12 | + 'autoDataType'=>true, //自动适应文本类型 |
|
| 13 | + 'freezePane'=>false, //冻结窗格(要冻结的首行首列"B2",false不开启) |
|
| 14 | 14 | /** |
| 15 | 15 | * 字段映射方式 |
| 16 | 16 | * ConstCode::FIELD_MAPPING_METHOD_FIELD_CORRESPONDING_NAME = 1;//字段对应名称 |
| 17 | 17 | * ConstCode::FIELD_MAPPING_METHOD_NAME_CORRESPONDING_FIELD = 2;//名称对应字段 |
| 18 | 18 | */ |
| 19 | 19 | 'fieldMappingMethod'=>ConstCode::FIELD_MAPPING_METHOD_NAME_CORRESPONDING_FIELD, |
| 20 | - 'mainTitleLine'=>false, //主标题行是否显示 |
|
| 21 | - 'mainTitle'=>'', //主标题名称,默认为sheet的名称 |
|
| 20 | + 'mainTitleLine'=>false, //主标题行是否显示 |
|
| 21 | + 'mainTitle'=>'', //主标题名称,默认为sheet的名称 |
|
| 22 | 22 | ]; |
@@ -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!'); |