PHP 如何利用phpexcel導(dǎo)入數(shù)據(jù)庫(kù)
更新時(shí)間:2013年08月24日 10:18:15 作者:
以下是對(duì)PHP中利用phpexcel導(dǎo)入數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下
廢話不多說(shuō),直接上代碼吧
<?php
error_reporting(E_ALL); //開(kāi)啟錯(cuò)誤
set_time_limit(0); //腳本不超時(shí)
date_default_timezone_set('Europe/London'); //設(shè)置時(shí)間
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . 'http://www.dhdzp.com/../Classes/');//設(shè)置環(huán)境變量
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
//$inputFileType = 'Excel5'; //這個(gè)是讀 xls的
$inputFileType = 'Excel2007';//這個(gè)是計(jì)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(); //取得總行數(shù)
$highestColumn = $sheet->getHighestColumn(); //取得總列
*/
$objWorksheet = $objPHPExcel->getActiveSheet();//取得總行數(shù)
$highestRow = $objWorksheet->getHighestRow();//取得總列數(shù)
echo 'highestRow='.$highestRow;
echo "<br>";
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//總列數(shù)
echo 'highestColumnIndex='.$highestColumnIndex;
echo "<br />";
$headtitle=array();
for ($row = 1;$row <= $highestRow;$row++)
{
$strs=array();
//注意highestColumnIndex的列數(shù)索引從0開(kāi)始
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]",
);
//在這兒,你可以連接,你的數(shù)據(jù)庫(kù),寫(xiě)入數(shù)據(jù)庫(kù)了
print_r($info);
echo '<br />';
}
?>
復(fù)制代碼 代碼如下:
<?php
error_reporting(E_ALL); //開(kāi)啟錯(cuò)誤
set_time_limit(0); //腳本不超時(shí)
date_default_timezone_set('Europe/London'); //設(shè)置時(shí)間
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . 'http://www.dhdzp.com/../Classes/');//設(shè)置環(huán)境變量
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
//$inputFileType = 'Excel5'; //這個(gè)是讀 xls的
$inputFileType = 'Excel2007';//這個(gè)是計(jì)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(); //取得總行數(shù)
$highestColumn = $sheet->getHighestColumn(); //取得總列
*/
$objWorksheet = $objPHPExcel->getActiveSheet();//取得總行數(shù)
$highestRow = $objWorksheet->getHighestRow();//取得總列數(shù)
echo 'highestRow='.$highestRow;
echo "<br>";
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//總列數(shù)
echo 'highestColumnIndex='.$highestColumnIndex;
echo "<br />";
$headtitle=array();
for ($row = 1;$row <= $highestRow;$row++)
{
$strs=array();
//注意highestColumnIndex的列數(shù)索引從0開(kāi)始
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]",
);
//在這兒,你可以連接,你的數(shù)據(jù)庫(kù),寫(xiě)入數(shù)據(jù)庫(kù)了
print_r($info);
echo '<br />';
}
?>
相關(guān)文章
php刪除與復(fù)制文件夾及其文件夾下所有文件的實(shí)現(xiàn)代碼
最近寫(xiě)代碼,需要個(gè)復(fù)制文件夾與及其文件夾下所有文件的功能,有時(shí)候需要?jiǎng)h除,就可以用這個(gè)函數(shù)了,通過(guò)xcopy與deldir的自定義函數(shù),確實(shí)方便多了,特分享下2013-01-01
解析php中static,const與define的使用區(qū)別
本篇文章是對(duì)php中static,const與define的使用區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP中將數(shù)組轉(zhuǎn)成XML格式的實(shí)現(xiàn)代碼
網(wǎng)上找的一段代碼! 然后我自己根據(jù)php DOMDocument又寫(xiě)了一段代碼,需要的朋友可以參考下。2011-08-08
PHP回調(diào)函數(shù)與匿名函數(shù)實(shí)例詳解
這篇文章主要介紹了PHP回調(diào)函數(shù)與匿名函數(shù),結(jié)合實(shí)例形式分析了php回調(diào)函數(shù)與匿名函數(shù)的具體功能、用法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08
在數(shù)據(jù)量大(超過(guò)10萬(wàn))的情況下
在數(shù)據(jù)量大(超過(guò)10萬(wàn))的情況下...2007-01-01
PHP中通過(guò)fopen()函數(shù)訪問(wèn)遠(yuǎn)程文件示例
這篇文章主要介紹了PHP中通過(guò)fopen()函數(shù)訪問(wèn)遠(yuǎn)程文件示例,本文講解了fopen函數(shù)的作用、使用它需要的配置問(wèn)題、超時(shí)問(wèn)題等內(nèi)容,并給出了代碼實(shí)例,需要的朋友可以參考下2014-11-11

