在调试设置时,确保引入了正确的文件并实例化。
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->setWidth(80);
$drawing->setHeight(80);
$drawing->setCoordinates('D'.$k);
$drawing->setOffsetX(12);
$drawing->setOffsetY(12);
$drawing->setWorksheet($spreadsheet->getActiveSheet());
将A列宽度设置为30(字符)。
$spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(30);
$spreadsheet->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
$spreadsheet->getActiveSheet()->getDefaultColumnDimension()->setWidth(12);
$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等文件处理接口。
更多使用设置请参照官网文档:https://phpspreadsheet.readthedocs.io/en/stable/。
require 'vendor/autoload.php';
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('tmp.xlsx');
$sheet = $spreadsheet->getActiveSheet();
$data = $sheet->toArray();
var_dump($data);
require 'vendor/autoload.php';
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getDefaultColumnDimension()->setWidth(40);
$sheet->getColumnDimensionByColumn(2)->setWidth(100);
$sheet->setCellValue('A2', 'Hello World !');
for ($i = 1; $i < 10; $i++) {
$sheet->setCellValueByColumnAndRow($i, 1, 'Col'.$i);
}
$fileName = '01simple.xlsx';
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'. $fileName .'"');
header('Cache-Control: max-age=0');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save('php://output');
exit;