1 | <?php |
||
2 | use tinymeng\spreadsheet\TSpreadSheet; |
||
3 | use tinymeng\spreadsheet\Util\ConstCode; |
||
4 | |||
5 | require __DIR__.'/../vendor/autoload.php'; |
||
6 | |||
7 | /** |
||
8 | * excel生成文件名 |
||
9 | */ |
||
10 | $filename = $sheetName = "export_demo"; |
||
11 | /** |
||
12 | * excel表头 |
||
13 | */ |
||
14 | //config 中 'fieldMappingMethod'=>ConstCode::FIELD_MAPPING_METHOD_NAME_CORRESPONDING_FIELD,//名称对应字段 |
||
15 | $title = [ |
||
16 | '序号'=>'_id', |
||
17 | 'ID'=>'id', |
||
18 | '订单编号'=>'order_sn', |
||
19 | '用户id'=>'user_id', |
||
20 | '结算日期'=>'day', |
||
21 | '下单时间'=>'create_time', |
||
22 | '图片'=>'image', |
||
23 | ]; |
||
24 | |||
25 | //config 中 'fieldMappingMethod'=>ConstCode::FIELD_MAPPING_METHOD_FIELD_CORRESPONDING_NAME,//字段对应名称 |
||
26 | //$title = [ |
||
27 | // '_id'=>'序号', |
||
28 | // 'id'=>'ID', |
||
29 | // 'order_sn'=>'订单编号', |
||
30 | // 'user_id'=>'用户id', |
||
31 | // 'day'=>'结算日期', |
||
32 | // 'create_time'=>'下单时间', |
||
33 | // 'image'=>'图片', |
||
34 | //]; |
||
35 | |||
36 | /** |
||
37 | * excel数据数组(二维) |
||
38 | */ |
||
39 | $data = [ |
||
40 | [ |
||
41 | 'id'=>'1', |
||
42 | 'order_sn'=>'20180101465464', |
||
43 | 'user_id'=>'1000', |
||
44 | 'day'=>'20220101', |
||
45 | 'create_time'=>'1687140376', |
||
46 | 'image'=>[ |
||
47 | 'type'=>'image', |
||
48 | 'content'=>'http://static.majiameng.com/main/img/portrait.jpg',//网络图片确保存在 |
||
49 | 'height'=>100, |
||
50 | // 'width'=>100,//只设置高,宽会自适应,如果设置宽后,高则失效 |
||
51 | ], |
||
52 | ],[ |
||
53 | 'id'=>'2', |
||
54 | 'order_sn'=>'20190101465464', |
||
55 | 'user_id'=>'1000', |
||
56 | 'day'=>'20220101', |
||
57 | 'create_time'=>'1687140376', |
||
58 | 'image'=>[ |
||
59 | 'type'=>'image', |
||
60 | 'content'=>'./text.png',//本地图片确保存在 |
||
61 | 'height'=>100, |
||
62 | ], |
||
63 | ],[ |
||
64 | 'id'=>'3', |
||
65 | 'order_sn'=>'20200101465464', |
||
66 | 'user_id'=>'1000', |
||
67 | 'day'=>'20220101', |
||
68 | 'create_time'=>'1687140376', |
||
69 | ],[ |
||
70 | 'id'=>'4', |
||
71 | 'order_sn'=>'20210101465464', |
||
72 | 'user_id'=>'1001', |
||
73 | 'day'=>'20220101', |
||
74 | 'create_time'=>'1687140376', |
||
75 | ], |
||
76 | ]; |
||
77 | $TSpreadSheet = TSpreadSheet::export() |
||
0 ignored issues
–
show
|
|||
78 | //创建一个sheet,设置sheet表头,并给表格赋值 |
||
79 | ->createWorkSheet($sheetName)->setWorkSheetData($title,$data); |
||
80 | // ->createWorkSheet($sheetName1)->setWorkSheetData($title1,$data1);//如果多个sheet可多次创建 |
||
81 | |||
82 | //文件存储本地 |
||
83 | $path = $TSpreadSheet->generate()->save($filename); |
||
84 | echo '生成excel路径:'.$path;exit(); |
||
85 | //生成excel路径:E:\spreadsheet-php\example\public\export\20240402\export_demo_2024-04-02_351.xlsx |
||
86 | |||
87 | //这样直接输出到浏览器中下载 |
||
88 | $TSpreadSheet->generate()->download($filename); |
||
89 | |||
90 | //配置参数可以通过配置文件在初始化时传入 |
||
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,//字段映射方式 |
||
102 | ]; |
||
103 | $TSpreadSheet = TSpreadSheet::export($config); |
||
104 | //配置参数也可以后期赋值 |
||
105 | $TSpreadSheet = TSpreadSheet::export($config)->setAutoFilter(true); |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.