本文目录一览:
- 1、PHP 用PHPExcel往数据库导入大量数据
- 2、PHPExcel如何从excel表导入到mysql数据库
- 3、如何用php实现将excel导入到数据库中
- 4、thinkphp怎么把excel导入数据库
PHP 用PHPExcel往数据库导入大量数据
1、首先我们准备一个含有数据的Excel表格,表头和数据表中的表字段相对应。
2、在ThinkPHP中引入PHPExcel类库。
3、然后我们编写导入的PHP代码。
4、然后我们编写导出的PHP代码。
5、然后我们进行导出测试发现可以导出即可。
PHPExcel如何从excel表导入到mysql数据库
这是我写过的,绝对能用,不行你再找我!!
?php
if($_POST[sub]){
$uptypes=array('application/vnd.ms-excel','application/octet-stream');
$max_file_szie=20*pow(2,20); //上传的文件小于20MB
$destination_folder='../conn/'; //上传文件保存路径
if($_SERVER['REQUEST_METHOD']=='POST'){
if(!is_uploaded_file($_FILES['upfile']['tmp_name'])){
exit("script alert('文件不存在!');history.back();/script");
}
if($max_file_szie$_FILES['upfile']['size']){
exit("script alert('文件太大了!');history.back();/script");
}
if(!in_array($_FILES['upfile']['type'],$uptypes)){
echo '文件类型不符合!'.$_FILES['upfile']['type'];
exit("script alert('文件类型不符合!');history.back();/script");
}
if(!file_exists($destination_folder)){
mkdir($destination_folder);
}
$filename=$_FILES['upfile']['tmp_name'];
$image_size=getimagesize($filename);
$pinfo=pathinfo($_FILES['upfile']['name']); //文件路径信息
$ftype=$pinfo['extension']; //旧文件后缀名
$destination = $destination_folder.$_FILES['upfile']['name']; //新文件名称
if(file_exists($destination)$voerwrie !=true){
exit("script alert('同名文件已经存在了!');history.back();/script");
}
//把上传的文件从临时文件夹移动到指定目录
if(!move_uploaded_file($filename,$destination)){
exit("script alert('移动文件出错了!');history.back();/script");
}
$pinfo=pathinfo($destination);
$fname=$pinfo[basename];
$tpfile=$destination;//上传文件名
//-----------上传成功,导入数据star-----
$dataf=$tpfile;
if(!file_exists($dataf))
{
exit("文件不存在"); //文件不存在
}
$file = fopen("$dataf",'r');
while ($d = fgetcsv($file)) { //每次读取CSV里面的一行内容
//print_r($d); //此为一个数组,要获得每一个数据,访问数组下标即可
$type="`uid`='$d[0]' name='$d[1]'";
$dsql=dbst($tableqz.message2,$type);
if(!$dsql){
$uid=trim($d[0]); //编号
$name=trim($d[1]); //客户名称
$type="(`uid`, `username`, `password`) VALUES (NULL, '$uid', '$name');";
dbin(hh_members,$type);
}
}
fclose($file);
unlink("$dataf");
}
//---上传end
exit("script alert('成功导入了所有数据!');history.back();/script");
}
?
如何用php实现将excel导入到数据库中
把excel
改为
cvs文件
?
//连接数据库文件
$connect=mysql_connect("localhost","admin","admin")
or
die("链接数据库失败!");
//连接数据库(test)
mysql_select_db("testcg",$connect)
or
die
(mysql_error());
$temp=file("test.csv");//连接EXCEL文件,格式为了.csv
for
($i=0;$i
count($temp);$i++)
{
$string=explode(",",$temp[$i]);//通过循环得到EXCEL文件中每行记录的值
//将EXCEL文件中每行记录的值插入到数据库中
$q="insert
into
ceshi
(name,num,dom)
values('$string[0]','$string[1]','$string[2]');";
mysql_query($q)
or
die
(mysql_error());
if
(!mysql_error());
{
echo
"
成功导入数据!";
}
echo
$string[4]."/n";
unset($string);
}
?
thinkphp怎么把excel导入数据库
thinkPHP实现将excel导入到数据库中的方法体如下:
PHPExcel插件可点击此处本站下载。
这里使用的是thinkphp框架的3.1版本,下载好压缩包,框架中的extend中的vendor文件夹中新建一个名为PHPExcel的文件夹,把classes里面的内容放到里面
下面是前端页面
提示:我在测试的时候遇到报错exception 'PHPExcel_Reader_Exception' with message 'The filename
原因是由于excel的文件后缀可能不同,我的文件后缀是xlsx,然后给把他另存为了xls的文件,就可以了
html
head
/head
body
form action="{pigcms::U('Jdb/abcdefgwulisuibian')}" method="post" enctype="multipart/form-data"
input type="file" name="import"/
input type="hidden" name="table" value="tablename"/
input type="submit" value="导入"/
/form
/body
/html
下面是php的
function abcdefgwulisuibianuplod(){
$this-display();//显示页面
}
function abcdefgwulisuibian(){
if (!empty($_FILES)) {
import("@.ORG.UploadFile");
$config=array(
'allowExts'=array('xlsx','xls'),
'savePath'='./Public/upload/',
'saveRule'='time',
);
$upload = new UploadFile($config);
if (!$upload-upload()) {
$this-error($upload-getErrorMsg());
} else {
$info = $upload-getUploadFileInfo();
}
vendor("PHPExcel.PHPExcel");
$file_name=$info[0]['savepath'].$info[0]['savename'];
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader-load($file_name,$encode='utf-8');
$sheet = $objPHPExcel-getSheet(0);
$highestRow = $sheet-getHighestRow(); // 取得总行数
$highestColumn = $sheet-getHighestColumn(); // 取得总列数
for($i=2;$i=$highestRow;$i++)//这个地方根据需要,一般第一行是名称,所以从第二行开始循环,也可以从第一行开始
{
$data['lianjieid'] = $objPHPExcel-getActiveSheet()-getCell("A".$i)-getValue();//数据库字段和excel列相对应
$data['yaoqingma'] = $objPHPExcel-getActiveSheet()-getCell("B".$i)-getValue();
$data['dlmima']= $objPHPExcel-getActiveSheet()-getCell("C".$i)-getValue();
$data['ljdizhi']= $objPHPExcel-getActiveSheet()-getCell("D".$i)-getValue();
M('jdb')-add($data);//插入数据库
}
$this-success('导入成功!');
}else
{
$this-error("请选择上传的文件");
}
}