本文会介绍php使用PhpSpreadsheet操作Excel,供大家参考,具体内容如下:
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、获取单元格
两种获取单元格方式
$cell = $sheet->getCell('A1'); $cell = $sheet->getCellByColumnAndRow(1,1); // 参数:列、行
2、设置单元格的值
// 获取单元格 $cellA = $sheet->getCell('A1'); // 设置单元格值 $cellA->setValue('姓名'); // 设置A1单元格的值为姓名 // 给A1设置值,参数:单元格位置,值 $sheet->setCellValue('A1','ID'); //给A2赋值 hello $sheet->setCellValueByColumnAndRow(1, 2, 'hello');
3、单元格文字样式
//将A1至D1单元格设置成粗体 $sheet->getStyle('A1:D1')->getFont()->setBold(true); //将A1单元格设置成粗体,黑体,10号字 $sheet->getStyle('A1')->getFont()->setBold(true)->setName('黑体')->setSize(10);
4、单元格文字颜色
// B3单元格设置颜色 $sheet->getStyle('B3')->getFont()->getColor()->setRGB('#AEEEEE'); $sheet->getStyle('B3')->getFont()->getColor()->setARGB('FFFF0000');
5、单元格内文字换行
//使用 \n 进行单元格内换行,相当于 Alt+Enter $sheet->getCell('A1')->setValue("hello\nworld"); $sheet->getStyle('A1')->getAlignment()->setWrapText(true);
6、单元格列和行
//设置默认列宽20 $sheet->getDefaultColumnDimension()->setWidth(20); //将A列宽度设置成20 $sheet->getColumnDimension('A')->setWidth(20); //自动计算列宽 $sheet->getColumnDimension('A')->setAutoSize(true);
//设置默认行高 $sheet->getDefaultRowDimension()->setRowHeight(20); //设置第一行行高为20pt $sheet->getRowDimension('1')->setRowHeight(20);
echo $sheet->getHighestColumn(); echo $sheet->getHighestRow();
7、单元格样式
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、单元格合并和拆分
// 合并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、超链接
$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、批量赋值
参数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 文件导出
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、设置工作簿标题
$sheet->setTitle('标题1');
1、导出简单数据(使用 ThinkPHP5 框架)
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'); }