• PHP处理Excel 数据


    0x01.准备:

    下载phpExcelReader URL:http://sourceforge.net/projects/phpexcelreader/

    文件包含: 示例文件example/example2.php 核心文件Excel/reader.php,oleread.inc

    reader.php 主类 ,oleread.inc 预配置文件

    修改reader.php文件中第31行(或附近) 

    修改require_once 'Spreadsheet/Excel/Reader/OLERead.php';为require_once'oleread.inc'; 否则报错(因为不存在oleread.php)

    0x02.使用:

    example.php中有示例 需要注意:

    $data->setOutptEncoding('cp936');

    使用繁体的话可以修改为CP950、日文是CP932,具体可参考codepage说明

    excel文件尽量选择xls格式的

    代码参照:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?php
        require_once 'Excel/reader.php'
        $filename 'xxx.xls';  //设置要读取的Excel文件
        $data new Spreadsheet_Excel_Reader(); //实例化
        $data->setOutputEncoding('cp936');  //设置输出编码格式
        $data->read($filename);
        for($i=1;$i<=$data->sheets[0]['numRows'];$i++){  //分别遍历行 列
            for($j=1;$j<=$data->sheets[0]['numCols'];$j++){
                echo $data->sheets[0]['cells'][$i][$j];
            }
        }
    ?>

    $data->sheets[0]['cells']  cells取出单元格中的值 。

    以上只是简单示例。

    假如读取excel表中的数据,一般列值是固定的,可采用数组的方式存储

    列值:id,name,passwd,sex,email,age,phone numbers,address

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <?php
        require_once 'Excel/reader.php'
        $filename 'xxx.xls';  //设置要读取的Excel文件
        $data new Spreadsheet_Excel_Reader(); //实例化
        $data->setOutputEncoding('cp936');  //设置输出编码格式
        $data->read($filename);
        for($i=1;$i<=$data->sheets[0]['numRows'];$i++){
            $id $data->sheets[0]['cells'][1];
            $name $data->sheets[0]['cells'][2];
            $passwd $data->sheets[0]['cells'][3];
            ....
        }
        //MySQL 实例化
        $sql "insert into xx set id='$id',name='$name',...... ";
        mysql_query($sql,$conn);
        ...
    ?>
  • 相关阅读:
    Winform架构
    SQL2008多数据库查询表或删表
    hdu1114 PiggyBank
    hdu2844 Coins
    hdu2602 Bone Collector
    hdu2191 珍惜现在
    hdu1203 I NEED A OFFER!
    求推荐
    TransparentBlt
    双缓冲加载位图 WinCE
  • 原文地址:https://www.cnblogs.com/developd/p/4431091.html
Copyright © 2020-2023  润新知