PhpSpreadsheet是一个纯PHP编写的组件库,它使用现代PHP写法,代码质量和性能比PHPExcel高不少,完全可以替代PHPExcel(PHPExcel已不再维护)。使用PhpSpreadsheet可以轻松读取和写入Excel文档,支持Excel的所有操作。
要使用PhpSpreadsheet需要满足以下条件:
现在开始,创建项目目录/PHPExcel,进入项目目录。
使用composer安装:
composer require phpoffice/phpspreadsheet
复制
在项目目录下新建/public目录,在public目录下创建示例文件test.php,编辑test.php,用以下代码。
<?php
require '../vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Welcome to Helloweba.');
$writer = new Xlsx($spreadsheet);
$writer->save('hello.xlsx');
复制
运行代码,你会发现在目录下生成一个hello.xlsx文件,打开Excel文件,你会看到Excel中的单元格A1中有“Welcome to Helloweba.”内容。当然你可以对单元格样式诸如颜色、背景、宽度、字体等等进行设置,这些会在接下来的几节中讲到。
思路:使用PhpSpreadsheet读取Excel表格中的有用信息,然后组装成sql语句,最后批量插入到MySQL表中。
require 'vendor/autoload.php';
include('conn.php'); //连接数据库
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load('students.xlsx'); //载入excel表格
$worksheet = $spreadsheet->getActiveSheet();
$highestRow = $worksheet->getHighestRow(); // 总行数
$highestColumn = $worksheet->getHighestColumn(); // 总列数
$highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn); // e.g. 5
$lines = $highestRow - 2;
if ($lines <= 0) {
exit('Excel表格中没有数据');
}
$sql = "INSERT INTO `t_student` (`name`, `chinese`, `maths`, `english`) VALUES ";
for ($row = 3; $row <= $highestRow; ++$row) {
$name = $worksheet->getCellByColumnAndRow(1, $row)->getValue(); //姓名
$chinese = $worksheet->getCellByColumnAndRow(2, $row)->getValue(); //语文
$maths = $worksheet->getCellByColumnAndRow(3, $row)->getValue(); //数学
$english = $worksheet->getCellByColumnAndRow(4, $row)->getValue(); //外语
$sql .= "('$name','$chinese','$maths','$english'),";
}
$sql = rtrim($sql, ","); //去掉最后一个,号
try {
$db->query($sql);
echo 'OK';
} catch (Exception $e) {
echo $e->getMessage();
}
复制
首先我们引入自动加载PhpSpreadsheet库,然后实例化,设置工作表标题名称为:学生成绩表,接着设置表头内容。表头分为两行,第一行是表格的名称,第二行数表格列名称。最后我们将第一行单元格进行合并,并设置表头内容样式:字体、对齐方式等。
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
include('conn.php'); //连接数据库
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
//设置工作表标题名称
$worksheet->setTitle('学生成绩表');
//表头
//设置单元格内容
$worksheet->setCellValueByColumnAndRow(1, 1, '学生成绩表');
$worksheet->setCellValueByColumnAndRow(1, 2, '姓名');
$worksheet->setCellValueByColumnAndRow(2, 2, '语文');
$worksheet->setCellValueByColumnAndRow(3, 2, '数学');
$worksheet->setCellValueByColumnAndRow(4, 2, '外语');
$worksheet->setCellValueByColumnAndRow(5, 2, '总分');
//合并单元格
$worksheet->mergeCells('A1:E1');
$styleArray = [
'font' => [
'bold' => true
],
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
];
//设置单元格样式
$worksheet->getStyle('A1')->applyFromArray($styleArray)->getFont()->setSize(28);
$worksheet->getStyle('A2:E2')->applyFromArray($styleArray)->getFont()->setSize(14);
复制
我们连接数据库后,直接读取学生成绩表t_student,然后for循环,设置每个单元格对应的内容,计算总成绩。注意的是表格中的数据是从第3行开始,因为第1,2行是表头占用了。
然后,我们设置整个表格样式,给表格加上边框,并且居中对齐。
$sql = "SELECT id,name,chinese,maths,english FROM `t_student`";
$stmt = $db->query($sql);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$len = count($rows);
$j = 0;
for ($i=0; $i < $len; $i++) {
$j = $i + 3; //从表格第3行开始
$worksheet->setCellValueByColumnAndRow(1, $j, $rows[$i]['name']);
$worksheet->setCellValueByColumnAndRow(2, $j, $rows[$i]['chinese']);
$worksheet->setCellValueByColumnAndRow(3, $j, $rows[$i]['maths']);
$worksheet->setCellValueByColumnAndRow(4, $j, $rows[$i]['english']);
$worksheet->setCellValueByColumnAndRow(5, $j, $rows[$i]['chinese'] + $rows[$i]['maths'] + $rows[$i]['english']);
}
$styleArrayBody = [
'borders' => [
'allBorders' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
'color' => ['argb' => '666666'],
],
],
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
];
$total_rows = $len + 2;
//添加所有边框/居中
$worksheet->getStyle('A1:E'.$total_rows)->applyFromArray($styleArrayBody);
复制
强制浏览器下载数据并保存为Excel文件
$filename = '成绩表.xlsx';
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
复制
强制浏览器下载数据并保存为Excel文件
$filename = '成绩表.xlsx';
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
复制
如想要保存为.xls文件格式的话,可以改下header代码:
$filename = '成绩表.xlsx';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'xls');
$writer->save('php://output');
复制
PhpSpreadsheet提供了丰富的API接口,可以设置诸多单元格以及文档属性,包括样式、图片、日期、函数等等诸多应用,总之你想要什么样的Excel表格,PhpSpreadsheet都能做到。
use PhpOffice\PhpSpreadsheet\Spreadsheet;
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
复制
第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('A')->setWidth(30);
复制
如果需要自动计算列宽,可以这样:
$spreadsheet->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
复制
设置默认列宽为12:
$spreadsheet->getActiveSheet()->getDefaultColumnDimension()->setWidth(12);
复制
设置第10行行高为100pt:
$spreadsheet->getActiveSheet()->getRowDimension('10')->setRowHeight(100);
复制
设置默认行高:
$spreadsheet->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15);
复制
将A1单元格设置为水平居中对齐:
$styleArray = [
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
];
$worksheet->getStyle('A1')->applyFromArray($styleArray);
复制
将A18到E22合并为一个单元格:
$spreadsheet->getActiveSheet()->mergeCells('A18:E22');
复制
将合并后的单元格拆分:
$spreadsheet->getActiveSheet()->unmergeCells('A18:E22');
复制
将B2至G8的区域添加红色边框:
$styleArray = [
'borders' => [
'outline' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
'color' => ['argb' => 'FFFF0000'],
],
],
];
$worksheet->getStyle('B2:G8')->applyFromArray($styleArray);
复制
设置当前工作表标题:
$spreadsheet->getActiveSheet()->setTitle('Hello');
复制
设置日期格式:
$spreadsheet->getActiveSheet()
->setCellValue('D1', '2018-06-15');
$spreadsheet->getActiveSheet()->getStyle('D1')
->getNumberFormat()
->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_YYYYMMDD2);
复制
使用\n进行单元格内换行,相当于(ALT+"Enter"):
$spreadsheet->getActiveSheet()->getCell('A4')->setValue("hello\nworld");
$spreadsheet->getActiveSheet()->getStyle('A4')->getAlignment()->setWrapText(true)
复制
将单元格设置为超链接形式:
$spreadsheet->getActiveSheet()->setCellValue('E6', 'www.helloweba.net');
$spreadsheet->getActiveSheet()->getCell('E6')->getHyperlink()->setUrl('https://www.helloweba.net');
复制
使用SUM计算B5到C5之间单元格的总和。其他函数同理:最大数(MAX),最小数(MIN),平均值(AVERAGE):
$spreadsheet->getActiveSheet()
->setCellValue('B7', '=SUM(B5:C5)');
复制
可以设置Excel文档属性:
$spreadsheet->getProperties()
->setCreator("Helloweba") //作者
->setLastModifiedBy("Yuegg") //最后修改者
->setTitle("Office 2007 XLSX Test Document") //标题
->setSubject("Office 2007 XLSX Test Document") //副标题
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.") //描述
->setKeywords("office 2007 openxml php") //关键字
->setCategory("Test result file"); //分类
复制
此外,除了提供丰富的Excel文件处理接口外,PhpSpreadshee还提供了CSV,PDF,HTML以及XML等文件处理接口。