Anjali Mirani
Anjali Mirani

Reputation: 65

Fatal error: Class 'PHPExcel_IOFactory' not found in phpExel

I used the code below and all PHPexcel library data are in the image below.

<?php
$inputFileName = './NIB.xlsx';
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);

$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
?>

Image

I want to read NIB.xlsx file and I have read this help link.

However, when I run my PHP script, I received the following error:

Fatal error: Class 'PHPExcel_IOFactory' not found in phpExcel in untitled-2.php

Upvotes: 5

Views: 38598

Answers (2)

Mike Volmar
Mike Volmar

Reputation: 2093

I received this error as well, while trying some simple examples from the official documentation. I am using PHPOffice installed with Composer. The concept should be the same if installed locally. Error is basically a path issue, i.e. the script not knowing where IOFactory can be found. Here is the complete working solution I found.

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;

I changed this line in online example from

$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');

to this

$writer = PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');

and the IO error was resolved

Upvotes: 2

affaz
affaz

Reputation: 1191

I think you forgot to include the PHPExcel file Add this line at the beginning of your code

require 'PHPExcel/Classes/PHPExcel.php';

Dont forget to add the path of your folder,incase its in a different folder

Upvotes: 5

Related Questions