phpoffice/phpexcel 读取Excel表格数据
2024-09-08
10
1. 使用示例
TP5.0
$file = request()->file('file');
$data = Excel::read($file->getRealpath());
2. 封装类
更新于:8天前<?php
/**
* 导入数据
* composer require phpoffice/phpexcel
* PHP7.2版本以下推荐使用 phpoffice/phpexcel
* PHP7.2版本以上推荐使用 phpoffice/phpspreadsheet
*/
class Excel
{
/**
* 读取表格数据
* @param string 临时文件路径
* @return array
*/
public static function read($file)
{
// 设置excel格式
$reader = PHPExcel_IOFactory::createReader('Excel5');
// 载入excel文件
$excel = $reader->load($file);
// 读取第一张表
$sheet = $excel->getSheet(0);
// 获取总行数
$row_num = $sheet->getHighestRow();
// 获取总列数
$col_num = $sheet->getHighestColumn();
$data = []; //数组形式获取表格数据
for ($col = 'A'; $col <= $col_num; $col++) {
for ($row = 2; $row <= $row_num; $row++) {
$data[$row - 2][] = $sheet->getCell($col . $row)->getValue();
}
}
return $data;
}
}
赞一波!
相关文章
- TP6.0 使用 phpoffice/phpspreadsheet 导入数据
- phpoffice/phpexcel 导出Excel表格数据
- 修改数据 update 命令
- 删除数据 delete、truncate 命令
- MySQL 添加数据 insert 命令及优化
- PHP 操作 Excel - phpoffice/phpspreadsheet 扩展包
- Vue 父子组件通信传值(子组件中使用父组件中的数据)
- 对无限级分类数据进行重新排序(非树形结构)
- 针对海量数据的存储与访问瓶颈的解决方案
- Layui 数据表格重载
- Layui 数据表格方法渲染中的复杂表头简单使用示例
- 如何快速优化几千万数据量的订单表
- 如何在 Python 中创建静态类数据和静态类方法?
- MySQL 哈希索引、空间数据索引、全文索引
- 如何在 Pandas 中创建一个空的数据帧并向其附加行和列?
- 如何将 Python datetime.datetime 转换为 Excel 序列号?
- 如何在 Python 和 Node.js 之间通信 JSON 数据?
- C++数据抽象简介:实现信息隐藏和模块化设计
- Python创建Excel重复写入
- Java导入Excel文件的实现方式
文章评论
全部评论