PhpSpreadsheet + Thinkphp 5.1 导入Excel表格到数据库

安装

1
composer require phpoffice/phpspreadsheet

引入

1
2
3
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;

获取导入的Excel表格数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 * 获取导入Excel表数据
 *
 * @param integer $sheet  //工作表sheet(传0则获取第一个sheet)
 * @param string $file_path  // 文件地址
 * @param integer $columnCnt //列数(传0则自动获取最大列)
 * @param array $options // 操作选项
 * @return void
 */
function getImportExcelData($file_path = '', $columnCnt = 0, $sheet = 0, $options = [])
{
    // 转码
    $file = iconv("utf-8", "gb2312", $file_path);
    if (empty($file) or !file_exists($file)) {
        throw new \Exception('文件不存在!');
    }
    $objRead = IOFactory::createReader('Xlsx');
    if (!$objRead->canRead($file)) {
        /** @var Xls $objRead */
        $objRead = IOFactory::createReader('Xls');
 
        if (!$objRead->canRead($file)) {
            throw new \Exception('只支持导入Excel文件!');
        }
    }
    /* 如果不需要获取特殊操作,则只读内容,可以大幅度提升读取Excel效率 */
    empty($options) && $objRead->setReadDataOnly(true);
 
    /* 建立excel对象 */
    $obj = $objRead->load($file);
    /* 获取指定的sheet表 */
    $currSheet = $obj->getSheet($sheet);
    if (isset($options['mergeCells'])) {
        /* 读取合并行列 */
        $options['mergeCells'] = $currSheet->getMergeCells();
    }
    if ($columnCnt === 0) {
        /* 取得最大的列号 */
        $columnH = $currSheet->getHighestColumn();
        /* 兼容原逻辑,循环时使用的是小于等于 */
        $columnCnt = Coordinate::columnIndexFromString($columnH);
    }
    // 获取总行数
    $count = $currSheet->getHighestRow();
    $data = [];
    // 读取内容
    for ($i = 2; $i <= $count; $i++) {
        $isNull = true;
        $m = 0;
        for ($n = 1; $n <= $columnCnt; $n++) {
            $cellName = Coordinate::stringFromColumnIndex($n);
            $cellId = $cellName . $i;
            $cell = $currSheet->getCell($cellId);
            if (isset($options['format'])) {
                /* 获取格式 */
                $format = $cell->getStyle()->getNumberFormat()->getFormatCode();
                /* 记录格式 */
                $options['format'][$i][$cellName] = $format;
            }
            if (isset($options['formula'])) {
                /* 获取公式,公式均为=号开头数据 */
                $formula = $currSheet->getCell($cellId)->getValue();
                if (0 === strpos($formula, '=')) {
                    $options['formula'][$cellName . $i] = $formula;
                }
            }
            if (isset($format) && 'm/d/yyyy' == $format) {
                /* 日期格式翻转处理 */
                $cell->getStyle()->getNumberFormat()->setFormatCode('yyyy/mm/dd');
            }
            $data[$i][$m] = trim($currSheet->getCell($cellId)->getFormattedValue());
            if (!empty($data[$i][$m])) {
                $isNull = false;
            }
            $m++;
        }
        /* 判断是否整行数据为空,是的话删除该行数据 */
        if ($isNull) {
            unset($data[$i]);
        }
    }
    return $data;
}

  

 

posted @   MldyFre  阅读(522)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示