PHP batch import excell spreadsheets to mysql database

  PHP batch import excell spreadsheets to mysql database, I have personally tested by here for everyone to share

1, download php excell library

    Online search can be downloaded here do not write the address

2, built html file

  

<form method="post" action="http://www.96net.com.cn/index.php?c=good&a=index" name="theForm" onsubmit="return validate()" enctype="multipart/form-data">
<table cellspacing="1" cellpadding="3" width="100%">

<TR>
<td class = "label"> Please select you want to upload EXCELL: </ td>
<td> Excell download the template </ td>
</ TR>

<TR>
<td class = "label"> Please select you want to upload Excell: </ td>
<td> <the INPUT of the type = "File" name = "myfile"> </ td>
</ TR>

<tr>
<td colspan="2" align="center"><br />
<input type="submit" class="button" value="提交" />
</td>
</tr>

</table>
</form>

3, php codes are written

// bulk upload
function upExecel () {

// determines whether to select a table to be uploaded
IF (empty ($ _ the POST [ 'myfile'])) {
echo "<Script> Alert (not choose your tables); history.go (-1); </ Script> ";
}

$ FILE_SIZE = $ _FILES [ 'myfile'] [ 'size'];
IF ($ FILE_SIZE> 1024 * 1024 *. 5) {
echo" <Script> Alert ( 'failed to upload , can not exceed the size of the uploaded table of 5M '); history.go (-1); </ Script> ";
Exit ();
}

// limit upload form type
$ file_type = $ _FILES [' myfile '] [' type '];
//application/vnd.ms-excel xls file type for the
// IF (! $ FILE_TYPE =' file application / vnd.ms-Excel ') {
// echo "<Script> Alert (' failed to upload only Upload excel2003 the xls format ');! history.go (-1) </ Script> ";
// Exit ();
//}




if (is_uploaded_file($_FILES['myfile']['tmp_name'])) {

if ($file_type=="application/vnd.ms-excel")
{
$objReader = PHPExcel_IOFactory::createReader('Excel5');
}

else
{
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
}
$filename = $_FILES['myfile']['tmp_name'];
$objPHPExcel = $objReader->load($filename);
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();

for($j=2;$j<=$highestRow;$j++)
{
$a = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();
$b = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();
$c = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();
$d = $objPHPExcel->getActiveSheet()->getCell("D".$j)->getValue();
$e = $objPHPExcel->getActiveSheet()->getCell("E".$j)->getValue();
$f = $objPHPExcel->getActiveSheet()->getCell("F".$j)->getValue();
$g = $objPHPExcel->getActiveSheet()->getCell("G".$j)->getValue();
$h = $objPHPExcel->getActiveSheet()->getCell("H".$j)->getValue();

// null primary key id, or null automatically increment the available added
// $ sql = "INSERT INTO house VALUES (null, '$ a', '$ b', '$ c', '$ d', '$ e ',' $ F ',' $ G ',' $ H ') ";

$ NewRow = Array (
' title '=> $ C,
' PID '=> $ B,
' huohao '=> $ A,
' guige '=> $ D,
'. price '=> $ E,
' huoc '=> $ F,
' wendu '=> $ G,
' ZBQ '=> $ H,
' Content '=> $ Content,
' upfile '= > '/upload/12.jpg',
'ADD_TIME' => Time (),
);

$ Row = spClass ( 'goods_list') -> Create ($ NewRow);

IF ($ Row) {
echo "<Script> Alert ( 'added successfully!'); window.location.href='http://www.96net.com.cn/index.php?c=goods&a=index';</script>";

}else{
echo "<script>alert('添加失败!');window.location.href='http://www.96net.com.cn/index.php?c=goods&a=ppaddpage';</script>";
exit();
}

}

}
}

Note is: xls file $ objReader = PHPExcel_IOFactory :: createReader ( 'Excel5'); xlsx file $ objReader = PHPExcel_IOFactory :: createReader ( 'Excel2007'); 

Guess you like

Origin www.cnblogs.com/96net/p/11517950.html