教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 phpexcel导入excel到数据库的代码

phpexcel导入excel到数据库的代码

发布时间:2016-12-01   编辑:jiaochengji.com
本文分享下,PHP使用phpexcel导入数据库的代码,有兴趣研究phpexcel的朋友作个参考,还是不错的。

phpexcel实现导入内容到数据库中,代码如下:

<?php
/**
* phpexcel实例 导入数据库
* by www.jbxue.com
*/
error_reporting(E_ALL); //开启错误 
set_time_limit(0); //脚本不超时 

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

/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . 'http://www.jbxue.com/../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 />'; 
        } 
?>

您可能感兴趣的文章:
PHPExcel常用方法举例
PHP导出EXCEL的简单范例 使用phpexcel类库导出excel
phpExcel类的使用方法分享
phpexcel导出excel的经典实例
PHPExcel读取excel文件的例子
phpexcel类库实例 支持(excel2003 excel2007)
phpexcel导出数据的实例代码
phpexcel快速开发指南(不错)
phpExcel中文帮助手册(知识点)
使用PHPExcel判别和格式化Excel中的日期格式的例子
phpexcel导出excel的颜色与网页中颜色不一致的解决方法
CI中使用PHPExcel导出数据到Excel

您可能感兴趣的文章:
phpexcel导出数据的实例代码
phpexcel导入excel到数据库的代码
PHP导出EXCEL的简单范例 使用phpexcel类库导出excel
PHP导出excel php使用phpexcel导出excel文件
解决php下载excel无法打开的问题
php导入excel php使用phpexcel导入excel文件
CI中使用PHPExcel导出数据到Excel
PHPExcel读取excel文件的例子
PHP通过PHPExcel类导入导出excel
PHPExcel实例代码 phpexcel类库示例

关键词: PHPExcel  导入excel  导入数据库   
[关闭]
~ ~