zoukankan      html  css  js  c++  java
  • 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);
        ...
    ?>
  • 相关阅读:
    Python 多线程、进程
    Python网络编程 Socket编程
    Python基础7 面向对象编程进阶
    Python基础6 面向对象编程
    Python基础5 常用模块学习
    Python基础4 迭代器、装饰器、软件开发规范
    Python基础3 函数、递归、内置函数
    Python基础2 列表 字典 集合
    21-Python-多进程
    20-Python-queue队列
  • 原文地址:https://www.cnblogs.com/developd/p/4431091.html
Copyright © 2011-2022 走看看