本文实例讲述了PHP使用PhpSpreadsheet操作Excel。分享给大家供大家参考,具体如下:
一、PhpSpreadsheet 介绍
1、PhpSpreadsheet 是什么
PhpSpreadsheet是一个用纯PHP编写的库,提供了一组类,使您可以读取和写入不同的电子表格文件格式
PhpSpreadsheet提供了丰富的API接口,可以设置诸多单元格以及文档属性,包括样式、图片、日期、函数等等诸多应用,总之你想要什么样的Excel表格,PhpSpreadsheet都能做到
- 使用 PhpSpreadsheet 开发的PHP要求 7.1或更高版本
- PhpSpreadsheet 支持链式操作
2、PhpSpreadsheet 支持的文件格式
3、PhpSpreadsheet 官方网址
- https://phpspreadsheet.readthedocs.io
4、PhpSpreadsheet 安装
- composer require phpoffice/phpspreadsheet
二、基础知识
1、载入
<"htmlcode"><"htmlcode"><"htmlcode"><"htmlcode"><"htmlcode"><"t2">三、强化单元格1、设置单元格
- setCellValue
- 参数1:单元格位置
- 参数2:单元格的值
- setCellValueByColumnAndRow
- 参数1:列位置
- 参数2:行位置
- 参数3:单元格的值
<"htmlcode"><"htmlcode"><"htmlcode"><"htmlcode"><"欧阳克\n黄蓉"); $sheet->getStyle('A1')->getAlignment()->setWrapText(true); # Xlsx类 将电子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
- getHyperlink 获取单元格链接
- setUrl 设置单元格链接
<"t3">四、批量操作1、使用公式
<"htmlcode"><"htmlcode"><"htmlcode"><"htmlcode"><"htmlcode"><"htmlcode"><"htmlcode"><"t4">五、工作薄操作1、xlsx 文件下载
- IOFactory::createWriter 写入到文件
<"htmlcode"><"htmlcode"><"htmlcode"><"t5">六、office 后缀对应的 content-type后缀 MIME Type .doc application/msword .dot application/msword .docx application/vnd.openxmlformats-officedocument.wordprocessingml.document .dotx application/vnd.openxmlformats-officedocument.wordprocessingml.template .docm application/vnd.ms-word.document.macroEnabled.12 .dotm application/vnd.ms-word.template.macroEnabled.12 .xls application/vnd.ms-excel .xlt application/vnd.ms-excel .xla application/vnd.ms-excel .xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet .xltx application/vnd.openxmlformats-officedocument.spreadsheetml.template .xlsm application/vnd.ms-excel.sheet.macroEnabled.12 .xltm application/vnd.ms-excel.template.macroEnabled.12 .xlam application/vnd.ms-excel.addin.macroEnabled.12 .xlsb application/vnd.ms-excel.sheet.binary.macroEnabled.12 .ppt application/vnd.ms-powerpoint .pot application/vnd.ms-powerpoint .pps application/vnd.ms-powerpoint .ppa application/vnd.ms-powerpoint .pptx application/vnd.openxmlformats-officedocument.presentationml.presentation .potx application/vnd.openxmlformats-officedocument.presentationml.template .ppsx application/vnd.openxmlformats-officedocument.presentationml.slideshow .ppam application/vnd.ms-powerpoint.addin.macroEnabled.12 .pptm application/vnd.ms-powerpoint.presentation.macroEnabled.12 .potm application/vnd.ms-powerpoint.presentation.macroEnabled.12 .ppsm application/vnd.ms-powerpoint.slideshow.macroEnabled.12七、实战
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 数据列表页面
<"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> <"htmlcode"><"htmlcode"># 商品分类表 CREATE TABLE `shop_cat` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID', `pid` int(10) unsigned DEFAULT '0' COMMENT '父ID', `name` varchar(50) DEFAULT NULL COMMENT '分类名', `status` tinyint(1) unsigned DEFAULT '1' COMMENT '状态 1开启 0关闭', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COMMENT='分类表'; INSERT INTO `shop_cat` VALUES (1, 0, '女装', 1); INSERT INTO `shop_cat` VALUES (2, 0, '男装', 1); INSERT INTO `shop_cat` VALUES (3, 0, '孕产', 1); INSERT INTO `shop_cat` VALUES (4, 1, '连衣裙', 1); INSERT INTO `shop_cat` VALUES (5, 1, '牛仔裤', 1); INSERT INTO `shop_cat` VALUES (6, 2, '衬衫', 1); INSERT INTO `shop_cat` VALUES (7, 3, '睡衣', 1); # 商品表 CREATE TABLE `shop_list` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `cat_id` int(10) unsigned DEFAULT NULL COMMENT '分类ID', `cat_fid` int(10) unsigned DEFAULT NULL COMMENT '分类父ID', `title` varchar(200) NOT NULL COMMENT '商品标题', `price` double(10,2) unsigned NOT NULL COMMENT '价格', `img` varchar(200) NOT NULL COMMENT '商品图片', `add_time` int(10) unsigned NOT NULL COMMENT '添加时间', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='商品表';
- index.php 导入页面
<!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;"> <button type="button" class="layui-btn" id="up"><i class="layui-icon"></i>上传文件</button> <a href="download.php" rel="external nofollow" rel="external nofollow" class="layui-btn layui-btn-danger"><i class="layui-icon"></i>示例下载</a> </div> <div id="log" style="text-align:center;"> </div> </body> </html> <script src="/UploadFiles/2021-04-02/layui.js">
- 导入功能
<"'.$cat_fname.'"'); $cat_id = find('shop_cat','id','name="'.$cat_name.'"'); $data = [ 'title' => $title, 'cat_fid' => $cat_fid['id'], 'cat_id' => $cat_id['id'], 'price' => $price, 'img' => $img, 'add_time' => time(), ]; $ins = insert('shop_list',$data); if($ins){ $log[] = '第'.$a.'条,插入成功'; }else{ $log[] = '第'.$a.'条,插入失败'; } } echo json_encode(['code'=>0,'msg'=>'成功','data'=>$log]);
- 下载示例(范文)
<"_blank" href="//www.jb51.net/Special/339.htm">php操作office文档技巧总结(包括word,excel,access,ppt)》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数学运算技巧总结》、《php正则表达式用法总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》希望本文所述对大家PHP程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com桃源资源网 Design By www.nqtax.com暂无“PHP使用PhpSpreadsheet操作Excel实例详解”评论...