PHP 如何利用phpexcel导入数据库
程序员文章站
2022-06-14 12:07:20
废话不多说,直接上代码吧复制代码 代码如下:
废话不多说,直接上代码吧
<?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
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实现利用phpexcel导出数据
下一篇: python基础教程之自定义函数介绍