Phpexcel importing Excel Tabular data into a database

Source: Internet
Author: User
Tags dsn

index.php
<form name= "Frm1" enctype= "Multipart/form-data" action= "insertdb.php" method= "POST" >
<input name= "filename" type= "file"/><input name= "Submit" type= "Submit" value= "Import"/>
</form>

insertdb.php

<?php
Header ("Content-type:text/html;charset:utf-8");
Global variables
$SUCC _result=0;
$error _result=0;
@ $file =$_files[' filename '];
$max _size= "2000000"; Maximum file limit (in bytes)
$fname = $file [' name '];
$ftype =strtolower (substr (STRRCHR ($fname, '. '), 1));

File format
$uploadfile = $file [' Tmp_name '];

if ($_server[' Request_method ']== ' POST ') {
if (Is_uploaded_file ($uploadfile)) {
if ($file [' Size ']> $max _size) {
echo "Import file is too large";
Exit
}
if ($ftype! = ' xls ') {
echo "Import file type is error";
Exit
}
}else{
echo "The file is not empty!";
Exit
}
}
$DSN = "Mysql:dbname=me;host=127.0.0.1;charset=utf8";
$user = "root";
$pwd = ' root ';
$pdo =new PDO ($DSN, $user, $pwd); Connect to MySQL Database

Calling the Phpexcel class library
Require_once ' classes/phpexcel.php ';
Require_once ' classes/phpexcel/iofactory.php ';
Require_once ' classes/phpexcel/reader/excel5.php ';

$objReader = Phpexcel_iofactory::createreader (' Excel2007 ');//use excel2007 for

$objPHPExcel = $objReader->load ($uploadfile);

$sheet = $objPHPExcel->getsheet (0);
$highestRow = $sheet->gethighestrow (); Total number of rows obtained
$highestColumn = $sheet->gethighestcolumn (); Total number of columns obtained
$arr _result=array ();
$strs =array ();

for ($j =2; $j <= $highestRow; $j + +)
{
unset ($arr _result);
Unset ($strs);
for ($k = ' A '; $k <= $highestColumn; $k + +)
{
Reading cells
@ $arr _result. = $objPHPExcel->getactivesheet ()->getcell ("$k $j")->getvalue (). ', ';
}
$strs =explode (",", $arr _result);
$sql = "INSERT into goods (id,name,content,decs) VALUES ($strs [0], ' $strs [1] ', ' $strs [2] ', $strs [3])";
echo $sql. " <br/> ";

$result = $pdo->exec ($sql) or Die ("execution error");

if ($result >0) {
$SUCC _result+=1;
}else{
$error _result+=1;
}

}

echo "Insert succeeded". $SUCC _result. " Bar data!!! <br> ";
echo "Insert Failed". $error _result. " Bar data!!! ";

?>

Phpexcel importing Excel Tabular data into a database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.