PHPExcel 集成到 Zend 框架中

2021-12-29 00:00:00 php phpexcel zend-framework

如何将 PHPExcel 集成到我的 Zend 应用程序中.

how can I integrate the PHPExcel into my Zend app.

我的实际文件夹结构如下:

My actual folder structure is the following:

/application
  controllers
  views  
  etc...
/library
  My
  Zend
  PHPExcel
/public
  index.php

我已经通过使用(在 index.php 中)包含了我的"库:

I already include 'My' libs by using (in index.php):

require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('My_');

现在我还想在我的一个控制器中使用 PHPExcel,例如:

Now I also want to use PHPExcel inside one of my controllers like:

$exc = PHPExcel_IOFactory::load('test.xls');
$excelWorksheet = $exc->getActiveSheet();

我该怎么做才能使它工作并摆脱 Class 'PHPExcel_IOFactory' not found 异常?

What do I have to do to make it work and get rid of the Class 'PHPExcel_IOFactory' not found Exception?

谢谢.
-lony

Thank you.
-lony

PS:一个简单的 $autoloader->registerNamespace('PHPExcel_'); 不起作用.我测试过了.

P.S.: A simple $autoloader->registerNamespace('PHPExcel_'); is not working. I tested it.

推荐答案

将PHPExcel库放入/library文件夹,如下:

Place the PHPExcel library into the /library folder, like this:

/application
...
/library
    /PHPExcel
    /PHPExcel.php

接下来,在您的 application.ini 配置文件中,添加以下内容:

Next, in your application.ini config file, add the following:

autoloaderNamespaces[] = "PHPExcel_"
autoloaderNamespaces[] = "PHPExcel"

应该可以.Autoloader 负责其余的工作,您只需开始使用示例代码读取 Excel 文件即可.

That should do it. Autoloader takes care of the rest, and you can just start using the example code to read an Excel file.

更新:添加了评论者建议的额外 autoloaderNamespace

Update: Added the extra autoloaderNamespace as suggested by commenters

相关文章