首頁 > php教程 > PHP源码 > 主體

PHP excel读取excel文件转换为数组

PHP中文网
發布: 2016-05-25 16:59:03
原創
2594 人瀏覽過

php代码

/*
* 将excel转换为数组 by aibhsc
* */
require(ROOT_PATH . 'includes/PHPExcel.php');//引入PHP EXCEL类
function format_excel2array($filePath='',$sheet=0){
        if(empty($filePath) or !file_exists($filePath)){die('file not exists');}
        $PHPReader = new PHPExcel_Reader_Excel2007();        //建立reader对象
        if(!$PHPReader->canRead($filePath)){
                $PHPReader = new PHPExcel_Reader_Excel5();
                if(!$PHPReader->canRead($filePath)){
                        echo 'no Excel';
                        return ;
                }
        }
        $PHPExcel = $PHPReader->load($filePath);        //建立excel对象
        $currentSheet = $PHPExcel->getSheet($sheet);        //**读取excel文件中的指定工作表*/
        $allColumn = $currentSheet->getHighestColumn();        //**取得最大的列号*/
        $allRow = $currentSheet->getHighestRow();        //**取得一共有多少行*/
        $data = array();
        for($rowIndex=1;$rowIndex<=$allRow;$rowIndex++){        //循环读取每个单元格的内容。注意行从1开始,列从A开始
                for($colIndex=&#39;A&#39;;$colIndexgetCell($addr)->getValue();
                        if($cell instanceof PHPExcel_RichText){ //富文本转换字符串
                                $cell = $cell->__toString();
                        }
                        $data[$rowIndex][$colIndex] = $cell;
                }
        }
        return $data;
}


使用方法:
                        $filePath = ROOT_PATH.&#39;data/diamondStock.xlsx&#39;;        //钻石库存文件
                        $data = format_excel2array($filePath);
                        print_r($data);die;

输出结果示例:

Array
(
    [1] => Array
        (
            [A] => 商品编号
            [B] => 商品名称
            [C] => 总重量
            [D] => 进货价格
            [E] => 销售价格
            [F] => 4C备注
        )

    [2] => Array
        (
            [A] => 10001
            [B] => GIA-2156685995
            [C] => 0.7
            [D] => 1760
            [E] => 1848
            [F] => G色、0.7ct、SI1、FR
        )

    [3] => Array
        (
            [A] => 10002
            [B] => GIA-2156685996
            [C] => 0.7
            [D] => 1760
            [E] => 1848
            [F] => G色、0.7ct、SI1、FR
        )

    [4] => Array
        (
            [A] => 10003
            [B] => GIA-2156685997
            [C] => 0.7
            [D] => 1760
            [E] => 1848
            [F] => G色、0.7ct、SI1、FR
        )

    [5] => Array
        (
            [A] => 10004
            [B] => GIA-2156685998
            [C] => 0.7
            [D] => 1760
            [E] => 1848
            [F] => G色、0.7ct、SI1、FR
        )

    [6] => Array
        (
            [A] => 10005
            [B] => GIA-2156685999
            [C] => 0.7
            [D] => 1760
            [E] => 1848
            [F] => G色、0.7ct、SI1、FR
        )

    [7] => Array
        (
            [A] => 10006
            [B] => GIA-2156686000
            [C] => 0.7
            [D] => 1760
            [E] => 1848
            [F] => G色、0.7ct、SI1、FR
        )

    [8] => Array
        (
            [A] => 10007
            [B] => GIA-2156686001
            [C] => 0.7
            [D] => 1760
            [E] => 1848
            [F] => G色、0.7ct、SI1、FR
        )

    [9] => Array
        (
            [A] => 10008
            [B] => GIA-2156686002
            [C] => 0.7
            [D] => 1760
            [E] => 1848
            [F] => G色、0.7ct、SI1、FR
        )

    [10] => Array
        (
            [A] => 10009
            [B] => GIA-2156686003
            [C] => 0.7
            [D] => 1760
            [E] => 1848
            [F] => G色、0.7ct、SI1、FR
        )

    [11] => Array
        (
            [A] => 10010
            [B] => GIA-2156686004
            [C] => 0.7
            [D] => 1760
            [E] => 1848
            [F] => G色、0.7ct、SI1、FR
        )

    [12] => Array
        (
            [A] => 10011
            [B] => GIA-2156686005
            [C] => 0.7
            [D] => 1760
            [E] => 1848
            [F] => G色、0.7ct、SI1、FR
        )

    [13] => Array
        (
            [A] => 10012
            [B] => GIA-2156686006
            [C] => 0.7
            [D] => 1760
            [E] => 1848
            [F] => G色、0.7ct、SI1、FR
        )

    [14] => Array
        (
            [A] => 10013
            [B] => GIA-2156686007
            [C] => 0.7
            [D] => 1760
            [E] => 1848
            [F] => G色、0.7ct、SI1、FR
        )

)
登入後複製

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!