|
法一: try { |
|
| 2 | $hostname = "localhost"; |
| 3 | $dbname = "test"; |
| 4 | $username = "root"; |
| 5 | $pw = "taiwan"; |
| 6 | $dbh = new PDO("mysql:host=$hostname;dbname=$dbname","$username","$pw"); |
| 7 | $dbh->query('SET NAMES "utf8"'); |
| 8 | } catch (PDOException $e) { |
| 9 | echo "Failed to get DB handle: " . $e->getMessage() . "\n"; |
| 10 | exit; |
| 11 | } |
| 12 | $res = $dbh->query("select * from tt order by t_id desc limit 1 ")->fetch(); |
| 13 | $res[t_id] = intval(doubleval(substr("$res[t_id]",1)))+1; |
| 14 | |
| 15 | //phpExcel開始 |
| 16 | ini_set('memory_limit','1024M'); |
| 17 | require_once('phpExcel/Classes/PHPExcel/IOFactory.php'); |
| 18 | $Excel = PHPExcel_IOFactory::createReader('Excel5'); |
| 19 | $Excel = PHPExcel_IOFactory::load('test.xls'); |
| 20 | //$objWorksheet = $objPHPExcel->getActiveSheet(); |
| 21 | $sheet = $Excel->getSheet(0); |
| 22 | $array_out[] = null; |
| 23 | foreach ($sheet->getRowIterator() as $row_key => $row){ |
| 24 | $cellIterator = $row->getCellIterator(); |
| 25 | $cellIterator->setIterateOnlyExistingCells(false); |
| 26 | foreach ($cellIterator as $cell_key => $cell){ |
| 27 | $array_out[$row_key][$cell_key] = $cell->getValue().''; |
| 28 | } |
| 29 | } |
| 30 | $length = count($array_out)-1; |
| 31 | for($i=1;$i<=50;$i++){ |
| 32 | for($j=0;$j<=1;$j++){ |
| 33 | if($j==0){ $a = $array_out[$i][0]; } |
| 34 | if($j==1){ $b = $array_out[$i][1]; } |
| 35 | } |
| 36 | $ins = $dbh->query("insert into tt (t_id,t_name,t_phone) values ('t$res[t_id]','$a','$b')"); |
| 37 | } |
法二
<?php
require_once ‘PHPExcel/IOFactory.php’;
$reader = PHPExcel_IOFactory::createReader(‘Excel5′); // 讀取舊版 excel 檔案
$PHPExcel = $reader->load("benefit_test.xls"); // 檔案名稱
$sheet = $PHPExcel->getSheet(0); // 讀取第一個工作表(編號從 0 開始)
$highestRow = $sheet->getHighestRow(); // 取得總列數
echo "==".$highestRow."<br />";
// 一次讀取一列
for ($row = 0; $row <= $highestRow; $row++) {for ($column = 0; $column <= 6; $column++) {
$val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
echo $val . ‘ ‘;
}
echo "<br />";}
?>
文章標籤
全站熱搜

好文章 我也寫php的