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 class GetPingYing {     private $pylist = array...

PHP过滤黑名单关键字的方法

本文实例讲述了PHP过滤黑名单关键字的方法。分享给大家供大家参考。具体实现方法如下: 关键字过滤非常的简单把要过滤的内容存在数组或文档中,然后用户提交时我们进行preg_match或is...

PHP实现的字符串匹配算法示例【sunday算法】

本文实例讲述了PHP实现的字符串匹配算法————sunday算法。分享给大家供大家参考,具体如下: Sunday算法是Daniel M.Sunday于1990年提出的字符串模式匹配。其核...

php实现通用的信用卡验证类

本文实例讲述了php实现通用的信用卡验证类。分享给大家供大家参考。 原文说明如下: Credit Card Validation Solution (PHP Edition) Vers...

Apache中php.ini的设置方法

例如:复制代码 代码如下:1 LoadModule php5_module "D:/wamp/bin/php/php5.4.3/php5apache2_2.dll"2 PHPIniDir...