zoukankan      html  css  js  c++  java
  • PHPExcel读取excel文件示例

    PHPExcel的类库下载地址:  https://github.com/PHPOffice/PHPExcel

    转载自: http://www.imhdr.com/1332/comment-page-1/

    PHPExcel是一个非常方便生成Excel格式文件的类,官方下载包中带有大量如何生成各种样式excel文件的示例,但没有一个读取Excel文件的完整例子.Xiaoqiang根据网上的资料,整理了一份简单读取Excel文件的例子.
    传统方法:

    <?php
    /**
     *
     * @copyright 2007-2012 Xiaoqiang.
     * @author Xiaoqiang.Wu <jamblues@gmail.com>
     * @version 1.01
     */
     
    error_reporting(E_ALL);
     
    date_default_timezone_set('Asia/ShangHai');
     
    /** PHPExcel_IOFactory */
    require_once '../Classes/PHPExcel/IOFactory.php';
     
     
    // Check prerequisites
    if (!file_exists("31excel5.xls")) {
        exit("not found 31excel5.xls.
    ");
    }
     
    $reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
    $PHPExcel = $reader->load("31excel5.xls"); // 载入excel文件
    $sheet = $PHPExcel->getSheet(0); // 读取第一個工作表
    $highestRow = $sheet->getHighestRow(); // 取得总行数
    $highestColumm = $sheet->getHighestColumn(); // 取得总列数
    $highestColumm= PHPExcel_Cell::columnIndexFromString($colsNum); //字母列转换为数字列 如:AA变为27
     
    /** 循环读取每个单元格的数据 */
    for ($row = 1; $row <= $highestRow; $row++){//行数是以第1行开始
        for ($column = 0; $column < $highestColumm; $column++) {//列数是以第0列开始
            $columnName = PHPExcel_Cell::stringFromColumnIndex($column);
            echo $columnName.$row.":".$sheet->getCellByColumnAndRow($column, $row)->getValue()."<br />";
        }
    }
     
    ?>

    精简方法:

    <?php
    /**
     *
     * @copyright 2007-2012 Xiaoqiang.
     * @author Xiaoqiang.Wu <jamblues@gmail.com>
     * @version 1.01
     */
     
    error_reporting(E_ALL);
     
    date_default_timezone_set('Asia/ShangHai');
     
    /** PHPExcel_IOFactory */
    require_once '../Classes/PHPExcel/IOFactory.php';
     
     
    // Check prerequisites
    if (!file_exists("31excel5.xls")) {
        exit("not found 31excel5.xls.
    ");
    }
     
    $reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
    $PHPExcel = $reader->load("31excel5.xls"); // 载入excel文件
    $sheet = $PHPExcel->getSheet(0); // 读取第一個工作表
    $highestRow = $sheet->getHighestRow(); // 取得总行数
    $highestColumm = $sheet->getHighestColumn(); // 取得总列数
     
    /** 循环读取每个单元格的数据 */
    for ($row = 1; $row <= $highestRow; $row++){//行数是以第1行开始
        for ($column = 'A'; $column <= $highestColumm; $column++) {//列数是以A列开始
            $dataset[] = $sheet->getCell($column.$row)->getValue();
            echo $column.$row.":".$sheet->getCell($column.$row)->getValue()."<br />";
        }
    }
     
    ?>
  • 相关阅读:
    [zz]std::vector,std::deque,std::list的区别的使用
    [zz]有关写c++代码的习惯
    [zz]Ubuntu linux 基本操作 双网卡双IP配置
    [zz]ZooKeeper 典型的应用场景
    [zz]Ubuntu配置双网卡
    java配置文件问题
    Struts2知识积累(2)_核心概念:拦截器
    【双旦献礼】PortalBasic Java Web 应用开发框架 v3.0.1 正式发布(源码、示例及文档)
    hibernate入门
    为Eclipse定制你自己的注释模板变量
  • 原文地址:https://www.cnblogs.com/mingaixin/p/4746454.html
Copyright © 2011-2022 走看看