php 用pdo连接数据库 实现excel批量导入

<?php


$dsn = "mysql:host=localhost;dbname=aveda3";//这就是数据源,
$user = "root";//这个是服务器的账号,我的电脑上是这样,就不知道你们的是不是,
$pwd = "root";//这是我电脑上的服务器密码,就是我没设
//$pdo = new PDO($dsn, $user, $pwd);//实例化一个PDO连接
//$pdo->query("set names gbk");//设置从数据库里面传递过来的数据的编码格式

try {
$dbh = new PDO($dsn, $user, $pwd); //初始化一个PDO对象
echo "连接成功<br/>";
} catch (PDOException $e) {
die ("Error!: " . $e->getMessage() . "<br/>");
}

include './PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';
include './PHPExcel-1.8/Classes/PHPExcel.php';
$inputFileName = 'shuju.csv';

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 = $objPHPExcel->getSheet(); // 确定要读取的sheet
$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().'|*|';//读取单元格
}
// print_r($str);die();
$strs = explode("|*|",$str);
// echo("<pre>");
// print_r($strs);
echo $strs[2] . "<br />";
$sql = " insert into oc_order ( ";
$sql .= " invoice_prefix, ";
$sql .= " store_id, ";
$sql .= " store_name, ";
$sql .= " customer_id, ";
$sql .= " customer_group_id, ";
$sql .= " store_id, ";
$sql .= " C ";

$sql .= " values ( ";
$sql .= " 'INV-2021-00', ";
$sql .= " 'Your Store', ";
$sql .= " '12', ";
$sql .= " 'Your Store', ";
$sql .= " '$strs[3]' ";
$sql .= " ) ";
$count = $dbh->exec($sql);
}

//随便记录一下 好像是未完成的 不过应该会给你思路
上一篇:php-tp5.1 导入导出


下一篇:Centos下安装docker