phpspreadsheet介绍
1、简介
phpspreadsheet 是一个用纯php编写的库,提供了一组类,使您可以读取和写入不同的电子表格文件格式
phpspreadsheet 提供了丰富的api接口,可以设置诸多单元格以及文档属性,包括样式、图片、日期、函数等等诸多应用,总之你想要什么样的excel表格,phpspreadsheet都能做到。
使用 phpspreadsheet 开发的php要求 7.1或更高版本
phpspreadsheet 支持链式操作
2、安装
使用 composer 安装,在项目根目录下执行下面命令,即可安装。本次是在thinkphp5框架里使用。
composer require phpoffice/phpspreadsheet
使用
1、引用和实例化
use phpoffice\phpspreadsheet\spreadsheet; // 实例化 spreadsheet 对象 $spreadsheet = new spreadsheet();
2、获取工作簿
getactivesheet
// 获取活动工作薄 $sheet = $spreadsheet->getactivesheet();
单元格操作
1、获取单元格
两种获取单元格方式
- getcell
- getcellbycolumnandrow 数字单元格坐标获取单元格
$cell = $sheet->getcell('a1');
$cell = $sheet->getcellbycolumnandrow(1,1); // 参数:列、行
2、设置单元格的值
- setvalue
- setcellvalue
- setcellvaluebycolumnandrow
// 获取单元格
$cella = $sheet->getcell('a1');
// 设置单元格值
$cella->setvalue('姓名'); // 设置a1单元格的值为姓名
// 给a1设置值,参数:单元格位置,值
$sheet->setcellvalue('a1','id');
//给a2赋值 hello
$sheet->setcellvaluebycolumnandrow(1, 2, 'hello');
3、单元格文字样式
- getstyle 获取单元格样式
- getfont 获取单元格文字样式
- setbold 设置文字粗细
- setname 设置文字字体
- setsize 设置文字大小
//将a1至d1单元格设置成粗体
$sheet->getstyle('a1:d1')->getfont()->setbold(true);
//将a1单元格设置成粗体,黑体,10号字
$sheet->getstyle('a1')->getfont()->setbold(true)->setname('黑体')->setsize(10);
4、单元格文字颜色
- getcolor() 获取坐标颜色
- setrgb() 设置字体颜色(颜色值带#)
- getrgb() 获取字体颜色
- setargb() 设置字体颜色(颜色值不带#)
- getargb() 获取字体颜色
// b3单元格设置颜色
$sheet->getstyle('b3')->getfont()->getcolor()->setrgb('#aeeeee');
$sheet->getstyle('b3')->getfont()->getcolor()->setargb('ffff0000');
5、单元格内文字换行
- setwraptext 设置文本里的\n符合为:换行
//使用 \n 进行单元格内换行,相当于 alt+enter
$sheet->getcell('a1')->setvalue("hello\nworld");
$sheet->getstyle('a1')->getalignment()->setwraptext(true);
6、单元格列和行
- getcolumndimension 获取一列
- getwidth 获取一列的宽度
- setwidth 设置一列的宽度
- setautosize 设置一列的宽度自动调整
- getdefaultcolumndimension 获取一列的默认值
//设置默认列宽20
$sheet->getdefaultcolumndimension()->setwidth(20);
//将a列宽度设置成20
$sheet->getcolumndimension('a')->setwidth(20);
//自动计算列宽
$sheet->getcolumndimension('a')->setautosize(true);
- getrowdimension 获取一行
- getrowheight 获取一行的高度
- setrowheight 设置一行的高度
//设置默认行高
$sheet->getdefaultrowdimension()->setrowheight(20);
//设置第一行行高为20pt
$sheet->getrowdimension('1')->setrowheight(20);
- gethighestcolumn 获取总列数
- gethighestrow 获取总行数
echo $sheet->gethighestcolumn(); echo $sheet->gethighestrow();
7、单元格样式
- applyfromarray 设置单元格样式
- 对齐
use phpoffice\phpspreadsheet\style\alignment;
// 居中对齐
$stylearray = [
'alignment' => [
'horizontal' => alignment::horizontal_center, //水平居中
'vertical' => alignment::vertical_center, //垂直居中
],
];
$worksheet->getstyle('a1')->applyfromarray($stylearray);
- 边框
上 下 左 右 全部 边框都可设置
//红色边框
use phpoffice\phpspreadsheet\style\border;
// 外边框
// border 类中的常量都是边框样式
// border::border_thick 边框样式
$stylearray = [
'borders' => [
'outline' => [
'borderstyle' => border::border_thick,
'color' => ['argb' => 'ffff0000'],
],
],
];
$worksheet->getstyle('b2:g8')->applyfromarray($stylearray);
8、单元格合并和拆分
- mergecells 合并
- unmergecells 拆分
// 合并a1到c1列
$sheet->mergecells('a1:c4');
// 合并a1到a4行
$sheet->mergecells('a1:a4');
// 合并后,赋值只能给a1,开始的坐标。
$sheet->getcell('a1')->setvalue('西安');
// 拆分
$sheet->mergecells('a1:c4');
$sheet->unmergecells('a1:a4');
9、超链接
- gethyperlink 获取单元格链接
- seturl 设置单元格链接
$spreadsheet->getactivesheet()->setcellvalue('e6', 'xxxx的博客');
$spreadsheet->getactivesheet()->getcell('e6')->gethyperlink()->seturl('https://blog.csdn.net/u011167662');
10、使用函数
sum求和a;verage平均数;min最小值;max最大值
$sheet->setcellvalue('a3', '=sum(a1:a2)');
$sheet->setcellvalue('a3', '=max(a1:a2)');
11、批量赋值
- fromarray 从数组中的值填充工作表
参数1:数据(数组)
参数2:去除某个值
参数3:从哪个位置开始
$sheet->fromarray(
[
[1,'欧阳克','18岁','188cm'],
[2,'黄蓉','17岁','165cm'],
[3,'郭靖','21岁','180cm']
], 3, 'a2' );
12、写入图片
use phpoffice\phpspreadsheet\worksheet\drawing;
//写入图片
$drawing = new drawing();
$drawing->setname('logo')->setdescription('logo')->setpath('../files/1.jpg')->setheight(30)->setcoordinates('d6')->setoffsetx(50)->setoffsety(6);
$drawing->setrotation(25);
$drawing->getshadow()->setvisible(true);
$drawing->getshadow()->setdirection(45);
$drawing->setworksheet($sheet);
工作簿操作
1、xlsx 文件导出
- iofactory::createwriter 写入到文件
use phpoffice\phpspreadsheet\iofactory;
// mime 协议,文件的类型,不设置,会默认html
header('content-type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
// mime 协议的扩展
$filename = date('y-m-d').'工作报告单';
header('content-disposition:attachment;filename='.$filename .'.xlsx');
// 缓存控制
header('cache-control:max-age=0');
$write = iofactory::createwriter($spreadsheet, 'xlsx');
$write->save('php://output');
2、xls 文件导出
// mime 协议,文件的类型,不设置,会默认html
header('content-type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
// mime 协议的扩展
$filename = date('y-m-d').'工作报告单';
header('content-disposition:attachment;filename='.$filename .'.xls');
// 缓存控制
header('cache-control:max-age=0');
$write = iofactory::createwriter($spreadsheet, 'xls');
$write->save('php://output');
3、设置工作簿标题
- settitle
$sheet->settitle('标题1');
实战
1、导出简单数据(使用 thinkphp5 框架)
<?php
namespace app\api\controller;
use phpoffice\phpspreadsheet\spreadsheet;
use phpoffice\phpspreadsheet\style\alignment;
use phpoffice\phpspreadsheet\style\border;
use phpoffice\phpspreadsheet\iofactory;
class excel
{
public function exporteasy()
{
$spreadsheet = new spreadsheet();
$worksheet = $spreadsheet->getactivesheet();
$title = 'excel导出';
$worksheet->settitle($title);
$worksheet->mergecells('b2:d2');
$worksheet->getrowdimension(1)->setrowheight(50);
$worksheet->getstyle('b2:d2')->applyfromarray([
'alignment' => [
'horizontal' => alignment::horizontal_center,
'vertical' => alignment::vertical_center,
],
'borders' => [
'outline' => [
'borderstyle' => border::border_thin,
'color' => ['argb' => '000000']
],
],
'font' => [
'name' => '黑体',
'bold' => true,
'size' => 22
]
]);
$worksheet->setcellvaluebycolumnandrow(2, 2, $title);
$worksheet->setcellvaluebycolumnandrow(2, 3, '姓名');
$worksheet->setcellvaluebycolumnandrow(3, 3, '性别');
$worksheet->setcellvaluebycolumnandrow(4, 3, '年龄');
$worksheet->setcellvaluebycolumnandrow(2, 4, '张三');
$worksheet->setcellvaluebycolumnandrow(3, 4, '男');
$worksheet->setcellvaluebycolumnandrow(4, 4, '20');
header('content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('content-disposition: attachment;filename=excel导出.xlsx');
header('cache-control: max-age=0');
$write = iofactory::createwriter($spreadsheet, 'xlsx');
$write->save('php://output');
}
}
导出表格截图:

2、导出数据
liu_system_log 管理员操作记录表
public function export()
{
$spreadsheet = new spreadsheet();
$sheet = $spreadsheet->getactivesheet();
$stylearray = [
'alignment' => [
'horizontal' => alignment::horizontal_center,
'vertical' => alignment::vertical_center,
],
'borders' => [
'outline' => [
'borderstyle' => border::border_thick,
],
],
];
$sheet->getdefaultcolumndimension()->setwidth(20);// 列宽
$sheet->getdefaultrowdimension()->setrowheight(20);// 行高
// 标题
$tabletitle = '管理员操作记录表';
$sheet->mergecells('a1:d1');
$sheet->getrowdimension('1')->setrowheight(40);// 行高
$sheet->getstyle('a1')->applyfromarray($stylearray);
$sheet->getstyle('a1')->getfont()->setbold(true)->setsize(16);
$sheet->setcellvalue('a1', $tabletitle);
$sheet->getstyle('a2:d2')->applyfromarray($stylearray);
$sheet->getstyle('a2:d2')->getfont()->setbold(true)->setsize(12);
$sheet->setcellvalue('a2','管理员姓名');
$sheet->setcellvalue('b2','操作');
$sheet->setcellvalue('c2','ip');
$sheet->setcellvalue('d2','操作时间');
$data = db::name('system_log')->select();
$sort = 0;
foreach ($data as $v){
$sheet->setcellvalue('a' . ($sort + 3), $v['admin_name']);
$sheet->setcellvalue('b' . ($sort + 3), $v['page']);
$sheet->setcellvalue('c' . ($sort + 3), $v['ip']);
$sheet->setcellvalue('d' . ($sort + 3), date('y-m-d h:i:s', $v['add_time']));
$sort++;
}
// 工作簿标题
$sheettitle = '管理员操作记录表';
$sheet->settitle($sheettitle);
header('content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
// 文件名
$filename = '管理员操作记录表';
header('content-disposition: attachment;filename=' . $filename .'.xlsx');
header('cache-control: max-age=0');
$write = iofactory::createwriter($spreadsheet, 'xlsx');
$write->save('php://output');
}
以上就是php使用phpspreadsheet导出excel表格的实例详解的详细内容,更多关于php phpspreadsheet导出excel的资料请关注代码网其它相关文章!
发表评论