PHP 如何利用phpexcel导入数据库

yipeiwu_com6年前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的对象注入(Object Injection)

深入讲解PHP的对象注入(Object Injection)

前言 虽然这篇文章叫做PHP对象注入,但是本质上还是和PHP的序列化的不正确使用有关。如果你阅读了PHP中的SESSION反序列化机制对序列化就会有一个大致的认识。PHP对象注入其实本质...

php 获取本机外网/公网IP的代码

大家一般都是用$_SERVER['REMOTE_ADDR']来获取用户IP, 但是如果使用了反向代理的,HTTP头中REMOTE_ADDR就不是用户的地址,反而是上一级代理的地址了。 经...

解析php时间戳与日期的转换

大家也许对PHP时间戳已经有所了解,那么我们如何应用它来获取具体的日期呢?我们今天来为大家介绍一下PHP时间戳获取当前时期的具体方式。 实现功能:获取某个日期的时间戳,或获取某个时间...

PHP高级编程之消息队列原理与实现方法详解

本文实例讲述了PHP高级编程之消息队列原理与实现方法。分享给大家供大家参考,具体如下:1. 什么是消息队列消息队列(英语:Message queue)是一种进程间通信或同一进程的不同线程间...

阿里云的WindowsServer2016上部署php+apache

阿里云的WindowsServer2016上部署php+apache

一、说明:项目需要在阿里云的WindowsServer2016上部署web环境,已经安装了Mysql,所以就不用一键安装(如phpstudy或者wamp来安装web环境了),就独立安装了...