-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.php
39 lines (31 loc) · 1.15 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
<?php
require 'vendor/autoload.php';
include('conn.php');
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;
$reader = IOFactory::createReader('Xlsx');
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load('students.xlsx');
$worksheet = $spreadsheet->getActiveSheet();
$highestRow = $worksheet->getHighestRow();//总行数
$highestColumn = $worksheet->getHighestColumn();//总列数
$highestColumnIndex = Coordinate::columnIndexFromString($highestColumn);
$lines = $highestRow - 2;
if($lines <= 0){
exit('Excel表格中没有数据');
}
$sql = "INSERT INTO `student` (`name`,`chinese`,`maths`,`english`) VALUES ";
for($row = 3;$row<=$highestRow;++$row){
$name = $worksheet->getCellByColumnAndRow(1,$row)->getValue();
$chinese = $worksheet->getCellByColumnAndRow(2,$row)->getValue();
$maths = $worksheet->getCellByColumnAndRow(3,$row)->getValue();
$english = $worksheet->getCellByColumnAndRow(4,$row)->getValue();
$sql.="('$name','$chinese','$maths','$english'),";
}
$sql = rtrim($sql,',');
try{
$db->query($sql);
echo "OK";
}catch (Exception $e){
echo $e->getMessage();
}