使用php-excel-reader讀取excel文件
來源:程序員人生 發(fā)布時(shí)間:2016-06-30 16:40:30 閱讀次數(shù):2913次
有時(shí)候如果有大量的數(shù)據(jù)需要導(dǎo)入到數(shù)據(jù)庫,最低級(jí)的辦法就是,1個(gè)1個(gè)的手動(dòng)添加,而平常生活中,常經(jīng)常使用表格來記錄,能不能讓PHP直接讀取1個(gè)excel表格,然后,將表格中的內(nèi)容,全部導(dǎo)入數(shù)據(jù)庫呢,這模樣,可以節(jié)省大量的時(shí)間。
php-excel-reader是1個(gè)讀取excel的類,可以很輕松的使用它讀取excel文件。
首先要下載有關(guān)的文件:
鏈接:http://pan.baidu.com/s/1i5990hv 密碼:4npd

其余文件為事例文件,請認(rèn)真分析源碼。
表格對應(yīng)內(nèi)容:
1:引入類,創(chuàng)建對象,設(shè)置讀取文件的目錄
<?php
error_reporting(E_ALL ^ E_NOTICE);
require_once 'excel_reader2.php';
$data = new Spreadsheet_Excel_Reader();//創(chuàng)建對象
$data->setOutputEncoding('UTF⑻');//設(shè)置編碼格式
$data->read("example.xls");//讀取excel文檔
2:讀取終了后,會(huì)將表格有關(guān)的信息,全部存到1個(gè)大數(shù)組中。
<?php
error_reporting(E_ALL ^ E_NOTICE);
require_once 'excel_reader2.php';
$data = new Spreadsheet_Excel_Reader();//創(chuàng)建對象
$data->setOutputEncoding('UTF⑻');//設(shè)置編碼格式
$data->read("example.xls");//讀取excel文檔
echo "<pre>";
print_r($data->sheets);
echo "</pre>";
運(yùn)行結(jié)果以下

3:如果要讀取,數(shù)組中的詳細(xì)內(nèi)容,給出幾個(gè)例子
<?php
error_reporting(E_ALL ^ E_NOTICE);
require_once 'excel_reader2.php';
$data = new Spreadsheet_Excel_Reader();//創(chuàng)建對象
$data->setOutputEncoding('UTF⑻');//設(shè)置編碼格式
$data->read("example.xls");//讀取excel文檔
//echo "<pre>";
//print_r($data->sheets);
//echo "</pre>";
echo $data->sheets[0]['numRows']."行<br>";//讀出1共幾行
echo $data->sheets[0]['numCols']."列<br>";//讀出1共幾列
echo $data->sheets[0]['cells'][1][1]."<br>";//讀出第1行第1列的內(nèi)容
print_r($data->sheets[0]['cells'][1]);//第1行的數(shù)據(jù)
echo "<br>";
echo implode(",",$data->sheets[0]['cells'][1])."<br>";//去除第1行的數(shù)據(jù),每一個(gè)中間添加分隔符
for($i=1;$i<=$data->sheets[0]['numCols'];$i++)//1次讀出第1行的所有數(shù)據(jù)
{
echo $data->sheets[0]['cells'][1][$i]." ";
}
echo "<br>";
echo "<br>";
//讀出所有數(shù)據(jù)
for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
//$data->sheets[0]['numCols']為Excel列數(shù)
for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
//顯示每一個(gè)單元格內(nèi)容
echo $data->sheets[0]['cells'][$i][$j].' ';
}
echo '<br>';
}
注意上述,echo implode(",",$data->sheets[0]['cells'][1])."<br>";//去除第1行的數(shù)據(jù),每一個(gè)中間添加分隔符,,的利用,這樣就能夠,直接向數(shù)據(jù)庫插入1整行的數(shù)據(jù)了
注:
dump(),它可以將excel內(nèi)容以html格式輸出:
echo $data->dump(true,true);
<?php
error_reporting(E_ALL ^ E_NOTICE);
require_once 'excel_reader2.php';
$data = new Spreadsheet_Excel_Reader("example.xls");
?>
<html>
<head>
<style>
table.excel {
border-style:ridge;
border-width:1;
border-collapse:collapse;
font-family:sans-serif;
font-size:12px;
}
table.excel thead th, table.excel tbody th {
background:#CCCCCC;
border-style:ridge;
border-width:1;
text-align: center;
vertical-align:bottom;
}
table.excel tbody th {
text-align:center;
width:20px;
}
table.excel tbody td {
vertical-align:bottom;
}
table.excel tbody td {
padding: 0 3px;
border: 1px solid #EEEEEE;
}
</style>
</head>
<body>
<?php echo $data->dump(true,true); ?>
</body>
</html>
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)