PHP 如何利用phpexcel导入数据库

yipeiwu_com4年前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设计模式 Decorator(装饰模式)

复制代码 代码如下: <?php /** * 装饰模式 * * 动态的给一个对象添加一些额外的职责,就扩展功能而言比生成子类方式更为灵活 */ header("Content-ty...

php下检测字符串是否是utf8编码的代码

 function is_utf8($string) {      return preg_ma...

php文件读取方法实例分析

本文实例讲述了php文件读取方法。分享给大家供大家参考。具体如下: <?php $file = fopen("Test//file.txt", "r"); //打开文...

php多进程模拟并发事务产生的问题小结

前言 本文通过实例代码给大家介绍了关于php多进程模拟并发事务产生的一些问题,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧 表 drop table if exi...

php表单提交与$_POST实例分析

php表单提交与$_POST实例分析

本文实例分析了php的表单提交与$_POST。分享给大家供大家参考。具体如下: 这里要注意:表单 checkbox 的 name 需要以数组形式来命名。 表单文件:index.php 复...