PHPExcel實現(xiàn)的讀取多工作表操作示例
本文實例講述了PHPExcel實現(xiàn)的讀取多工作表操作。分享給大家供大家參考,具體如下:
最近我們公司crm模塊需要優(yōu)化一下客戶導(dǎo)入功能,之前的要求是:只需要從單個工作表中獲取數(shù)據(jù);現(xiàn)在的要求是:需要在多個工作表中獲取對應(yīng)數(shù)據(jù),并導(dǎo)入數(shù)據(jù)庫;
幸虧PHPExcel已經(jīng)給我們提供了獲取多個sheet工作表的方法。下面直接上代碼:【這里我用的是tp3.2.3】
/** * 導(dǎo)入excel文件 * @param string $file excel文件路徑 * @return array excel文件內(nèi)容數(shù)組 */ function import_excel($file){ // 判斷文件是什么格式 $type = pathinfo($file); $type = strtolower($type["extension"]); if ($type=='xlsx') { $type='Excel2007'; }elseif($type=='xls') { $type = 'Excel5'; } ini_set('max_execution_time', '0'); Vendor('PHPExcel.PHPExcel'); $objReader = PHPExcel_IOFactory::createReader($type);//判斷使用哪種格式 $objReader ->setReadDataOnly(true); //只讀取數(shù)據(jù),會智能忽略所有空白行,這點很重要?。?! $objPHPExcel = $objReader->load($file); //加載Excel文件 $sheetCount = $objPHPExcel->getSheetCount();//獲取sheet工作表總個數(shù) $rowData = array(); $RowNum = 0; /*讀取表格數(shù)據(jù)*/ for($i =0;$i <= $sheetCount-1;$i++){//循環(huán)sheet工作表的總個數(shù) $sheet = $objPHPExcel->getSheet($i); $highestRow = $sheet->getHighestRow(); $RowNum += $highestRow-1;//計算所有sheet的總行數(shù) $highestColumn = $sheet->getHighestColumn(); //從第$i個sheet的第1行開始獲取數(shù)據(jù) for($row = 1;$row <= $highestRow;$row++){ //把每個sheet作為一個新的數(shù)組元素 鍵名以sheet的索引命名 利于后期數(shù)組的提取 $rowData[$i][] = arrToOne($sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE)); } } /*刪除每行表頭數(shù)據(jù)*/ foreach($rowData as $k=>$v){ array_shift($rowData[$k]); } echo '<pre>'; print_r($rowData);//打印結(jié)果 echo '</pre>'; return array("RowNum" => $RowNum,"Excel_Data" => $rowData); }
Excel截圖如下:
打印結(jié)果如下:0對應(yīng)第一個sheet;以此類推第3個就是最后一個sheet;
注意:當某一個sheet不存在任何數(shù)據(jù)時,我這里會創(chuàng)建該元素為空數(shù)組,比如下面結(jié)果的第3個元素 ;所以在插入數(shù)據(jù)庫操作的時候需要再判斷一下是否為空!
<pre>Array
(
[0] => Array
(
[0] => Array
(
[0] => 測試數(shù)據(jù)001
[1] =>
[2] => 聯(lián)系人1
[3] => 女
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
)
[1] => Array
(
[0] => 測試數(shù)據(jù)002
[1] =>
[2] => 聯(lián)系人2
[3] => 女
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
)
)
[1] => Array
(
[0] => Array
(
[0] => 測試數(shù)據(jù)014
[1] =>
[2] => 聯(lián)系人13
[3] => 女
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
)
)
[2] => Array
(
[0] => Array
(
[0] => 測試數(shù)據(jù)015
[1] =>
[2] => 聯(lián)系人13
[3] => 女
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
[9] =>
[10] =>
[11] =>
[12] =>
)
)
[3] => Array
(
)
)
</pre>
至此, 以上就成功獲取到了每一個sheet的數(shù)據(jù);
PS:關(guān)于PHPExcel文件可至此下載 http://www.dbjr.com.cn/codes/194070.html。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計算法總結(jié)》、《PHP數(shù)學(xué)運算技巧總結(jié)》、《php正則表達式用法總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
php獲取從百度搜索進入網(wǎng)站的關(guān)鍵詞的詳細代碼
以下是關(guān)于php該如何獲取從百度搜索進入網(wǎng)站的關(guān)鍵詞的詳細代碼,希望本文對廣大php開發(fā)者有所幫助2014-01-01關(guān)于查看MSSQL 數(shù)據(jù)庫 用戶每個表 占用的空間大小
本篇文章是對查看MSSQL數(shù)據(jù)庫用戶每個表占用的空間大小進行了詳細的分析介紹,需要的朋友參考下2013-06-06PHP使用json_encode函數(shù)時不轉(zhuǎn)義中文的解決方法
這篇文章主要介紹了PHP使用json_encode函數(shù)時不轉(zhuǎn)義中文的解決方法,給出一個自定義函數(shù)代替json_encode函數(shù)的功能,是非常實用的技巧,需要的朋友可以參考下2014-11-11PHP實現(xiàn)網(wǎng)頁內(nèi)容html標簽補全和過濾的方法小結(jié)【2種方法】
這篇文章主要介紹了PHP實現(xiàn)網(wǎng)頁內(nèi)容html標簽補全和過濾的方法,結(jié)合實例形式分析了php常見的標簽檢查、補全、閉合、過濾等相關(guān)操作技巧,需要的朋友可以參考下2017-04-04