<?php
//设置请求头
header("Content-Type:text/html;charset=utf8");
header("Access-Control-Allow-Origin: *"); //解决跨域
header('Access-Control-Allow-Methods:GET');// 响应类型
header('Access-Control-Allow-Headers:*'); // 响应头设置
//连接数据库
$dbms='mysql'; //数据库类型
$host='XXX'; //数据库主机名
$dbName='XXX'; //使用的数据库
$user='root'; //数据库连接用户名
$pass='XXX'; //对应的密码
$dsn="$dbms:host=$host;dbname=$dbName";
try {
$dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象
echo "连接成功<br/>";
} catch (PDOException $e) {
die ("Error!: " . $e->getMessage() . "<br/>");
}
set_time_limit(0);
error_reporting(0);
// https://codeload.github.com/PHPOffice/PHPExcel/zip/1.8下载压缩包
include './PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';
include './PHPExcel-1.8/Classes/PHPExcel.php';
$inputFileName = 'read.xls';
// date_default_timezone_set('Asia/Shanghai');
// 读取excel文件
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
die('加载文件发生错误:"'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
// 确定要读取的sheet
$sheet = $objPHPExcel->getSheet();
// 取得总行数
$highestRow = $sheet->getHighestRow();
// 取得总列数
$highestColumn = $sheet->getHighestColumn();
// 获取行的数据
for($j=2;$j<=$highestRow;$j++){
$str="";
//从A列读取数据
for($k='A';$k!=$highestColumn;$k++){
$str .=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue().'|*|';//读取单元格
}
$strs = explode("|*|",$str);
// echo("<pre>");
// print_r($strs);
echo $strs[2] . "<br />";
$sql = " insert into ec_tank_rule ( ";
$sql .= " A, ";
$sql .= " B, ";
$sql .= " C ";
$sql .= " values ( ";
$sql .= " '$strs[1]', ";
$sql .= " '$strs[2]', ";
$sql .= " '$strs[3]' ";
$sql .= " ) ";
$count = $dbh->exec($sql);
}
?>