jsp后台批量导入excel表格数据到mysql中_PHP批量导入excell表格到mysql数据库
PHP批量導(dǎo)入excell表格到mysql數(shù)據(jù)庫,本人通過親自測試,在這里分享給大家
1,下載 php? excell類庫
網(wǎng)上搜索可以下載,這里不寫地址
2,建html文件
| 請選擇你要上傳的EXCELL: | excell下載模板 |
| 請選擇你要上傳的excell: | |
3,php代碼寫入
//批量上傳操作
function upExecel(){
//判斷是否選擇了要上傳的表格
if (empty($_POST[‘myfile‘])) {
echo "";
}
$file_size = $_FILES[‘myfile‘][‘size‘];
if ($file_size>5*1024*1024) {
echo "";
exit();
}
//限制上傳表格類型
$file_type = $_FILES[‘myfile‘][‘type‘];
//application/vnd.ms-excel 為xls文件類型
//if ($file_type!=‘a(chǎn)pplication/vnd.ms-excel‘) {
//echo "";
//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 為主鍵id,自增可用null表示自動添加
//$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‘,
‘a(chǎn)dd_time‘ => time(),
);
$row=spClass(‘goods_list‘)->create($newrow);
if ($row){
echo "";
}else{
echo "";
exit();
}
}
}
}
需要注意是:xls文件? 用?$objReader = PHPExcel_IOFactory::createReader(‘Excel5‘);? xlsx文件? 用?$objReader = PHPExcel_IOFactory::createReader(‘Excel2007‘);
原文:https://www.cnblogs.com/96net/p/11517950.html
總結(jié)
以上是生活随笔為你收集整理的jsp后台批量导入excel表格数据到mysql中_PHP批量导入excell表格到mysql数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 打开win7系统事件查看器的四种方法
- 下一篇: QQLive有什么用?