PHPExcel 读取文件内容

//声明header头部
header("Content-type:text/html;charset=utf-8");
//引入类库
//include "Classes/PHPExcel/IOFactory.php";
include __DIR__ . "/PHPExcel/Classes/PHPExcel/IOFactory.php";

date_default_timezone_set('PRC');
//TODO 导入数组开关
$data = array();

//elsx文件路径
$file = __DIR__ . '/20220510.xlsx';
//$file = __DIR__ . '/sx_0903.csv';
$allData = read_execl($file);



// 读取excel文件
function read_execl($inputFileName)
{
    try {
        $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcel = $objReader->load($inputFileName);
    } catch (Exception $e) {
        echo "读取失败";
    }

    // 确定要读取的sheet,从0开始,0即第一个表,1即第二个表,...
    $sheet = $objPHPExcel->getSheet(0);// 0即第一个表
    $highestRow = $sheet->getHighestRow();// 取得总行数
    $highestColumn = $sheet->getHighestColumn(); // 取得总列数

    // 获取excel文件的数据,$row=2代表从第二行开始获取数据
    for ($row = 2; $row <= $highestRow; $row++) {
//        // rowData是一行的数据,遍历出来就是一列数据,字母A代表遍历所有列的数据,字母B开始就是单独遍历那一列的数据,我这里填写的C就是获取C列的数据
//        $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
//        //$rowData是一个嵌套数组,需要这样输出
//        echo $rowData['0']['0'];
        if (empty($sheet->getCell('A' . $row)->getValue())) {
            break;
        }
        $data[] = array(
            'content' =>  $sheet->getCell('A' . $row)->getValue(),
            'city' => $sheet->getCell('B' . $row)->getValue(),
            'hid' =>  $sheet->getCell('C' . $row)->getValue(),
            'create_time' => $sheet->getCell('D' . $row)->getValue(),
        );
    }
    return $data;
}

posted @   码农编程进阶笔记  阅读(92)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2021-10-20 Centos 根据端口查看占用进程 根据进程查看占用端口
2021-10-20 Elasticsearch 之Mapping设置
2021-10-20 实战!聊聊PHP如何使用 ElasticSearch 做搜索
2018-10-20 最完美解决Nginx部署ThinkPHP项目的办法
2018-10-20 最完美解决Nginx部署ThinkPHP项目的办法
2018-10-20 nginx 80端口重定向到443端口
2018-10-20 nginx 80端口重定向到443端口
返回顶部 有事您Q我

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示