下载PHPEXCEL 后放到项目里。
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
public function InExcel() {
//设定缓存模式为经gzip压缩后存入cache(PHPExcel导入导出及大量数据导入缓存方式的修改 )
$cacheMethod
= PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
$cacheSettings
= array ();
PHPExcel_Settings::setCacheStorageMethod( $cacheMethod , $cacheSettings );
$objPHPExcel
= new PHPExcel();
//读入上传文件
if
( $_POST ) {
$objPHPExcel
= PHPExcel_IOFactory::load( $_FILES [ "inputExcel" ][ "tmp_name" ]);
//内容转换为数组
$indata
= $objPHPExcel ->getSheet(0)->toArray();
//print_r($indata);
//excel sheet个数
//echo $objPHPExcel->getSheetCount();
//把数据新增到mysql数据库中
$arr
= array ();
$j
= 0;
$model
= D( ‘goods_unit‘ );
//根据具体情况修改
if
( count ( $indata ) == 1) {
//根据具体情况修改
$arr [0] = array ( "hang"
=> 0, "Cause"
=> "无记录!" );
$this ->assign( ‘errorss‘ , $arr );
$list
= $this ->menu();
$this ->assign( ‘menu‘ , $list );
$this ->display( ‘Base:baseunit‘ , ‘utf-8‘ );
} else
{
if
( $indata [0][0] == "单位名称" ) {
for
( $i = 1; $i < count ( $indata ); $i ++) {
$all [ ‘UnitName‘ ] = $indata [ $i ][0];
$w
= $model ->where( $all )->select();
if
( $w !== null) {
$arr [ $j ] = array ( "hang"
=> $i + 1, "Cause"
=> "该单位已存在" );
$j ++;
continue ;
}
$result
= $model ->add( $all );
if
(false == $result ) {
$arr [ $j ] = array ( "hang"
=> $i + 1, "Cause"
=> "写入单位表时失败" );
$j ++;
continue ;
}
}
if
( $arr == null) {
$arr [0] = array ( "hang"
=> 0, "Cause"
=> "导入Excel成功" );
}
$this ->assign( ‘errorss‘ , $arr );
$list
= $this ->menu();
$this ->assign( ‘menu‘ , $list );
$this ->display( ‘Base:baseunit‘ , ‘utf-8‘ );
} else
{
$arr [0] = array ( "hang"
=> 0, "Cause"
=> "上传文件格式不正确!" );
$this ->assign( ‘errorss‘ , $arr );
$list
= $this ->menu();
$this ->assign( ‘menu‘ , $list );
$this ->display( ‘Base:baseunit‘ , ‘utf-8‘ );
}
}
} else
{
$arr [0] = array ( "hang"
=> 0, "Cause"
=> "服务器未检测到有上传文件!" );
$this ->assign( ‘errorss‘ , $arr );
$list
= $this ->menu();
$this ->assign( ‘menu‘ , $list );
$this ->display( ‘Base:baseunit‘ , ‘utf-8‘ );
}
} |
以下是前台代码
<script> $("#ProjectToolbar").on(‘click‘, "a[name=‘haveExcel‘]", function(data) { $w= $(‘#mk‘).html().trim(); if($w=="选择上传文件"){ $("#inputFile").click(); }else{ if($(‘#inputFile‘).val()!=null){ $("#submitexcel").click(); }else{ alert("还未选择文件"); $(‘#mk‘).html("选择上传文件"); } } }); function dohtml() { $w= $(‘#mk‘).html().trim(); if($w=="选择上传文件"){ $(‘#mk‘).html("上传所选文件"); }else{ $(‘#mk‘).html("选择上传文件"); } } </script> <div id="ProjectToolbar"> <form action="__URL__/InExcel" id="formExcel" method="post" enctype="multipart/form-data"> <input type="file" name="inputExcel" id="inputFile" style="display:none;" onchange="dohtml()"> <a name="haveExcel" id="mk" href="javascript:;">选择上传文件</a> <input type="submit" name="submite" id="submitexcel" value="提交" style="display:none;" /> </form> </div>
用ajax不知道怎么做。