PHP使用PHPEXCEL导入Excel
2020-12-13 02:27
标签:style blog class code java color 下载PHPEXCEL 后放到项目里。 以下是前台代码 用ajax不知道怎么做。 PHP使用PHPEXCEL导入Excel,搜素材,soscw.com PHP使用PHPEXCEL导入Excel 标签:style blog class code java color 原文地址:http://www.cnblogs.com/aroundight/p/3718450.html
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‘
);
}
}