简体   繁体   English

如何集成codeigniter和PHPExcel?

[英]How to integrate codeigniter and PHPExcel?

always display an error message on my browser: 总是在我的浏览器上显示错误消息:

An Error Was Encountered
Non-existent class: IOFactory

all classes PHPExcel, i extract on library. 所有类PHPExcel,我在库中提取。

Here it is my controller code 这是我的控制器代码

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Report extends CI_Controller 
{
     public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form','url'));
    }

    public function index()
    {   

        $this->load->library('phpexcel');
        $this->load->library('PHPExcel/IOFactory.php');
        $objPHPexcel = PHPExcel_IOFactory::load('tandaterima.xlsx');
        $objWorksheet = $objPHPexcel->getActiveSheet();
        //Daftar barang (4item)
        $objWorksheet->getCell('B16')->setValue('UTP');
        $objWorksheet->getCell('B17')->setValue('Cross');
        $objWorksheet->getCell('B18')->setValue('');
        $objWorksheet->getCell('B19')->setValue('');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPexcel, 'Excel5');
        $objWriter->save('write5.xls');
    }
}

please help me. 请帮我。

Follow the instruction here https://github.com/EllisLab/CodeIgniter/wiki/PHPExcel 按照此处的说明https://github.com/EllisLab/CodeIgniter/wiki/PHPExcel

Please remember to remove the PHPExcel_ part in the class name in IOFactory.php. 请记住要删除IOFactory.php中类名称中的PHPExcel_部分。 And change the construct function from private to public 并将构造功能从私人变为公共

确保您将PHPExcel / IOFactory.php保存在库文件夹中,并将其加载为$ this-> load-> library('PHPExcel / iofactory');

在某些Linux Server中,您必须关心大小写。

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

You can replace this line 您可以替换此行

$objWriter = PHPExcel_IOFactory::createWriter($objPHPexcel, 'Excel5');

by this line 通过这条线

IOFactory::createWriter($objPHPexcel, 'Excel5');

First you need to place your PHPExcel folder inside thirdparty folder. 首先,您需要将PHPExcel文件夹放在thirdparty文件夹中。 Then create class file in the library folder. 然后在库文件夹中创建类文件。 There you need to include thirdparty/PHPExcel file folder and extend the class. 在那里,您需要包括thirdparty/PHPExcel文件夹并扩展类。 After that you can use it in your controller. 之后,您可以在控制器中使用它。

Codeiginiter 3 supports Composer to use PHPExcel: Codeiginiter 3支持Composer使用PHPExcel:

In application/config/config.php , set $config['composer_autoload'] to TRUE . application/config/config.php ,将$config['composer_autoload']TRUE

Then you can use Composer to install PHPExcel in Codeiginiter : 然后,您可以使用Composer在Codeiginiter中安装PHPExcel:

composer require phpoffice/phpexcel

Further, You could try PHPExcel Helper to easier handle PHPExcel: 此外,您可以尝试使用PHPExcel Helper来更轻松地处理PHPExcel:

composer require yidas/phpexcel-helper

https://github.com/yidas/phpexcel-helper https://github.com/yidas/phpexcel-helper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM