@@ -30,9 +30,9 @@ |
||
30 | 30 | * @return $this |
31 | 31 | * @throws Exception |
32 | 32 | */ |
33 | - public function initWorkSheet($filename='') |
|
33 | + public function initWorkSheet($filename = '') |
|
34 | 34 | { |
35 | - if(!empty($filename)){ |
|
35 | + if (!empty($filename)) { |
|
36 | 36 | $this->setFileName($filename); |
37 | 37 | } |
38 | 38 | $this->spreadSheet = IOFactory::load($this->fileName); |
@@ -43,8 +43,8 @@ discard block |
||
43 | 43 | * @author: Tinymeng <[email protected]> |
44 | 44 | * @time: 2022/4/24 17:35 |
45 | 45 | */ |
46 | - protected function cellName($columnIndex){ |
|
47 | - $columnIndex =(int)$columnIndex+1; |
|
46 | + protected function cellName($columnIndex) { |
|
47 | + $columnIndex = (int) $columnIndex + 1; |
|
48 | 48 | static $indexCache = []; |
49 | 49 | |
50 | 50 | if (!isset($indexCache[$columnIndex])) { |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | do { |
54 | 54 | $characterValue = ($indexValue % 26) ?: 26; |
55 | 55 | $indexValue = ($indexValue - $characterValue) / 26; |
56 | - $base26 = chr($characterValue + 64) . ($base26 ?: ''); |
|
56 | + $base26 = chr($characterValue + 64).($base26 ?: ''); |
|
57 | 57 | } while ($indexValue > 0); |
58 | 58 | $indexCache[$columnIndex] = $base26; |
59 | 59 | } |
@@ -66,10 +66,10 @@ discard block |
||
66 | 66 | * @param string |
67 | 67 | * @return mixed |
68 | 68 | */ |
69 | - protected function formatValue($v){ |
|
70 | - if($this->format === false) return $v; |
|
71 | - if(is_numeric($v) && strlen($v)===10){ |
|
72 | - $v = date($this->format_date,$v);//时间戳转时间格式 |
|
69 | + protected function formatValue($v) { |
|
70 | + if ($this->format === false) return $v; |
|
71 | + if (is_numeric($v) && strlen($v) === 10) { |
|
72 | + $v = date($this->format_date, $v); //时间戳转时间格式 |
|
73 | 73 | } |
74 | 74 | return $v; |
75 | 75 | } |
@@ -79,9 +79,9 @@ discard block |
||
79 | 79 | * @param $lastCell |
80 | 80 | * @return array |
81 | 81 | */ |
82 | - protected function getCellName($lastCell){ |
|
82 | + protected function getCellName($lastCell) { |
|
83 | 83 | $cellName = array(); |
84 | - for($i='A'; $i!=$lastCell; $i++) { |
|
84 | + for ($i = 'A'; $i != $lastCell; $i++) { |
|
85 | 85 | $cellName[] = $i; |
86 | 86 | } |
87 | 87 | $cellName[] = $i++; |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | * @return array|string|string[] |
96 | 96 | * @throws PhpSpreadsheetException |
97 | 97 | */ |
98 | - protected function verifyFile($path, $verifyFile = true, $zip = null){ |
|
98 | + protected function verifyFile($path, $verifyFile = true, $zip = null) { |
|
99 | 99 | if ($verifyFile && preg_match('~^data:image/[a-z]+;base64,~', $path) !== 1) { |
100 | 100 | // Check if a URL has been passed. https://stackoverflow.com/a/2058596/1252979 |
101 | 101 | if (filter_var($path, FILTER_VALIDATE_URL)) { |
@@ -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 | //配置参数也可以后期赋值 |
@@ -13,12 +13,12 @@ discard block |
||
13 | 13 | * 注意:分组字段必须在title中定义 |
14 | 14 | */ |
15 | 15 | $fileTitle = [ |
16 | - 'title_row' => 2, // 表头行号 |
|
17 | - 'group_left' => ['user_id', 'day'], // 左侧分组字段,最多支持两级分组 |
|
16 | + 'title_row' => 2, // 表头行号 |
|
17 | + 'group_left' => ['user_id', 'day'], // 左侧分组字段,最多支持两级分组 |
|
18 | 18 | 'title' => [ |
19 | 19 | 'ID' => 'id', |
20 | - '用户ID' => 'user_id', // 分组字段1 |
|
21 | - '结算日期' => 'day', // 分组字段2 |
|
20 | + '用户ID' => 'user_id', // 分组字段1 |
|
21 | + '结算日期' => 'day', // 分组字段2 |
|
22 | 22 | '订单编号' => 'order_sn', |
23 | 23 | '下单时间' => 'create_time', |
24 | 24 | ] |
@@ -30,43 +30,43 @@ discard block |
||
30 | 30 | $data = [ |
31 | 31 | [ |
32 | 32 | 'id' => '1', |
33 | - 'user_id' => '1000', // 第一组用户 |
|
34 | - 'day' => '20220101', // 第一天 |
|
33 | + 'user_id' => '1000', // 第一组用户 |
|
34 | + 'day' => '20220101', // 第一天 |
|
35 | 35 | 'order_sn' => '20180101465464', |
36 | 36 | 'create_time' => '1687140376', |
37 | 37 | ], |
38 | 38 | [ |
39 | 39 | 'id' => '2', |
40 | - 'user_id' => '1000', // 第一组用户 |
|
41 | - 'day' => '20220101', // 第一天 |
|
40 | + 'user_id' => '1000', // 第一组用户 |
|
41 | + 'day' => '20220101', // 第一天 |
|
42 | 42 | 'order_sn' => '20180101465465', |
43 | 43 | 'create_time' => '1687140377', |
44 | 44 | ], |
45 | 45 | [ |
46 | 46 | 'id' => '3', |
47 | - 'user_id' => '1000', // 第一组用户 |
|
48 | - 'day' => '20220102', // 第二天 |
|
47 | + 'user_id' => '1000', // 第一组用户 |
|
48 | + 'day' => '20220102', // 第二天 |
|
49 | 49 | 'order_sn' => '20180102465466', |
50 | 50 | 'create_time' => '1687140378', |
51 | 51 | ], |
52 | 52 | [ |
53 | 53 | 'id' => '4', |
54 | - 'user_id' => '1001', // 第二组用户 |
|
55 | - 'day' => '20220101', // 第一天 |
|
54 | + 'user_id' => '1001', // 第二组用户 |
|
55 | + 'day' => '20220101', // 第一天 |
|
56 | 56 | 'order_sn' => '20180101465467', |
57 | 57 | 'create_time' => '1687140379', |
58 | 58 | ], |
59 | 59 | [ |
60 | 60 | 'id' => '5', |
61 | - 'user_id' => '1000', // 第二组用户 |
|
62 | - 'day' => '20220101', // 第一天 |
|
61 | + 'user_id' => '1000', // 第二组用户 |
|
62 | + 'day' => '20220101', // 第一天 |
|
63 | 63 | 'order_sn' => '20180101465468', |
64 | 64 | 'create_time' => '1687140379', |
65 | 65 | ], |
66 | 66 | [ |
67 | 67 | 'id' => '6', |
68 | - 'user_id' => '1001', // 第二组用户 |
|
69 | - 'day' => '20220101', // 第一天 |
|
68 | + 'user_id' => '1001', // 第二组用户 |
|
69 | + 'day' => '20220101', // 第一天 |
|
70 | 70 | 'order_sn' => '20180101465469', |
71 | 71 | 'create_time' => '1687140379', |
72 | 72 | ], |
@@ -74,13 +74,13 @@ discard block |
||
74 | 74 | |
75 | 75 | // 配置参数 |
76 | 76 | $config = [ |
77 | - 'horizontalCenter' => true, // 是否居中 |
|
78 | - 'titleHeight' => 22, // 定义表头行高 |
|
79 | - 'titleWidth' => 20, // 定义表头列宽 |
|
80 | - 'height' => 22, // 定义数据行高 |
|
81 | - 'autoFilter' => true, // 开启自动筛选 |
|
82 | - 'freezePane' => false, // 冻结窗格(首行首列) |
|
83 | - 'fieldMappingMethod' => ConstCode::FIELD_MAPPING_METHOD_NAME_CORRESPONDING_FIELD, // 名称对应字段方式 |
|
77 | + 'horizontalCenter' => true, // 是否居中 |
|
78 | + 'titleHeight' => 22, // 定义表头行高 |
|
79 | + 'titleWidth' => 20, // 定义表头列宽 |
|
80 | + 'height' => 22, // 定义数据行高 |
|
81 | + 'autoFilter' => true, // 开启自动筛选 |
|
82 | + 'freezePane' => false, // 冻结窗格(首行首列) |
|
83 | + 'fieldMappingMethod' => ConstCode::FIELD_MAPPING_METHOD_NAME_CORRESPONDING_FIELD, // 名称对应字段方式 |
|
84 | 84 | ]; |
85 | 85 | |
86 | 86 | // 创建导出实例并设置数据 |