1

我正在尝试将 PHPExcel 与 CodeIgniter 一起使用。

我的问题是当我想在下面使用这个方法时,我得到了PHP Fatal Error : Cannot redeclare class IOFactory

如果不确定文件类型,可以使用 IO 工厂的 identify() 方法来识别您需要的读取器,然后再使用 createReader() 方法实例化读取器对象。

下面是我的代码:

$this->load->library('PHPExcel');

$this->load->library('PHPExcel/IOFactory');

$path = $upload_data['full_path'];

$inputFileType = PHPExcel_IOFactory::identify($path);

$objReader = PHPExcel_IOFactory::createReader($inputFileType);

$objPHPExcel = $objReader->load($path);

$objWorksheet = $objPHPExcel->getActiveSheet();

我尝试查找 IOFactory 类是否已在某处创建但找不到。

仅供参考,我在撰写本文时使用的是最新版本的 CodeIgniter (2.1) 和 PHPExcel (1.7.6)。

4

3 回答 3

1

不需要写

$this->load->library('PHPExcel');

刚开始

$this->load->library('PHPExcel/IOFactory');

已编辑 :::

在我的项目中,我通过这些方式完成了,

第一步:在libraries\PHPExcel\Reader

重命名Excel5.phpPHPExcel_Reader_Excel5.php

第2步:

$filePath = $dir.$uploadedfile;
$objReader = $this->load->library('PHPExcel/Reader/PHPExcel_Reader_Excel5', $filePath);
$objReader = new PHPExcel_Reader_Excel5();
$objPHPExcel = $objReader->load($filePath);     
$rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();                        
$sheet = $objPHPExcel->getActiveSheet();
$maxRowIndex = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$maxColIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
于 2012-05-18T06:11:01.083 回答
1

全部

我正在使用 PHPExcel 通过 CI 和grocery_CRUD 从 MySQL 中的 Excel 上传数据。这是我的方式:

// 我的 Excel CI 库

<?php

if (!defined('BASEPATH'))     exit('No direct script access allowed');
require_once APPPATH . "/third_party/PHPExcel/IOFactory.php";
/*
 * PHPExcel Lib For CI
 *
 * Class Excel
 *
 * Using PHP Excel Class
 */
class Lib_excel extends PHPExcel_IOFactory {
    public function __construct() {
    }
}
?>

//我的 Excel CI 模型

<?php
// My CI Model for handling all Excel methods
if (!defined('BASEPATH'))     exit('No direct script access allowed');
// Class Model extend with CI Model
class Mod_excel extends CI_Model {
    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }
    /**
     * Read data from Excel file
     * 

     * @input string $excelFileName
     * @input string $columnName (starting column point)
     * @input string $startRow (starting column point)
     * @return array $cellValues
     *
     * @example code
     *  $excelFileName = $_FILES['uploadedfile']['tmp_name'];
     *  $columnName = 'B';
     *  $startRow = '2';
     *  $cellValues = readWithExcel($excelFileName, $columnName, $startRow);
     *  $iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($cellValues));
     *  foreach($iterator as $value) {
     *      $insert_data[] = $value;
     *  }
     */
    function readWithExcel($excelFileName, $columnName, $startRow) {
        //load our new Lib Excel library as object excel
        $this->load->library('Lib_excel', null, 'excel');
        // load excel file
        $objPHPExcel  = $this->excel->load($excelFileName);
        // get active sheets
        $objWorksheet = $objPHPExcel->getActiveSheet();
        // get highest row column
        $lastRow      = $objWorksheet->getHighestRow();
        // check cell value empty or not
        for ($i = $startRow; $i < $lastRow; $i++) {
            $colB = $objPHPExcel->getActiveSheet()->getCell($columnName . $i)->getValue();
            if ($colB == NULL || $colB == '') {
                die('The Cell' . ' <strong>B' . $i . '</strong> is empty. Please, remove or fill with data');
            }
        }
        // get excel data with range e.g. (B2:B56)
        $cellValues = $objPHPExcel->getActiveSheet()->rangeToArray("$columnName$startRow:$columnName$lastRow");
        // return all data as array
        return $cellValues;
    }
}
?>

// 我的控制器调用方法

<?php
// My Admin Controller with grocery CRUD createCouponCode method (URL: admin/createCouponCode)
if (!defined('BASEPATH'))     exit('No direct script access allowed');
class Admin extends CI_Controller {
    /**
     * Just construct method to load 
     */
    function __construct() {
        parent::__construct();
        // Load Model excel and use as object ModExcel
        $this->load->model('Mod_excel', 'ModExcel', true);
        // Load CRUD lib
        $this->load->library('grocery_CRUD');
    }
    /**
     * Upload Coupon Code from Excel
     */
    public function createCouponCode() {
        $crud = new grocery_CRUD();
        $crud->set_table('coupon_code');
        $crud->fields('coupon_no');
        $crud->columns('coupon_no');
        $crud->set_subject('Coupon');
        $crud->required_fields('coupon_no');
        $crud->set_field_upload('coupon_no', '../public/uploads/');
        $crud->callback_before_upload(array(
            $this,
            '_excel_uploaded_file'
        ));
        $output = $crud->render();
        $this->_example_output($output);
    }
    /**
     * Call back Function form CRUD Upload
     * @param array $files_to_upload
     * @param array $field_info
     * @return string
     */
    function _excel_uploaded_file($files_to_upload = array(), $field_info = array()) {
        $insert_data   = array();
        $ext           = '';
        $file_tmp_path = '';
        foreach ($files_to_upload as $value) {
            $ext           = pathinfo($value['name'], PATHINFO_EXTENSION);
            $file_tmp_path = $value['tmp_name'];
        }
        $allowed_formats = array(
            "xlsx"
        );
        if (in_array($ext, $allowed_formats)) {
            $excelFileName = $file_tmp_path;
            $columnName    = 'B';
            $startRow      = '2';
            $cellValues    = $this->ModExcel->readWithExcel($excelFileName, $columnName, $startRow);
            $iterator      = new RecursiveIteratorIterator(new RecursiveArrayIterator($cellValues));
            foreach ($iterator as $value) {
                $insert_data[] = array(
                    'coupon_no' => $value
                );
            }
            $this->output->enable_profiler(TRUE); //Turns on CI debugging
            $this->db->insert_batch('coupon_code', $insert_data);
            echo ($status) ? true : false;
        } else {
            return 'Error: Wrong file format. Use Excel 2007 format';
        }
    }
}
?>

// 我在 CI 中放置 PHPExcel Lib 的文件夹

在此处输入图像描述

于 2013-03-06T16:13:17.430 回答
0

你可以试试这个,它对我有用:

$filePath = "uploads/import/test.xlx";                                  
$this->load->library('PHPExcel');                         
$objReader = new PHPExcel_Reader_Excel5();                        
$objPHPExcel = $objReader->load($filePath);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
于 2012-09-08T06:58:47.453 回答