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)行了介紹,需要的朋友可以過來參考下
廢話不多說,直接上代碼吧
<?php
error_reporting(E_ALL); //開啟錯(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://chabaoo.cn/../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開始
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ù),寫入數(shù)據(jù)庫(kù)了
print_r($info);
echo '<br />';
}
?>
復(fù)制代碼 代碼如下:
<?php
error_reporting(E_ALL); //開啟錯(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://chabaoo.cn/../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開始
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ù),寫入數(shù)據(jù)庫(kù)了
print_r($info);
echo '<br />';
}
?>
相關(guān)文章
PHP 內(nèi)存緩存加速功能memcached安裝與用法
memcached 簡(jiǎn)介在很多場(chǎng)合,我們都會(huì)聽到 memcached 這個(gè)名字,但很多同學(xué)只是聽過,并沒有用過或?qū)嶋H了解過,只知道它是一個(gè)很不錯(cuò)的東東。這里簡(jiǎn)單介紹一下。2009-09-09php實(shí)現(xiàn)圖片上傳并利用ImageMagick生成縮略圖
這篇文章主要為大家詳細(xì)介紹了php實(shí)現(xiàn)圖片上傳并利用ImageMagick生成縮略圖的相關(guān)資料,需要的朋友可以參考下2016-03-03php download.php實(shí)現(xiàn)代碼 跳轉(zhuǎn)到下載文件(response.redirect)
一直對(duì)php不太熟悉,今天需要類型asp的 response.redirect語句,但一直沒有很好的解決方法。下面是問了朋友才知道的。2009-08-08用php定義一個(gè)數(shù)組最簡(jiǎn)單的方法
這篇文章主要介紹了用php定義一個(gè)數(shù)組最簡(jiǎn)單的方法,以及相關(guān)實(shí)例代碼,需要的朋友們學(xué)習(xí)下。2019-10-10PHP寫入WRITE編碼為UTF8的文件的實(shí)現(xiàn)代碼
可以把uft-8格式的文件,寫到文本中的實(shí)現(xiàn)代碼2008-07-07thinkphp操作mongo數(shù)據(jù)的三種方法
這篇文章主要給大家介紹了thinkphp操作mongo數(shù)據(jù)的三種方法,使用tp中的擴(kuò)展,使用tp中的db類和使用MongoDB PHP驅(qū)動(dòng)程序這三種方法,并通過代碼講解的非常詳細(xì),需要的朋友可以參考下2023-12-12PHP中$GLOBALS[''HTTP_RAW_POST_DATA'']和$_POST的區(qū)別分析
這篇文章主要介紹了PHP中$GLOBALS['HTTP_RAW_POST_DATA']和$_POST的區(qū)別,結(jié)合具體實(shí)例形式分析了$GLOBALS['HTTP_RAW_POST_DATA']和$_POST的功能與使用過程中的區(qū)別,需要的朋友可以參考下2017-07-07