Excel在工作中有至关重要的地位,这边简单介绍一下Excel!
maatwebsite/excel
首先分享几个链接:
github地址: https://github.com/Maatwebsite/Laravel-Excel
官网地址: https://laravel-excel.com
1. 安装
1.1依赖:
PHP: ^7.0
Laravel: ^5.5
PhpSpreadsheet: ^1.6
PHP扩展已php_zip启用
PHP扩展已php_xml启用
PHP扩展已php_gd2启用
1.2 安装
composer require maatwebsite/excel
1.3 在laravel项目config/app.php中
//providers元素添加
Maatwebsite\Excel\ExcelServiceProvider::class,
//aliases元素添加
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
1.4 发布配置
php artisan vendor:publish --provider=
"Maatwebsite\Excel\ExcelServiceProvider"
1.5 新建导出类
php artisan make:export TestExport
//执行后会在新建app/Export/TestExport.php文件
1.6 新建导入类
php artisan make:import TestImport
//执行后会在新建app/Export/TestExport.php文件
phpoffice/phpspreadsheet
首先分享几个链接:
官网地址: https://phpspreadsheet.readthedocs.io/en/stable/
gitHub地址: https://github.com/PHPOffice/PhpSpreadsheet
1. 安装依赖
PHP5.6或更改版本,推荐PHP7
支持php_zip扩展
支持php_xml扩展
支持php_gd2扩展
2. 安装
composer require phpoffice/phpspreadsheet
3. 特性
支持读取.xls,.xlsx,.html,.csv等格式文件,支持写入导出.xls,.xlsx,.html,.csv,.pdf格式文件。
提供丰富的API,提供单元格样式设置、Excel表格属性设置、图表设置等等诸多功能。使用PhpSpreadsheet完全可以生成一个外观结构都满足你的Excel表格文件。
卓越的性能,尤其在PHP7上表现优异,比PHPExcel强大很多。
4. 使用
字体: 第1行代码将A7至B7两单元格设置为粗体字,Arial字体,10号字;第2行代码将B1单元格设置为粗体字
$spreadsheet->getActiveSheet()->getStyle('A7:B7')->getFont()->setBold(true)->setName('Arial')
->setSize(10);;
$spreadsheet->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
颜色 :将文字颜色设置为红色
$spreadsheet->getActiveSheet()->getStyle('A4')
->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
图片: 可以将图片加载到Excel中
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setName('Logo');
$drawing->setDescription('Logo');
$drawing->setPath('./images/officelogo.jpg');
$drawing->setHeight(36);
列宽: 将A列宽度设置为30(字符)
$spreadsheet->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
行高: 设置第10行行高为100pt
$spreadsheet->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15);
日期时间: 设置日期格式
$spreadsheet->getActiveSheet()
->setCellValue('D1', '2018-06-15');
$spreadsheet->getActiveSheet()->getStyle('D1')
->getNumberFormat()
->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_YYYYMMDD2);
超链接: 将单元格设置为超链接形式
$spreadsheet->getActiveSheet()->setCellValue('E6', 'www.helloweba.net');
$spreadsheet->getActiveSheet()->getCell('E6')->getHyperlink()->setUrl('https://www.baidu.com');
使用函数: 使用SUM计算B5到C5之间单元格的总和。其他函数同理:最大数(MAX),最小数(MIN),平均值(AVERAGE)
$spreadsheet->getActiveSheet()
->setCellValue('B7', '=SUM(B5:C5)');
对齐: 将A1单元格设置为水平居中对齐
$styleArray = [
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
];
$worksheet->getStyle('A1')->applyFromArray($styleArray);
边框: 将B2至G8的区域添加红色边框
$styleArray = [
'borders' => [
'outline' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
'color' => ['argb' => 'FFFF0000'],
],
],
];
$worksheet->getStyle('B2:G8')->applyFromArray($styleArray);
此外,除了提供丰富的Excel文件处理接口外,PhpSpreadshee还提供了CSV,PDF,HTML以及XML等文件处理接口。