PHP 如何利用phpexcel导入数据库

yipeiwu_com5年前PHP代码库
废话不多说,直接上代码吧
复制代码 代码如下:

<?php
error_reporting(E_ALL); //开启错误
set_time_limit(0); //脚本不超时

date_default_timezone_set('Europe/London'); //设置时间

/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '//www.jb51.net/../Classes/');//设置环境变量

/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';

//$inputFileType = 'Excel5';    //这个是读 xls的
    $inputFileType = 'Excel2007';//这个是计xlsx的
//$inputFileName = './sampleData/example2.xls';
$inputFileName = './sampleData/book.xlsx';

        echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcel = $objReader->load($inputFileName);
        /*
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow(); //取得总行数
        $highestColumn = $sheet->getHighestColumn(); //取得总列
        */   
        $objWorksheet = $objPHPExcel->getActiveSheet();//取得总行数
        $highestRow = $objWorksheet->getHighestRow();//取得总列数

        echo 'highestRow='.$highestRow;
        echo "<br>";
        $highestColumn = $objWorksheet->getHighestColumn();
        $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
        echo 'highestColumnIndex='.$highestColumnIndex;
        echo "<br />";
        $headtitle=array();
        for ($row = 1;$row <= $highestRow;$row++)
        {
            $strs=array();
            //注意highestColumnIndex的列数索引从0开始
            for ($col = 0;$col < $highestColumnIndex;$col++)
            { 
                $strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
            } 
              $info = array(
                      'word1'=>"$strs[0]",
                    'word2'=>"$strs[1]",
                    'word3'=>"$strs[2]",
                    'word4'=>"$strs[3]",
              );
              //在这儿,你可以连接,你的数据库,写入数据库了
              print_r($info);
              echo '<br />';
        }
?>

相关文章

PHP实现简单搜歌的方法

本文实例讲述了PHP实现简单搜歌的方法。分享给大家供大家参考。具体实现方法如下: <form name="" method="post" action=""> <i...

PHP封装函数实现生成随机的字符串验证码

前言 一般情况下我们在做程序的时候肯定会有很多地方使用到随机字符串、比如做验证码用到的、然后就把这个函数封装起来、使用时候要设置2个参数、原理是随机抓取字符串、对字符串进行拼接 $st...

PHP 面向对象详解

对象的主要三个特性 对象的行为:可以对 对象施加那些操作,开灯,关灯就是行为。 对象的形态:当施加那些方法是对象如何响应,颜色,尺寸,外型。 对象的表示:对象的表示就相当于身份证,具体区...

PHP5 面向对象程序设计

PHP5有一个单重继承的,限制访问的,可以重载的对象模型. 本章稍后会详细讨论的”继承”,包含类间的父-子关系. 另外,PHP支持对属性和方法的限制性访问. ...

PHP依赖注入原理与用法分析

本文实例讲述了PHP依赖注入原理与用法。分享给大家供大家参考,具体如下: 引言 依然是来自到喜啦的一道面试题,你知道什么是依赖注入吗? 依赖注入(DI)的概念虽然听起来很深奥,但是如果你...