跳至
[1]
[全屏预览]
<?php
header("Content-Type:text/html;charset=utf-8");
$dir = $_POST['exceldir'];
/** PHPExcel_IOFactory */
require_once 'PHPExcel.php';
if (!file_exists("$dir")) {
exit("not found mb.xlsx.\n");
}
$reader = PHPExcel_IOFactory::createReader('Excel2007'); //设置以Excel5格式(Excel97-2003工作簿)
$PHPExcel = $reader->load("$dir"); // 载入excel文件
$sheet = $PHPExcel->getSheet(0); // 读取第一個工作表
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumm = $sheet->getHighestColumn(); // 取得总列数
//1.导入配置文件
require("conf/dbconfig.php");
//2.连接MySQL、并选择数据库
$link = @mysql_connect(HOST, USER, PASS) or die("数据库连接失败!");
mysql_select_db(DBNAME, $link);
//循环读取excel文件,读取一条,插入一条
for ($j = 2; $j <= $highestRow; $j++) {
$name = $PHPExcel->getActiveSheet()->getCell("A" . $j)->getValue();//获取A列的值
$telephone = $PHPExcel->getActiveSheet()->getCell("B" . $j)->getValue();//获取B列的值
$fenzu = $PHPExcel->getActiveSheet()->getCell("C" . $j)->getValue();//获取C列的值
$school = $PHPExcel->getActiveSheet()->getCell("D" . $j)->getValue();//获取D列的值
$banji = $PHPExcel->getActiveSheet()->getCell("E" . $j)->getValue();//获取E列的值
$sql = "INSERT INTO `drtable`(`id`, `name`, `telephone`, `fenzu`, `school`, `banji`) VALUES (null,'{$name}','{$telephone}','{$fenzu}','{$school}','{$banji}')";
mysql_query($sql, $link);
// echo $sql;
}
//释放工作表对象
unset($sheet);
//释放Excel文件对象
unset($PHPExcel);
//返回数据
echo "导入成功!";
//删除文件
unlink($dir);
?>
Copy after login