php读写Excel/Pdf

版权声明:本文为博主原创文章,欢迎转载,转载时请以超链接形式标明文章原始出处。 https://blog.csdn.net/lilongsy/article/details/84375057

PHPExcel1已经废弃,现在用PhpSpreadsheet2官方文档

PhpSpreadsheet是一个用纯PHP写的类库,可以读写操作不同的电子表格文件格式,像Excel和LibreOffice。

支持的文档格式:

Format Reading Writing
Open Document Format/OASIS (.ods)
Office Open XML (.xlsx) Excel 2007 and above
BIFF 8 (.xls) Excel 97 and above
BIFF 5 (.xls) Excel 95
SpreadsheetML (.xml) Excel 2003
Gnumeric
HTML
SYLK
CSV
PDF (using either the TCPDF, Dompdf or mPDF libraries, which need to be installed separately)

示例,创建第一个xlsx文档

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

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

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

读取Excel

use PhpOffice\PhpSpreadsheet\IOFactory;
// $inputFileName 也可以是表单上传的$_FILES['file']['tmp_name']
$inputFileName = __DIR__ . '/sampleData/example1.xls';
$spreadsheet = IOFactory::load($inputFileName);
$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
var_dump($sheetData);

更多读取Excel见Reader


  1. https://github.com/PHPOffice/PHPExcel ↩︎

  2. https://github.com/PHPOffice/PhpSpreadsheet ↩︎

猜你喜欢

转载自blog.csdn.net/lilongsy/article/details/84375057