时间:2021-07-01 10:21:17 帮助过:3人阅读
原因是由于excel的文件后缀可能不同,我的文件后缀是xlsx,然后给把他另存为了xls的文件,就可以了
?1 2 3 4 5 6 7 8 9 10 11 |
<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的
?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 |
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( "请选择上传的文件" );
}
}
|
更多关于thinkPHP
thinkPHP实现将excel导入到数据库中的方法
标签: