加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP 如何利用phpexcel导入数据库

发布时间:2020-12-13 06:32:53 所属栏目:PHP教程 来源:网络整理
导读:废话不多说,直接上代码吧 div class="codetitle" a style="CURSOR: pointer" data="64002" class="copybut" id="copybut64002" onclick="doCopy('code64002')" 代码如下: div class="codebody" id="code64002" ?php error_reporting(E_ALL); //开启错误 set

废话不多说,直接上代码吧
<div class="codetitle"><a style="CURSOR: pointer" data="64002" class="copybut" id="copybut64002" onclick="doCopy('code64002')"> 代码如下:<div class="codebody" id="code64002">
<?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.52php.cn/../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,'
';
$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 "
";
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
echo 'highestColumnIndex='.$highestColumnIndex;
echo "
";
$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 '
';
}
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读