PHPExcel讀取excel並匯入mysql資料庫代碼_PHP教程

來源:互聯網
上載者:User
本文章來給大家介紹一款PHPExcel讀取excel並匯入資料庫代碼實現,有需要瞭解的朋友可參考,這裡我們介紹的是讀取表格之後再建立mysql串連,然後儲存到mysql資料庫。

PHPExcel是相當強大的 MS Office Excel 文檔產生類庫,當需要輸出比較複雜格式資料的時候,PHPExcel 是個不錯的選擇。不過其使用方法相對來說也就有些繁瑣

代碼如下 複製代碼

set_time_limit(20000);
ini_set('memory_limit','-1');
require_once './PHPExcel.php';
require_once './PHPExcel/IOFactory.php';
require_once './PHPExcel/Reader/Excel5.php';

//使用pdo串連資料庫
$dsn = "mysql:host=localhost;dbname=alumni;";
$user = "root";
$password = "";
try{
$dbh = new PDO($dsn,$user,$password);
$dbh->query('set names utf8;');
}catch(PDOException $e){
echo "串連失敗".$e->getMessage();
}
//pdo綁定參數操作
$stmt = $dbh->prepare("insert into alumni(gid,student_no,name) values (:gid,:student_no,:name) ");
$stmt->bindParam(":gid", $gid,PDO::PARAM_STR);
$stmt->bindParam(":student_no", $student_no,PDO::PARAM_STR);
$stmt->bindParam(":name", $name,PDO::PARAM_STR);

$objReader = new PHPExcel_Reader_Excel5(); //use excel2007
$objPHPExcel = $objReader->load('bks.xls'); //指定的檔案
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得總行數
$highestColumn = $sheet->getHighestColumn(); // 取得總列數

for($j=1;$j<=10;$j++)
{

$student_no = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//第一列學號
$name = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//第二列姓名
$gid = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();//第三列gid
}
//將擷取的excel內容插入到資料庫
$stmt->execute();
?>

http://www.bkjia.com/PHPjc/630718.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630718.htmlTechArticle本文章來給大家介紹一款PHPExcel讀取excel並匯入資料庫代碼實現,有需要瞭解的朋友可參考,這裡我們介紹的是讀取表格之後再建立mysql串連...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.