<?php
namespace
Admin\Controller;
use
Think\Controller;
class
ExcelController
extends
Controller {
public
function
excelList(){
$this
->display();
}
public
function
import(){
if
(!
empty
(
$_FILES
[
'file_stu'
][
'name'
])){
$tmp_file
=
$_FILES
[
'file_stu'
][
'tmp_name'
];
$file_types
=
explode
(
'.'
,
$_FILES
[
'file_stu'
][
'name'
]);
$file_type
=
$file_types
[
count
(
$file_types
) - 1];
if
(
$file_type
==
'xls'
||
$file_type
==
'xlsx'
||
$file_type
==
'csv'
){
$upload
=
new
\Think\Upload();
$upload
->maxSize = 3145728 ;
$upload
->exts =
array
(
'xls'
,
'csv'
,
'xlsx'
);
$upload
->rootPath =
'./Public'
;
$upload
->savePath =
'/Excel/'
;
$upload
->saveName =
date
(
'YmdHis'
);
$info
=
$upload
->upload();
if
(!
$info
) {
$this
->error(
$upload
->getError());
}
else
{
$filename
=
'./Public'
.
$info
[
'file_stu'
][
'savepath'
].
$info
[
'file_stu'
][
'savename'
];
import(
"Org.Yufan.ExcelReader"
);
vendor(
'PHPExcel.PHPExcel'
);
$reader
= \PHPExcel_IOFactory::createReader(
'Excel2007'
);
$PHPExcel
=
$reader
->load(
$filename
);
$sheet
=
$PHPExcel
->getSheet(0);
$highestRow
=
$sheet
->getHighestRow();
var_dump(
$highestRow
);
$highestColumm
=
$sheet
->getHighestColumn();
$data
=
array
();
for
(
$row
= 2;
$row
<=
$highestRow
;
$row
++){
if
(
$column
=
'A'
){
$data
[
'name'
] =
$sheet
->getCell(
$column
.
$row
)->getValue();
}
if
(
$column
=
'B'
){
$data
[
'account'
] =
$sheet
->getCell(
$column
.
$row
)->getValue();
}
if
(
$column
=
'C'
){
$data
[
'password'
] =
$sheet
->getCell(
$column
.
$row
)->getValue();
}
M(
'data'
)->add(
$data
);
}
$this
->success(
'导入数据库成功'
,U(
'Excel/show'
));
}
}
else
{
$this
->error(
'不是excel文件,请重新上传...'
);
}
}
else
{
$this
->error(
'(⊙o⊙)~没传数据就导入'
);
}
}
public
function
export(){
import(
"ORG.Yufan.Excel"
);
$list
= M(
'data'
)->select();
if
(
$list
== null){
$this
->error(
'数据库信息为空...'
,__APP__.
'/Admin/Excel/show'
);
}
else
{
$row
=
array
();
$row
[0]=
array
(
'平台名称'
,
'帐号'
,
'密码'
);
$i
=1;
foreach
(
$list
as
$v
){
$row
[
$i
][
'name'
] =
$v
[
'name'
];
$row
[
$i
][
'account'
] =
$v
[
'account'
];
$row
[
$i
][
'password'
] =
$v
[
'password'
];
$i
++;
}
$xls
=
new
\Excel_XML(
'UTF-8'
, false,
'datalist'
);
$xls
->addArray(
$row
);
$xls
->generateXML(
date
(
'YmdHis'
));
}
}
public
function
show(){
$m
= M(
'data'
);
$data
=
$m
->select();
$this
->assign(
'data'
,
$data
);
$this
->display();
}
}