1

I'm using PHPExcel to read from Excel files in CodeIgniter. Having issues with integrating the following example provided by PHPExcel in their documentations

Here's the code from the example:

<?php

/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');

/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';


$inputFileType = 'Excel5';
//  $inputFileType = 'Excel2007';
//  $inputFileType = 'Excel2003XML';
//  $inputFileType = 'OOCalc';
//  $inputFileType = 'Gnumeric';
$inputFileName = './sampleData/example1.xls';
$sheetname = 'Data Sheet #3';


class MyReadFilter implements PHPExcel_Reader_IReadFilter
{
    public function readCell($column, $row, $worksheetName = '') {
        // Read rows 1 to 7 and columns A to E only
        if ($row >= 1 && $row <= 7) {
            if (in_array($column,range('A','E'))) {
                return true;
            }
        }
        return false;
    }
}

$filterSubset = new MyReadFilter();

echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
echo 'Loading Sheet "',$sheetname,'" only<br />';
$objReader->setLoadSheetsOnly($sheetname);
echo 'Loading Sheet using filter<br />';
$objReader->setReadFilter($filterSubset);
$objPHPExcel = $objReader->load($inputFileName);

echo '<hr />';

$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData);

?>

So, integrating the example with CodeIgniter I did the following:

  1. Created a function 'importFromExcel()' which is called from a button click in the View.

    public function importFromExcel() 
    {
    $this->load->library('excel');
    
    $this->load->library('Excel/MyReadFilter');
    
    $inputFileType = 'Excel5';
    //  $inputFileType = 'Excel2007';
    $inputFileName = FCPATH.'uploads/khanas.xlsx';
    $sheetname = 'Sheet1';
    
    $filterSubset = new MyReadFilter();
    
    echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    echo 'Loading Sheet "',$sheetname,'" only<br />';
    $objReader->setLoadSheetsOnly($sheetname);
    echo 'Loading Sheet using filter<br />';
    $objReader->setReadFilter($filterSubset);
    $objPHPExcel = $objReader->load($inputFileName);
    
    
    echo '<hr />';
    
    $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
    var_dump($sheetData);
    }
    
  2. Created a library in 'application/libraries' and named it 'MyReadFilter.php'

    class MyReadFilter implements PHPExcel_Reader_IReadFilter
    {
    public function readCell($column, $row, $worksheetName = '') {
        $startRow   = 1;
        $endRow     = 7;
        $startCol   = 'A';
        $endCol     = 'E';
    
        // Read rows 1 to 7 and columns A to E only
        if ($row >= $startRow && $row <= $endRow) {
            if (in_array($column,range($startCol,$endCol))) {
                return true;
            }
        }
        return false;
    }
    }
    

When I call the class as below doesn't work.

$this->load->library('Excel/MyReadFilter');

How do I go about fixing this?

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.