如何在cakephp2中使用phpplayer

kmynzznz  于 2023-11-19  发布在  PHP
关注(0)|答案(1)|浏览(185)

我尝试了很多方法在CakePHP 2.x中实现PHP工作表,在CakePHP 4中它为我工作。
最后我尝试了给定链接博客中提到的事情
http://abakalidis.blogspot.com/2018/08/how-to-use-latest-phpofficespreadsheet.html
它解决了所有的问题,但它生成Xlsx文件。对于XLS它的工作。
将PHPSpreadsheet SOrce TO Vender放入应用程序文件夹后。
使用以下代码

<?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);
$spreadsheet->createSheet();
$writer->save('hello world.xlsx');
?>

字符串
对于XLS I,替换如下3行

<?php

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xls;

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

$writer = new Xls($spreadsheet);
$spreadsheet->createSheet();
$writer->save('hello world.xls');
?>


这段代码工作,但对于Xlsx它是给错误

uklbhaso

uklbhaso1#

我找到解决办法了
Phpplayer sheet已安装composer:

composer require phpoffice/phpspreadsheet

字符串
composer.json中添加:

"require": {
     "google/apiclient": "^2.12.1",
     "phpoffice/phpspreadsheet": "^1.25"
 }


技巧是在控制器类的开头添加公共$components = array('GoogleAPI');。然后您只需用途:

\PhpOffice\PhpSpreadsheet\IOFactory


即,

$objPHPExcel = \PhpOffice\PhpSpreadsheet\IOFactory::load(YOUR_FILE);

相关问题