Lectura de Excel usando PHPExcel en CodeIgniter

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

Aquí está el código del ejemplo:

<?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');

¿Cómo puedo solucionar esto?

preguntado el 12 de febrero de 14 a las 08:02

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.