common.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. use ba\Filesystem;
  3. use PhpOffice\PhpSpreadsheet\IOFactory;
  4. if (!function_exists('get_controller_list')) {
  5. function get_controller_list($app = 'admin'): array
  6. {
  7. $controllerDir = root_path() . 'app' . DIRECTORY_SEPARATOR . $app . DIRECTORY_SEPARATOR . 'controller' . DIRECTORY_SEPARATOR;
  8. return Filesystem::getDirFiles($controllerDir);
  9. }
  10. }
  11. if (!function_exists('read_excel')) {
  12. /**
  13. * 读取excel内容
  14. * @param $filename
  15. * @return array
  16. * @throws \PhpOffice\PhpSpreadsheet\Exception
  17. * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
  18. */
  19. function read_excel($filename)
  20. {
  21. //设置excel格式
  22. $reader = IOFactory::createReader('Xlsx');
  23. //载入excel文件
  24. $excel = $reader->load($filename);
  25. //读取第一张表
  26. $sheet = $excel->getSheet(0);
  27. //获取总行数
  28. $row_num = $sheet->getHighestRow();
  29. //获取总列数
  30. $col_num = $sheet->getHighestColumn();
  31. $data = []; //数组形式获取表格数据
  32. for($col='A';$col<=$col_num;$col++)
  33. {
  34. //从第二行开始,去除表头(若无表头则从第一行开始)
  35. for($row=2;$row<=$row_num;$row++)
  36. {
  37. $data[$row-2][] = $sheet->getCell($col.$row)->getValue();
  38. }
  39. }
  40. return $data;
  41. }
  42. }