您的位置:首页 > 运维架构 > Linux

CI 框架 调用PHPExcel linux 无效问题

2016-12-16 09:41 507 查看
最近商品部需要导入大量的数据,问我能不能弄个导入excel 的。我看了看,CI框架可以使用PHPExcel 类库进行导入。

$this->load->library("PHPExcel");//ci框架中引入excel类
$PHPReader = null;
if ($extension == 'xls') {
$PHPReader = new PHPExcel_Reader_Excel5();
if (!$PHPReader->canRead($uploadfile)) {
die('{"status":-1,"msg":"Excel 不规范。无法解析!"}');
return;
}
} elseif ($extension == 'xlsx') {

$PHPReader = new PHPExcel_Reader_Excel2007();
if (!$PHPReader->canRead($uploadfile)) {
die('{"status":-1,"msg":"Excel 不规范。无法解析!"}');
return;

}
}
if (!isset($PHPReader)) {
die('{"status":-1,"msg":"不是一个excel,无法解析!"}');
};
// 加载excel文件
$PHPExcel = $PHPReader->load($uploadfile);

// 读取excel文件中的第一个工作表
$currentSheet = $PHPExcel->getSheet(0);
// 取得最大的列号
$allColumn = $currentSheet->getHighestColumn();
// 取得一共有多少行
$allRow = $currentSheet->getHighestRow();


在windows 下 phpstudy 集成php 环境里面没有任何问题。

但是部署到正式环境:centos linux 下面,死活不行,提示无法加载phpexcel 类。

找了很多文章,都没有得解决。

如:

1.修改 PHPExcel.php 的文件名。没有效果

2.改用单个文件引用

include (APPPATH.'librarys/PHPExcel/PHPExcel.php');
include (APPPATH.'librarys/PHPExcel/PHPExcel/IOFactory.php');
include (APPPATH.'librarys/PHPExcel/PHPExcel/Reader/Excel2007.php');
include (APPPATH.'librarys/PHPExcel/PHPExcel/Reader/Excel5.php');
文件是加载上来了,但是$PHPReader = new PHPExcel_Reader_Excel2007(); 无法进行实例化。

3.修改内核:\system\core\Loader.php 里面的$filepath = $path.'libraries/'.$subdir.$class.'.php'; 改为:$filepath = $path.'libraries/'.ucfirst($subdir).$class.'.php';(该方法也是网找的)

以上方法都不行。

最后,还是看回
http://stackoverflow.com/questions/15028175/phpexcel-error-in-codeigniter-unable-to-load-the-requested-class-iofactory
这篇文章,在michail1982的回复里面 

//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());
}


$this->load->file(APPPATH.'libraries/PHPExcel.php');//full
path to

就是这句话。通过load file 终于把类库加载进来了。无论是windows还是linux 都OK了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php excel