七、实战
1、导出数据
- login_log 登陆日志表
CREATE TABLE `login_log` ( `id` int(11) NOT NULL AUTO_INCREMENT, `uid` int(11) DEFAULT NULL COMMENT '管理员ID', `client` tinyint(4) unsigned DEFAULT '0' COMMENT '0-PC 1-ios 2-android', `add_time` int(11) DEFAULT '0' COMMENT '创建时间', `ip` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '登录IP', PRIMARY KEY (`id`) USING BTREE ) ENGINE=MyISAM AUTO_INCREMENT=1122 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='登录日志';
- index.php 数据列表页面
<?php # 载入方法库 require 'function.php'; $select = select('login_log','*'); if(empty($select)){ exit; }else{ foreach($select as &$v){ switch ($v['client']) { case 0: $v['client'] = 'PC电脑'; break; case 1: $v['client'] = '苹果手机'; break; case 2: $v['client'] = '安卓手机'; break; } $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']); } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>导出数据</title> <link rel="stylesheet" href="layui/css/layui.css" rel="external nofollow" rel="external nofollow" > </head> <body> <div style="text-align:center;"> <a href="download.php" rel="external nofollow" rel="external nofollow" class="layui-btn layui-btn-radius layui-btn-danger">导出数据</a> </div> <table class="layui-table"> <thead> <tr> <th>ID</th> <th>用户ID</th> <th>登陆设备</th> <th>登陆时间</th> <th>登陆ip</th> </tr> </thead> <tbody> <?php foreach($select as $v){ ?> <tr> <td><?php echo $v['id'] ?></td> <td><?php echo $v['uid'] ?></td> <td><?php echo $v['client'] ?></td> <td><?php echo $v['add_time'] ?></td> <td><?php echo $v['ip'] ?></td> </tr> <?php } ?> </tbody> </table> </body> </html>
- download.php 导出操作
<?php # 载入方法库 require 'function.php'; $select = select('login_log','*'); if(empty($select)){ exit; }else{ foreach($select as &$v){ switch ($v['client']) { case 0: $v['client'] = 'PC电脑'; break; case 1: $v['client'] = '苹果手机'; break; case 2: $v['client'] = '安卓手机'; break; } $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']); } } # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use \PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','用户ID'); $sheet->setCellValue('C1','登陆设备'); $sheet->setCellValue('D1','登陆时间'); $sheet->setCellValue('E1','登陆ip'); $sheet->fromArray( $select, null, 'A2' ); // MIME 协议,文件的类型,不设置,会默认html header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // MIME 协议的扩展 header('Content-Disposition:attachment;filename=1.xlsx'); // 缓存控制 header('Cache-Control:max-age=0'); $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx'); // php://output 它是一个只写数据流, 允许你以 print 和 echo一样的方式写入到输出缓冲区。 $writer->save('php://output'); ?>
内容版权声明:除非注明,否则皆为本站原创文章。