ThinkPHP6之Excel解析

news/2024/9/20 14:49:59/文章来源:https://www.cnblogs.com/aeolian/p/18336516

PhpSpreadsheet解析Excel文件

安装 PhpSpreadsheet

通过 Composer 安装了 PhpSpreadsheet:

composer require phpoffice/phpspreadsheet

控制器

ExcelController

<?phpnamespace app\controller;use think\facade\Db;
use think\facade\Request;
use think\facade\View;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;class ExcelController
{public function upload(){// 获取上传的文件$file = Request::file('file');if (!$file) {return json(['status' => 'fail', 'message' => 'No file uploaded']);}/* 验证文件类型和大小$validate = ['size' => 10485760, // 10MB'ext' => 'xls,xlsx',];$fileInfo = $file->validate($validate);if (!$fileInfo) {return json(['status' => 'fail', 'message' => $file->getError()]);}*/// thinkphp6验证文件类型和大小$validate = Validate::rule(['file' => 'file|fileExt:xls,xlsx|fileSize:10485760', // 10MB]);$data = ['file' => $file];if (!$validate->check($data)) {return json(['status' => 'fail', 'message' => $validate->getError()]);}// 将文件保存到临时路径$savePath = $file->getPathname();try {// 解析Excel文件$spreadsheet = IOFactory::load($savePath);$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);// 开启数据库事务Db::startTrans();try {// 批量插入数据到数据库foreach ($sheetData as $index => $row) {if ($index == 0) {// 跳过第一行标题行continue;}// 假设有三列需要插入到数据库Db::table('your_table_name')->insert(['column1' => $row['A'],'column2' => $row['B'],'column3' => $row['C'],]);}// 提交事务Db::commit();return json(['status' => 'success', 'message' => 'Data imported successfully']);} catch (\Exception $e) {// 回滚事务Db::rollback();return json(['status' => 'fail', 'message' => 'Data import failed: ' . $e->getMessage()]);}} catch (\PhpOffice\PhpSpreadsheet\Reader\Exception $e) {return json(['status' => 'fail', 'message' => 'Error reading file: ' . $e->getMessage()]);}}
}

路由配置

use think\facade\Route;Route::post('upload-excel', 'ExcelController/upload');

前端

<form action="/upload-excel" method="post" enctype="multipart/form-data"><label for="file">选择 Excel 文件:</label><input type="file" name="file" id="file" accept=".xlsx,.xls"><input type="submit" value="上传并解析">
</form>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/776107.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

腾讯云数据库认证官方的考试费是多少钱?

腾讯云的认证项目很多,包括云计算、大数据、人工智能等多个技术领域方向的认证路径,每个方向包括工程师(Associate)、高级工程师(Professional)、专家(Expert)三个不同的等级。 对于数据库方面,腾讯云的这三个级别的考试费用分别是: TCCA工程师:1200元 TCCP高级工程师:18…

P3043 [USACO12JAN] Bovine Alliance G 题解

P3043 [USACO12JAN] Bovine Alliance G 题目传送门 思路 首先分情况讨论每种联通块的可能,有三种不同的情况会对答案 \(ans\) 产生不同的贡献。 联通块有环如图,因为每条边都有要有归属,所以环上的边只能全都顺时针或逆时针属于某个点,且不在环上的点仅有一种可能。 因此该…

组合数学学习笔记(持续完善中)

基础知识 一、加法原理 完成某个工作有 \(n\) 类办法,第 \(i\) 类办法有 \(a_i\) 种,则完成此工作的方案数有 \(\sum\limits _{i=1}^n a_i\) 种。 二、乘法原理 完成某个工作有 \(n\) 个步骤,第 \(i\) 个步骤有 \(b_i\) 种,则完成此工作的方案数有 \(\prod\limits _{i=1}^n…

Apple Safari 17.6 - macOS 专属浏览器 (独立安装包下载)

Apple Safari 17.6 - macOS 专属浏览器 (独立安装包下载)Apple Safari 17.6 - macOS 专属浏览器 (独立安装包下载) 适用于 macOS Ventura 和 macOS Monterey 的 Safari 浏览器 17 请访问原文链接:https://sysin.org/blog/apple-safari-17/,查看最新版。原创作品,转载请保留出…

Gartner 魔力象限:安全信息和事件管理 (SIEM) 2024

Gartner Magic Quadrant for Security Information and Event Management 2024Gartner Magic Quadrant for Security Information and Event Management 2024 Gartner 魔力象限:安全信息和事件管理 2024 请访问原文链接:https://sysin.org/blog/gartner-magic-quadrant-siem-…

Metasploit Pro 4.22.2-2024072501 (Linux, Windows) - 专业渗透测试框架

Metasploit Pro 4.22.2-2024072501 (Linux, Windows) - 专业渗透测试框架Metasploit Pro 4.22.2-2024072501 (Linux, Windows) - 专业渗透测试框架 Rapid7 Penetration testing, release Jul 25, 2024 请访问原文链接:https://sysin.org/blog/metasploit-pro-4/,查看最新版。…

C#中常用集合类型

原文:C#中常用集合类型 - Y00 - 博客园 (cnblogs.com)在C#中,集合是用于存储和操作一组数据项的数据结构。这些集合通常位于 System.Collections 和 System.Collections.Generic 命名空间中。下面我将概述C#中几种常用的集合类型及其特点: 1. System.Collections 命名空间中…

[实践]wireguard安装和配置

wireguard安装和配置,实现异地组网目录Server Install & ConfigUbuntu 20.0.4 install wireguard生成私钥&公钥开启内核IP转发配置文件启动&停止wg-quicksystemctlClient Install & Config下载ConfigmacOS Server Install & Config Ubuntu 20.0.4 install…

费曼积分法——以一个简单的例子讲解

今天又又又刷到一个视频,很想睡觉(昨晚熬了个大夜),但是又临近午饭不能睡,只能水篇随笔来打发时间了。 什么是费曼积分法? 先看看官方解释: 费曼积分法(Feynman integral)是一种求解复变函数定积分的计算方法,由理查德费曼(Richard P. Feynman)提出。这种方法特别适…

wireguard安装和使用

wireguard安装和使用目录Server Install & ConfigUbuntu 20.0.4 install wireguard生成私钥&公钥开启内核IP转发配置文件启动&停止wg-quicksystemctlClient Install & Config下载ConfigmacOS Server Install & Config Ubuntu 20.0.4 install wireguard sud…

Adam-mini:内存占用减半,性能更优的深度学习优化器

Adam(W)目前为训练LLM的主流优化器,但其内存开销较大,这是因为Adam优化器需要存储一阶动量m和二阶动量v,总内存占用至少是模型大小的两倍,这对现有的高端显卡也是一种负担。论文提出一种新的优化器Adam-mini,在不牺牲性能的情况下减少Adam优化器的内存占用。 https://avo…

稀土领域生产一体化管控系统建设案例

经过2个月的详细调研,**稀土目前缺少生产车间之间数据协同交互、缺少完整的生产工序数据协同监测和分析,无法及时了解生产过程和经营情况,更无法进行有效的生产过程优化。本项目推动**稀土生产环节的数字化、信息化和智能化改造,从行业、战略、技术和市场等方案全面提升**稀…