使用CodeIgniter中的PHPExcel从Excel读取

时间:2014-02-12 08:03:02

标签: codeigniter phpexcel

我正在使用PHPExcel读取CodeIgniter中的Excel文件。在将PHPExcel提供的以下示例集成到其文档中时遇到问题

以下是示例中的代码:

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

?>

因此,将示例与CodeIgniter集成我执行了以下操作:

  1. 创建了一个函数'importFromExcel()',该函数是从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. 在'application / libraries'中创建了一个库并将其命名为'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;
    }
    }
    
  3. 当我打电话给下面的班级时不起作用。

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

    我该如何解决这个问题?

0 个答案:

没有答案