12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?php
- use ba\Filesystem;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- if (!function_exists('get_controller_list')) {
- function get_controller_list($app = 'admin'): array
- {
- $controllerDir = root_path() . 'app' . DIRECTORY_SEPARATOR . $app . DIRECTORY_SEPARATOR . 'controller' . DIRECTORY_SEPARATOR;
- return Filesystem::getDirFiles($controllerDir);
- }
- }
- if (!function_exists('read_excel')) {
- /**
- * 读取excel内容
- * @param $filename
- * @return array
- * @throws \PhpOffice\PhpSpreadsheet\Exception
- * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
- */
- function read_excel($filename)
- {
- //设置excel格式
- $reader = IOFactory::createReader('Xlsx');
- //载入excel文件
- $excel = $reader->load($filename);
- //读取第一张表
- $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;
- }
- }
|