Vendor('PHPExcel.PHPExcel');
public
function
excel_runimport(){
$PHPExcel
=
new
\PHPExcel();
if
(!
empty
(
$_FILES
['file'] ['name'] )){
$file_types
=
explode
(
"."
,
$_FILES
['file'] ['name'] );
$exts
=
$file_types
[
count
(
$file_types
) - 1];
if
(
strtolower
(
$exts
) !=
"xlsx"
||
strtolower
(
$exts
) !=
"xls"
){
$this
->error ( '不是Excel文件,重新上传');
}
$config
=
array
('maxSize'=>70000000,
'exts'=>
array
('xlsx','xls'),
'rootPath'=>'./Uploads/excel/',
'saveName' =>
$filename
,
'subName' =>
array
('
date
','Ymd'),
);
$upload
=
new
\Think\Upload(
$config
);
$info
=
$upload
->upload();
if
(
$info
){
if
(
$exts
== 'xls'){
import(
"Vendor.PHPExcel.Reader.Excel5"
);
$PHPReader
=
new
\PHPExcel_Reader_Excel5();
}
else
if
(
$exts
== 'xlsx'){
import(
"Vendor.PHPExcel.Reader.Excel2007"
);
$PHPReader
=
new
\PHPExcel_Reader_Excel2007();
}
$rootPath
='./Uploads/excel/';
$savePath
=
$info
['file']['savepath'];
$saveName
=
$info
['file']['savename'];
$filePath
=
$rootPath
.
$savePath
.
$saveName
;
$objReader
=
$PHPReader
->load(
$filePath
);
$currentSheet
=
$objReader
->getSheet(0);
$allColumn
=
$currentSheet
->getHighestColumn();
$allRow
=
$currentSheet
->getHighestRow();
$data
=
array
();
for
(
$rowIndex
=2;
$rowIndex
<=
$allRow
;
$rowIndex
++){
for
(
$colIndex
='A';
$colIndex
<=
$allColumn
;
$colIndex
++){
$addr
=
$colIndex
.
$rowIndex
;
$cell
=
$currentSheet
->getCell(
$addr
)->getValue();
if
(
$cell
instanceof
PHPExcel_RichText){
$cell
=
$cell
->__toString();
}
$data
[
$rowIndex
][
$colIndex
] =
$cell
;
}
}
if
(
is_file
(
$filename
)) unlink(
$filename
);
}
else
{
echo
$upload
->getError();
}
}
}