PHP中的数据库怎么使用phpexcel进行导入

发布时间:2020-12-21 16:32:40 作者:Leah
来源:亿速云 阅读:129

这篇文章将为大家详细讲解有关PHP中的数据库怎么使用phpexcel进行导入,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

复制代码 代码如下:


<?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 . 'https://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中的数据库怎么使用phpexcel进行导入就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. PHPexcel怎么导入日志
  2. thinkphp中怎样使用PHPExcel导入Excel

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

phpexcel php 数据库

上一篇:怎么在PHP项目中使用str_replace防止注入

下一篇:怎么在php中使用phpexcel对数据进行导出

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》