前端代码:
用的是链接按钮
导出Excel
后台代码:
首先引入类
use PHPExcel_IOFactory;
use PHPExcel;
//导出excel
public function export(){
PHPExcel=newPHPExcel();//设置表格样式
P
H
P
E
x
c
e
l
=
n
e
w
P
H
P
E
x
c
e
l
(
)
;
/
/
设
置
表
格
样
式
PHPExcel->setActiveSheetIndex(0)
->setCellValue(‘A1’, ‘名称’)
->setCellValue(‘B1’, ‘状态’)
->setCellValue(‘C1’, ‘类型’);
data=db(‘semplan′)−>select();
d
a
t
a
=
d
b
(
‘
s
e
m
p
l
a
n
′
)
−
>
s
e
l
e
c
t
(
)
;
i=2; //定义一个i变量,目的是在循环输出数据是控制行数
count=count(
c
o
u
n
t
=
c
o
u
n
t
(
data);
//计算有多少条数据
for (
i=2;
i
=
2
;
i <=
count+1;
c
o
u
n
t
+
1
;
i++) {
PHPExcel−>getActiveSheet()−>setCellValue(′A′.
P
H
P
E
x
c
e
l
−
>
g
e
t
A
c
t
i
v
e
S
h
e
e
t
(
)
−
>
s
e
t
C
e
l
l
V
a
l
u
e
(
′
A
′
.
i,
data[
d
a
t
a
[
i-2][tgjhmc]);
PHPExcel−>getActiveSheet()−>setCellValue(′B′.
P
H
P
E
x
c
e
l
−
>
g
e
t
A
c
t
i
v
e
S
h
e
e
t
(
)
−
>
s
e
t
C
e
l
l
V
a
l
u
e
(
′
B
′
.
i,
data[
d
a
t
a
[
i-2][tgjhzt]);
PHPExcel−>getActiveSheet()−>setCellValue(′C′.
P
H
P
E
x
c
e
l
−
>
g
e
t
A
c
t
i
v
e
S
h
e
e
t
(
)
−
>
s
e
t
C
e
l
l
V
a
l
u
e
(
′
C
′
.
i,
data[
d
a
t
a
[
i-2][jhlx]);
}
PHPExcel−>getActiveSheet()−>setTitle(‘user′);//设置sheet的名称
P
H
P
E
x
c
e
l
−
>
g
e
t
A
c
t
i
v
e
S
h
e
e
t
(
)
−
>
s
e
t
T
i
t
l
e
(
‘
u
s
e
r
′
)
;
/
/
设
置
s
h
e
e
t
的
名
称
PHPExcel->setActiveSheetIndex(0);
//设置sheet的起始位置
objWriter=PHPExcelIOFactory::createWriter(
o
b
j
W
r
i
t
e
r
=
P
H
P
E
x
c
e
l
I
O
F
a
c
t
o
r
y
::
c
r
e
a
t
e
W
r
i
t
e
r
(
PHPExcel, ‘Excel5’);
//Excel2003通过PHPExcel_IOFactory的写函数将上面数据写出来
PHPWriter=PHPExcelIOFactory::createWriter(
P
H
P
W
r
i
t
e
r
=
P
H
P
E
x
c
e
l
I
O
F
a
c
t
o
r
y
::
c
r
e
a
t
e
W
r
i
t
e
r
(
PHPExcel,”Excel2007”);
//Excel2007
header(‘Content-Disposition: attachment;filename=”表名.xlsx”’);
header(‘Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet’);
$PHPWriter->save(“php://output”);
}