-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimport.php
65 lines (55 loc) · 1.85 KB
/
import.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/** 引入需要的类库*/
require_once 'Classes\phpexcel.php';
require_once 'Classes\PHPExcel\IOFactory.php';
require_once 'Classes\PHPExcel\Reader\Excel5.php';
//数据库连接
$link = mysqli_connect('localhost','root','','test');
if(!$link){
echo "数据库连接失败";
exit;
}
//读excel表中内容生成对应数组
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load('1.xlsx');
//读取csv文档内容
// $objReader = new PHPExcel_Reader_CSV();
// $objPHPExcel = $objReader->load('D.csv');
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
//获取文档中的值,存入数组中
$arr_result=array();
for($j=1;$j<=$highestRow;$j++)
{
for($k='A';$k<= $highestColumn;$k++)
{
//读取单元格
array_push($arr_result,($objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue()));
}
}
//数组转换成字符串,方便直接插入数据库中
$str = '';
foreach($arr_result as $item){
$strs.=$item.',';
}
for($j=2;$j<=$highestRow;$j++)
{
$a = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();//获取A列的值
$b = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();//获取B列的值
$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();
$sql = "INSERT INTO `test`(`Id`, `price`, `stock`, `num`) VALUES ('".$a."','".$b."','".$c."','".$d."')";
// mysqli_query($link,$sql);
// mysqli_query($link,'set names utf-8');
if(mysqli_query($link,$sql)){
echo 1;
}
else{
echo "导入数据失败";
echo mysqli_errno($link);
}
}
?>