zoukankan      html  css  js  c++  java
  • 如何用 php 读取一个很大的 excel 文件。

    这个程序是用php 读取一个很大的excel文件,

    先将 excel 文件保存成csv 文件,

    然后利用 迭代器 逐行读取 excel 单元格的值,

    拿到值以后 做相应处理,并打印结果。

    <?php

    // 加载 GuzzleHttp 为发送 http 请求提供方便
    include_once('./vendor/autoload.php');
    $client = new GuzzleHttpClient();


        // 定义读取文件
        $file = './file/a.csv';

        function getRows($file)
        {
            $handle = fopen($file, 'rb');


            if (!$handle) {
                throw new Exception();
            }
            while (!feof($handle)) {
                 yield fgetcsv($handle);
            }
            fclose($handle);
        }


        // 获取迭代器
        $xrange = getRows($file);
        $ii = 0;

        // 迭代处理 cvs 文件中的每一行内容
        foreach ($xrange as $key => $value) {
            $value = eval('return '.iconv('gbk','utf-8',var_export($value,true)).';');

            $str = $value[4];
            $url = 'http://xxxx.xxxx.com/?api='.$str;

            $response = $client->get($url);
            $body = $response->getBody();
            $remainingBytes = $body->getContents();

            $remainingBytes = json_decode($remainingBytes,true);
            if( !empty($remainingBytes['data']) ){
                $res_ids = '';
                foreach ($remainingBytes['data'] as $kt => $vt) {
                    $res_ids .= $vt['outid'].',';
                }
                $res_ids = trim($res_ids,',');
            }else{
                $res_ids = 'error';
            }

                print_r($str);
                echo " ";
                echo " ";
                echo " ";
                print_r($value[4]);
                echo " ";
                echo " ";
                echo " ";
                print_r($res_ids);

            $file = fopen('./new_end/new_a.xls', 'a');
            fwrite($file, $res_ids." ".$str." ".$value[4]." ");
            // fclose($file);
            echo " ";
            echo " ";
            echo " ";
            // die;
            $ii++;
            echo "time_________".$ii;
            echo " ";
            echo " ";
            echo " ";
        }

     ?>

  • 相关阅读:
    【6.24校内test】T1 江城唱晚
    【6.20校内test】
    【洛谷p1983】车站分级
    【洛谷p2239】螺旋矩阵
    【6.18校内test】T2分数线划定
    【6.18校内test】T1多项式输出
    【洛谷p1981】表达式求值
    【洛谷p1077】摆花
    【洛谷p1158】导弹拦截
    博客园 api
  • 原文地址:https://www.cnblogs.com/handongyu/p/7676801.html
Copyright © 2011-2022 走看看