php CodeIgniter 中的 PHPExcel 错误“无法加载请求的类:iofactory”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15028175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHPExcel error in CodeIgniter "Unable to load the requested class: iofactory"
提问by sheldon90
I'm trying to export an XLS file with PHPExcel 1.7.8 + CodeIgniter 2.1.3
我正在尝试使用 PHPExcel 1.7.8 + CodeIgniter 2.1.3 导出 XLS 文件
I have followed all the instructions from PHPExcel
我已按照PHPExcel 的所有说明进行操作
but I'm getting this error:
但我收到此错误:
Unable to load the requested class:
iofactory
无法加载请求的类:
iofactory
and here's my Controller code:
这是我的控制器代码:
//expoxt to excel all admin data
function export_excel_admin()
{
//$data['resultsadmin'] = $this->admin_model->get_all_data_admin();
//var_dump($data['resultsadmin']);
//$this->load->view('administrator/export_excel/export_excel_admin', $data);
$query = $this->db->get('tbl_admin');
if(!$query)
return false;
// Starting the PHPExcel library
$this->load->library('excel');
$this->load->library('PHPexcel/IOFactory');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("export")->setDescription("none");
$objPHPExcel->setActiveSheetIndex(0);
// Field names in the first row
$fields = $query->list_fields();
$col = 0;
foreach ($fields as $field)
{
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
$col++;
}
// Fetching the table data
$row = 2;
foreach($query->result() as $data)
{
$col = 0;
foreach ($fields as $field)
{
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);
$col++;
}
$row++;
}
$objPHPExcel->setActiveSheetIndex(0);
$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
// Sending headers to force the user to download the file
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Products_'.date('dMy').'.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
}
Ah also I have removed the "PHPExcel_" part in IOFactory.php file any solution for this issue?
啊,我也删除了 IOFactory.php 文件中的“PHPExcel_”部分,这个问题有什么解决方案吗?
回答by devrooms
I've used PHPExcel with CodeIgniter successfully before.
我以前成功地将 PHPExcel 与 CodeIgniter 一起使用。
All I did was drop the phpexcel folder into application/third-party and created the following wrapper library:
我所做的只是将 phpexcel 文件夹放入 application/thirdparty 并创建以下包装库:
<?php
class Excel {
private $excel;
public function __construct() {
// initialise the reference to the codeigniter instance
require_once APPPATH.'third_party/phpexcel/PHPExcel.php';
$this->excel = new PHPExcel();
}
public function load($path) {
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$this->excel = $objReader->load($path);
}
public function save($path) {
// Write out as the new file
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
$objWriter->save($path);
}
public function stream($filename) {
header('Content-type: application/ms-excel');
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Cache-control: private");
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
$objWriter->save('php://output');
}
public function __call($name, $arguments) {
// make sure our child object has this method
if(method_exists($this->excel, $name)) {
// forward the call to our child object
return call_user_func_array(array($this->excel, $name), $arguments);
}
return null;
}
}
?>
I could then do the following in my controllers:
然后我可以在我的控制器中执行以下操作:
$this->load->library("excel");
$this->excel->load("/path/to/input.xls");
$this->excel->setActiveSheetIndex(0);
$this->excel->getActiveSheet()->SetCellValue('B2', "whatever");
$this->excel->save("/path/to/output.xls");
Hope this helps you out?
希望这可以帮助你?
回答by Dindon fiqri aji
Maybe this comment was too late. But maybe useful for others.
也许这个评论太晚了。但也许对其他人有用。
Change the part code below:
更改下面的零件代码:
// Starting the PHPExcel library
$this->load->library('excel');
$this->load->library('PHPexcel/IOFactory');
to:
到:
// Starting the PHPExcel library
$this->load->library('excel');
//$this->load->library('PHPexcel/IOFactory');
Then change this part code too:
然后也更改此部分代码:
$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
to:
到:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
回答by michail1982
all you need is
所有你需要的是
//afrer this you can use any of PHPExcel classes and methods
$this->load->file(APPPATH.'libraries/PHPExcel.php'); //full path to
$objReader = new PHPExcel_Reader_Excel2007();//change by filetype
try {
$objPHPExcel = $objReader->load($inputFileName); //you file name
} catch (Exception $e) {
show_error($e->getMessage());
}
tested on CI 2.1.3 && PHPExcel 1.7.8
在 CI 2.1.3 && PHPExcel 1.7.8 上测试
回答by iMezied
I follow these instructions for the integration between CodeIgniter and PHPExcel.
我按照这些说明进行 CodeIgniter 和 PHPExcel 之间的集成。
Create a new PHP file inside CI's application/libraries/and name it Excel.php.
在 CI 中创建一个新的 PHP 文件application/libraries/并将其命名为 Excel.php。
require_once APPPATH."/third_party/PHPExcel.php";
class Excel extends PHPExcel {
public function __construct(){
parent::__construct();
}
}
To follow the full instructions visit Easily Integrateload Phpexcel Into Codeigniter Framework.
要遵循完整说明,请访问轻松将 Phpexcel 集成到 Codeigniter 框架中。

