本文实例讲述了thinkphp5.1 框架导入/导出excel文件操作。分享给大家供大家参考,具体如下:
thinkphp5.1 导入excel文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
public function importExcel() { try { //获取表格的大小,限制上传表格的大小 if ( $_FILES [ 'file' ][ 'size' ] > 10 * 1024 * 1024) { //文件过大 log_debug( $log_title . 'END === MSG:' . '文件过大' ); parent::endBack([ 'state' => 0, 'msg' => '文件过大' ]); } //限制上传表格类型 $ext = substr ( strrchr ( $_FILES [ 'file' ][ "name" ], '.' ), 1); if ( $ext != 'xls' && $ext != 'xlsx' ) { log_debug( $log_title . 'END === MSG:' . '文件格式不正确' ); parent::endBack([ 'state' => 0, 'msg' => '上传文件必须为excel表格' ]); } //读取表格 $filename = $_FILES [ 'file' ][ 'tmp_name' ]; $reader = IOFactory::createReader( 'Xlsx' ); //Xls,Xlsx都可读取 $canRead = $reader ->canRead( $filename ); if (! $canRead ) { log_debug( $log_title . 'END,文件格式不正确,SQL:' . Db::name( '' )->getLastSql()); parent::endBack([ 'state' => 0, 'msg' => '文件格式不正确' , 're_login' => false]); } $spreadsheet = $reader ->load( $filename ); //载入excel表格 $worksheet = $spreadsheet ->getActiveSheet(); //选中sheet表 $highestRow = $worksheet ->getHighestRow(); // 总行数 // $highestColumn = $worksheet->getHighestColumn(); // 总列数 if (!(0 < $highestRow )) { log_debug( $log_title . 'END,文件内容空,SQL:' . Db::name( '' )->getLastSql()); parent::endBack([ 'state' => 0, 'msg' => '文件没有数据' , 're_login' => false]); } //循环读取--有效判断 $sst_word_arr = []; //存放敏感词的数组 for ( $row = 1; $row <= $highestRow ; $row ++) { //取列数A列的数据 $tmp_word = $spreadsheet ->getActiveSheet()->getCell( 'A' . $row )->getValue(); if ( '' != trim( $tmp_word ) && null != $tmp_word ) { $sst_word_arr [] = $tmp_word ; break ; //发现有效数据,直接退出,接下来插入数据 } } // $sst_word_arr = array_unique($sst_word_arr); if ( empty ( $sst_word_arr )) { log_debug( $log_title . 'END,文件无有效数据,SQL:' . Db::name( '' )->getLastSql()); parent::endBack([ 'state' => 0, 'msg' => '文件无有效数据' , 're_login' => false]); } //判断和数据库操作 for ( $row = 2; $row <= $highestRow ; $row ++) { //取列数A列的数据 $tmp_old_car_num = $spreadsheet ->getActiveSheet()->getCell( 'A' . $row )->getValue(); $car_num = trim( $tmp_old_car_num ); if ( '' != $car_num && null != $car_num ) { //数据库操作 } } } $ret_arr = [ 'state' => 1, //返回数据 ]; log_debug( $log_title . 'END,SUCCESS' ); parent::endBack( $ret_arr ); } catch (\Exception $e ) { // } } |
excel文件格式为:
thinkphp5.1 导出excel文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
namespase app\test; use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; class test { public function carNumsExport() { $log_title = '测试 => 车牌列表导出[' . __METHOD__ . '] ' ; try { $file_name = '《车牌列表》from y8zh - ' . $user_info [ 'uid' ] . '.xlsx' ; $file_relative_path = parent:: $module_name . DIRECTORY_SEPARATOR . 'fcb_car_nums' . DIRECTORY_SEPARATOR; $file_path = parent:: $api_file_root_path . $file_relative_path ; // 已生成过则直接返回 if ( file_exists ( $file_path . $file_name )) { $ret_arr = [ 'state' => 1, 'download_url' => parent:: $api_file_get_url . $file_relative_path . $file_name , ]; parent::endBack( $ret_arr ); } if (! is_dir ( $file_path )) { mkdir ( $file_path , 0777, true); } $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet ->getActiveSheet(); //获取所有车牌号 $car_nums = Db::connect( 'db_config_yun' )->name( 'vechicle' )->column( 'DISTINCT number' ); $i = 1; $sheet ->setCellValue( 'A' . $i , '车牌号' )->getStyle( 'A' . $i )->getFont()->setBold(true); $i ++; // 表内容 if (! empty ( $car_nums )) { foreach ( $car_nums as $k_c => $v_c ) { $sheet ->setCellValue( 'A' . $i , $v_c ); $i ++; } } $writer = new Xlsx( $spreadsheet ); $writer ->save( $file_path . $file_name ); $ret_arr = [ 'state' => 1, 'download_url' => parent:: $api_file_get_url . $file_relative_path . $file_name , ]; log_debug( $log_title . 'END === DOWNLOAD_URL:' . $ret_arr [ 'download_url' ]); parent::endBack( $ret_arr ); } catch (\Exception $e ) { // } } } |
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。
原文链接:https://blog.csdn.net/qq_42176520/article/details/98482181