PhpSpreadSheet 读写excel文件

phpoffice 系列很好用。PhpSpreadSheet取代了原先的phpexcel,用来处理excel文件

https://phpspreadsheet.readthedocs.io/en/latest/

简单的使用示例:

1、基本操作

注意:新建和修改是不同的类来处理的

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\IReader;
use PhpOffice\PhpSpreadsheet\Writer\IWriter;

新建

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');

$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');

修改

$spreadsheet = IOFactory::load($old_file);
$worksheet = $spreadsheet->getActiveSheet();
// 修改
$worksheet->getCell('A1')->setValue('John');
$worksheet->getCell('A2')->setValue('Smith');

$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($new_file);   

 

上一篇:使用PhpSpreadsheet对Excel文件进行操作


下一篇:在excel中格式化mysql数据