composer require phpoffice/phpspreadsheet
$spreadsheet = new Spreadsheet();$sheet = $spreadsheet->getActiveSheet(); // 获取当前激活的工作表
$spreadsheet->getDefaultStyle()->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
$spreadsheet->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$spreadsheet->getDefaultStyle()->getAlignment()->setWrapText(true);
$spreadsheet->getProperties() ->setCreator("mrcdh") // 作者 ->setLastModifiedBy("mrcdh") // 最后修改者 ->setTitle("Office 2007 XLSX Test Document") // 标题 ->setSubject("Office 2007 XLSX Test Document") // 副标题 ->setDescription("这是一一个用php构建的Excel文档。") // 描述 ->setKeywords("office php excel") // 关键字 ->setCategory("Test result file"); // 分类
try{ $spreadsheet->createSheet(1); // 传入索引,0代表第一个工作表}catch(Exception $e){}
try{ $spreadsheet->getSheet(1); // 获取索引为1的工作表}catch(Exception $e){}
$sheet->setTitle('Demo');
try{ $spreadsheet->removeSheetByIndex(0); // 删除第一个工作表}catch(Exception $e){}
$sheet->getStyle('A1')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); // A1单元格垂直居中$sheet->getStyle('A1:E3')->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); // A1到E3单元格垂直居中
$sheet->getStyle('A1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); // A1单元格水平居中
$sheet->getDefaultRowDimension()->setRowHeight(20); // 默认行高$sheet->getRowDimension(4)->setRowHeight(30); // 设置第4行的行高
$sheet->getDefaultColumnDimension()->setWidth(20); // 默认单元格宽度$sheet->getColumnDimension('A')->setWidth(30); // 设置A列的宽度为30
$sheet->setCellValue('A3', '商品编码');$sheet->getCell('B3')->setValue('商品名称');
// 设置A2到K2的背景色,也可以单独指定单元格$sheet->getStyle('A2:K2') ->getFill() ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID) // 设置填充样式 ->getStartColor() ->setARGB('FFD8E4BC'); // 可以忽略透明,直接使用RGB
// BORDER_THIN 细边框// BORDER_THICK 粗边框$sheet->getStyle('A2:K2')->getBorders()->getAllBorders()->setBorderStyle(Border::BORDER_THIN); // 所有边框$sheet->getStyle('B2')->getBorders()->getTop()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK); // 上边框$sheet->getStyle('B2')->getBorders()->getBottom()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK);// 下边框$sheet->getStyle('B2')->getBorders()->getLeft()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK); // 左边框$sheet->getStyle('B2')->getBorders()->getRight()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK); // 右边框
使用SUM计算B5到C5之间单元格的总和。其他函数同理:最大数(MAX),最小数(MIN),平均值(AVERAGE)。
SUM
B5
C5
MAX
MIN
AVERAGE
$sheet->setCellValue('A3', '=SUM(B5:C5)');
$sheet->getCell('A4')->setValue("hello\nworld"); // 内容\n换行$sheet->getStyle('A4')->getAlignment()->setWrapText(true); // 指定单元格自动换行
$sheet->getStyle('A1:B7')->getFont() ->setBold(true) // 加粗 ->setName('Arial') // 字体 ->setSize(10); // 10号字 // 设置文字颜色为红色$sheet->getStyle('A4')->getFont()->getColor() ->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);
$sheet->mergeCells('A18:E22'); // 将A18到E22合并为一个单元格。
$sheet->unmergeCells('A18:E22'); // 将合并后的单元格拆分
$sheet->freezePane('A2');
A2
B1
B2
$sheet->getComment('E11')->setAuthor('mrcdh'); // 设置批注作者$commentRichText = $sheet->getComment('E11')->getText()->createTextRun('mrcdh:'); // 设置批注内容$commentRichText->getFont()->setBold(true); // 给上个批注内容加粗$sheet->getComment('E11')->getText()->createTextRun("\r\n"); // 换行$sheet->getComment('E11')->getText()->createTextRun('注释内容'); // 追加内容
$cellVal= $workSheet->getCell('A1')->getValue();$toTimestamp = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($cellVal);$date = date("Y-m-d", $toTimestamp);// 获取时可能出现时间比表格时间快了8个小时// 这是因为PHPExcel从文件中获取的时间,是以格林威治时间标准(GMT/UTC)格式// 这时需要使用`gmdate`函数格式化格林威治时间,`date`格式化本地时间$date = date("Y-m-d", $toTimestamp);
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;$writer = new Xlsx($spreadsheet);$writer->save('demo.xlsx');
use PhpOffice\PhpSpreadsheet\Reader\Xlsx; $reader = new Xlsx(); $spreadsheet = $read->load($url); // 加载xlsx文件 $sheet = $spreadsheet->getActiveSheet(); $rows = $sheet->getHighestRow(); // 总行数 $cols = $sheet->getHighestColumn(); // 总列数: B $colsIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($cols); // 总列数: 2,将字母转换为索引值 $sheet->getCellByColumnAndRow(1, 2)->getValue(); // 获取第2行第1列单元格内容