php导出excel表 | 世外天堂

php导出excel表

安装类库
从GitHub上下载PHPExcel类库
地址:https://github.com/PHPOffice/PHPExcel

解压后将Classes文件夹移动到ThinkPHP的extend目录,并将其重命名为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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import('phpexcel.PHPExcel', EXTEND_PATH);
代码实现
<?php
namespace app\index\controller;

use think\Controller;

class Excel extends Controller
{
// 将数据导出至Excel
public function exportExcel($data,$table_name='sheet1',$index=0)
{
// 引入类库
import('phpexcel.PHPExcel', EXTEND_PATH);

// 文件名和文件类型
$fileName = "student";
$fileType = "xlsx";

$obj = new \PHPExcel();

// 以下内容是excel文件的信息描述信息
$obj->getProperties()->setCreator(''); //设置创建者
$obj->getProperties()->setLastModifiedBy(''); //设置修改者
$obj->getProperties()->setTitle(''); //设置标题
$obj->getProperties()->setSubject(''); //设置主题
$obj->getProperties()->setDescription(''); //设置描述
$obj->getProperties()->setKeywords('');//设置关键词
$obj->getProperties()->setCategory('');//设置类型
//创建表
if($index!==0){
$obj->createSheet();
}
// 设置当前sheet
$obj->setActiveSheetIndex($index);

// 设置当前sheet的名称
$obj->getActiveSheet()->setTitle($table_name);

// 列标
$list = ['A', 'B', 'C'];

// 填充第一行数据
$obj->getActiveSheet()
->setCellValue($list[0] . '1', '学号')
->setCellValue($list[1] . '1', '姓名')
->setCellValue($list[2] . '1', '班级');

// 填充第n(n>=2, n∈N*)行数据
$length = count($data);
for ($i = 0; $i < $length; $i++) {
$obj->getActiveSheet()->setCellValue($list[0] . ($i + 2), $data[$i]['stuNo'], \PHPExcel_Cell_DataType::TYPE_STRING);//将其设置为文本格式
$obj->getActiveSheet()->setCellValue($list[1] . ($i + 2), $data[$i]['name']);
$obj->getActiveSheet()->setCellValue($list[2] . ($i + 2), $data[$i]['class']);
}

// 设置加粗和左对齐
foreach ($list as $col) {
// 设置第一行加粗
$obj->getActiveSheet()->getStyle($col . '1')->getFont()->setBold(true);
// 设置第1-n行,居中
for ($i = 1; $i <= $length + 1; $i++) {
$obj->getActiveSheet()->getStyle($col . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
}
}

// 设置列宽
$obj->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$obj->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$obj->getActiveSheet()->getColumnDimension('C')->setWidth(15);
//设置单元格边框
$style_array = array(
'borders' => array(
'allborders' => array(
'style' => \PHPExcel_Style_Border::BORDER_THIN
)
));
$obj->getActiveSheet()->getStyle('A1:G'.(string)($length+1))->applyFromArray($style_array);
// 导出
ob_clean();
if ($fileType == 'xls') {
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $fileName . '.xls');
header('Cache-Control: max-age=1');
$objWriter = new \PHPExcel_Writer_Excel5($obj);
$objWriter->save('php://output');
exit;
} elseif ($fileType == 'xlsx') {
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx');
header('Cache-Control: max-age=1');
$objWriter = \PHPExcel_IOFactory::createWriter($obj, 'Excel2007');
$objWriter->save('php://output');
exit;
}
}


// 准备数据
$studentList = [
[
'stuNo' => '20190101',
'name' => 'student01',
'class' => '1班'
], [
'stuNo' => '20190102',
'name' => 'student02',
'class' => '1班'
], [
'stuNo' => '20190103',
'name' => 'student03',
'class' => '1班'
]
];
// 导出表格
exportExcel($data);

}