<?php
include("conn.php"); //链接数据库
require_once 'PHPExcel/Classes/PHPExcel.php';
require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
require_once 'PHPExcel/Classes/PHPExcel/Reader/Excel5.php';
header("Content-type: text/html; charset=utf-8");
$path = $_FILES['file']['tmp_name'];
//print_r($path);die;
$objReader = PHPExcel_IOFactory::createReader('Excel2007'); //use Excel5 for 2003 format
$objPHPExcel = $objReader->load($path);
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); //取得总行数
$highestColumn = $sheet->getHighestColumn(); //取得总列数
for($j=2;$j<=$highestRow;$j++) //从第二行开始读取数据
{
$str="";
for($k='A';$k<=$highestColumn;$k++) //从A列读取数据
{
$str .=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue().',';//读取单元格
}
// var_dump($str);die;
$str=mb_convert_encoding($str,'UTF-8',"auto");//根据自己编码修改
// $s = iconv('GBK', 'UTF-8', $str);
$strs = explode(",",$str);
// print_r($strs);die;
$sql = "insert into data (contact_person,mobile_phone,address) values ('$strs[0]','$strs[1]','$strs[2]')";
//echo $sql;
if(!mysqli_query($connect,$sql))
{
echo 'no';
}
}
echo "导入成功";
conn.php
$connect = mysqli_connect('ip','root','密码','dnmalldev') or die('数据库服务器连接错误');