Fatal error: Class 'PHPExcel_IOFactory' not found in phpExel
Fatal error: Class 'PHPExcel_IOFactory' not found in phpExel
我使用了下面的代码,所有 PHPexcel 库数据都在下图中。
<?php
$inputFileName = './NIB.xlsx';
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
?>
我想阅读 NIB.xlsx 文件并且我已经阅读了这个 help link。
但是,当我 运行 我的 PHP 脚本时,我收到以下错误:
Fatal error: Class 'PHPExcel_IOFactory' not found in phpExcel in untitled-2.php
我想你忘了包含 PHPExcel 文件
在您的代码开头添加此行
require 'PHPExcel/Classes/PHPExcel.php';
不要忘记添加文件夹的路径,以防它在不同的文件夹中
我在尝试官方文档中的一些简单示例时也遇到了这个错误。我正在使用随 Composer 安装的 PHPOffice。如果在本地安装,概念应该相同。错误基本上是路径问题,即脚本不知道在哪里可以找到 IOFactory。这是我找到的完整的工作解决方案。
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$styleArray = [
'font' => [
'bold' => true,
],
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,
],
'borders' => [
'top' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
],
],
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'color' => [
'argb' => 'FFA0A0A0',
]
],
];
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$spreadsheet->getActiveSheet()->getStyle('A3:m3')->applyFromArray($styleArray);
// Redirect output to a client's web browser (Xlsx)
header('Content-Type: application/vnd.openxmlformats- officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="webstats.xlsx"');
header('Cache-Control: max-age=0');
$writer = PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
exit;
我将在线示例中的这一行从
更改为
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
至此
$writer = PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
IO 错误已解决
我使用了下面的代码,所有 PHPexcel 库数据都在下图中。
<?php
$inputFileName = './NIB.xlsx';
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
?>
我想阅读 NIB.xlsx 文件并且我已经阅读了这个 help link。
但是,当我 运行 我的 PHP 脚本时,我收到以下错误:
Fatal error: Class 'PHPExcel_IOFactory' not found in phpExcel in untitled-2.php
我想你忘了包含 PHPExcel 文件 在您的代码开头添加此行
require 'PHPExcel/Classes/PHPExcel.php';
不要忘记添加文件夹的路径,以防它在不同的文件夹中
我在尝试官方文档中的一些简单示例时也遇到了这个错误。我正在使用随 Composer 安装的 PHPOffice。如果在本地安装,概念应该相同。错误基本上是路径问题,即脚本不知道在哪里可以找到 IOFactory。这是我找到的完整的工作解决方案。
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$styleArray = [
'font' => [
'bold' => true,
],
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_RIGHT,
],
'borders' => [
'top' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
],
],
'fill' => [
'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
'color' => [
'argb' => 'FFA0A0A0',
]
],
];
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$spreadsheet->getActiveSheet()->getStyle('A3:m3')->applyFromArray($styleArray);
// Redirect output to a client's web browser (Xlsx)
header('Content-Type: application/vnd.openxmlformats- officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="webstats.xlsx"');
header('Cache-Control: max-age=0');
$writer = PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
exit;
我将在线示例中的这一行从
更改为$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
至此
$writer = PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
IO 错误已解决