摘要:PhpSpreadsheet是PHPExcel的升级版本,也就是说PHPExcel有的功能,PhpSpreadsheet也都可以实现,而且PHPExcel已经不再维护了,所以在以后要操作excel将要使用PhpSpreadsheet。
PhpSpreadsheet is the next version of PHPExcel. It breaks compatibility to dramatically improve the code base quality (namespaces, PSR compliance, use of latest PHP language features, etc.).
Because all efforts have shifted to PhpSpreadsheet, PHPExcel will no longer be maintained. All contributions for PHPExcel, patches and new features, should target PhpSpreadsheet master branch.
下面就通过实例来演示 PhpSpreadsheet 如何设置单元格。
1、在项目中引入PhpSpreadsheet
在PhpSpreadsheet中使用的是命名空间,最好可以通过composer引入:composer require phpoffice/phpspreadsheet
PHP version 7.1 or newer to develop using PhpSpreadsheet. Other requirements, such as PHP extensions, are enforced by composer.
官方文档也说了,需要用composer引入,不过值得一提的是,PhpSpreadsheet的使用要求php的版本在7.1以上。
PhpSpreadsheet的核心文件在src文件夹中,我们在使用它操作excel时,用的也都是这文件里面的类。
2、实例演示<?php
//如果你的项目中不能自动引用,通过composer安装后在vendor文件夹下有个autoload.php文件
//在项目中引入autoload.php 文件就可以通过使用命名空间引入 PhpSpreadsheet 相关的功能类
require_once 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Color; //设置字体颜色
use PhpOffice\PhpSpreadsheet\Style\Alignment; //设置对齐方式
use PhpOffice\PhpSpreadsheet\Styl