Newby.. generating a simple excel file.

Topics: Developer Forum, User Forum
Jun 22, 2009 at 12:52 AM

Hello everyone.

Just wondering if anyone can give me a hand on this.  I downloaded the 1.6.7 release.  Just want to generate a simple excel file with data coming from a database.  Just columns and rows.  Got the sample files and simple copied and pasted and change what was necesary to accomodate it to what I need.  However, when I click on the link, my page simply displays all the CODE I wrote.  I am trying to simple generate a file on Excel 2007 and 2003.

So, wondering if there is something I am missing, the ZIP libraries of my PHP are enable as the requirements mentions. I'm running PHP 5.2.9 and using an Apache webserver.


Thanks for your help.

Jun 22, 2009 at 7:35 AM

The most common cause for the page displaying your code is that the PHP isn't being parsed at all: can you give us more details about your platform/webserver configuration, particularly relating to how you are running PHP.

Jun 22, 2009 at 9:37 AM
Edited Jun 23, 2009 at 11:08 AM

Hi Mark,

Thanks for getting back to me.  Well, I am running Apache Web Server 2.2, PHP 5.2.9 and MySQL Server 5.1.  and I'm running all this on WinXP Pro SP3.  I simply went to each webside and got them from there.  I am creating an application where involves accessing a database in MySQL and display information on the Browser (IE 7, and Google Chrome).  The application works ok, I can connect, and bring data with PHP to my application.  I just have some information that is already displaied on the browser and make it available for my users to download it in an excel file.


Jun 23, 2009 at 11:08 AM

Hi Mark,

You were rigth, was my mistake, I had <? at the begining of my page, instead of <?php.  However, I am getting this error message. 

Fatal error: Class 'Classes/PHPExcel_Writer_Excel5' not found in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\NewAct\Classes\PHPExcel\IOFactory.php on line 130

Any ideas, or something you can help me with ??  Please.

thanks a lot.

Good day.

Jun 23, 2009 at 9:43 PM

I cannot understand how you got that error message. Can you show us some code?

Jun 24, 2009 at 12:40 PM

Hi Koyama,

Well, what I think is happening is this.  Within the PHPExcel_IOFactory there is a procedure to search for location of classes.

 private static $_searchLocations = array(
  array( 'type' => 'IWriter', 'path' => 'Classes/PHPExcel/Writer/{0}.php', 'class' => 'Classes/PHPExcel_Writer_{0}' ),
  array( 'type' => 'IReader', 'path' => 'Classes/PHPExcel/Reader/{0}.php', 'class' => 'Classes/PHPExcel_Reader_{0}' )

Now, the problem is that it is refering to the IWrite file, but inside the IWrite file there is not such a class named PHPeXCEL_Writer_XXXXX that in my case is PHPExcel_Writer_Excel2007.  Here is the contect of the IWriter file.

interface PHPExcel_Writer_IWriter
	 * Save PHPExcel to file
	 * @param 	string 		$pFileName
	 * @throws 	Exception
	public function save($pFilename = null);
Now the actual PHPExcel_Writer_Excel2007 class is located in Classes\PHPExcel\Writer\Excel2007.php file.
Jun 24, 2009 at 1:00 PM

Yes, something is going wrong in that function.

How are you creating the writer? It is supposed to look something like this:

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

Do you have that?

Jun 26, 2009 at 8:51 AM

Hi Koyama,

Sorry I took so long to get back to you.  Here is my code.

/** PHPExcel */
/** PHPExcel_IOFactory */
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Specifiend what sheet we will write on
//Serial Number on first cell.
$objPHPExcel->getActiveSheet()->setCellValue('A1', $_GET['sn']);
//Column Names
$objPHPExcel->getActiveSheet()->setCellValue('A3', 'Date Activated');
$objPHPExcel->getActiveSheet()->setCellValue('B3', 'Time Activated');
$objPHPExcel->getActiveSheet()->setCellValue('D3', 'Machine ID');
$objPHPExcel->getActiveSheet()->setCellValue('E3', 'Company');
$objPHPExcel->getActiveSheet()->setCellValue('F3', 'Product Version');
// Variable to control the looping of the rows.
$CurrentRow = 4;
// Put data records from mysql by while loop.
while (odbc_fetch_row($rs)){
 $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $CurrentRow, $CreationDate);
 $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(2, $CurrentRow, $CreationTime);
 $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(3, $CurrentRow, $MachineId);
 $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(4, $CurrentRow, $CompanyName);
 $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(5, $CurrentRow, $ClientVersion);
// Rename sheet
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
// Save Excel 2007 file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
Jun 26, 2009 at 9:22 PM

I think it could be your include path that is set up wrong. Where can we see your include path?

The reason I am saying this is because you should really do like this:


rather than like this:


Can you try to change your include path so it points to Classes rather than one directory level above? Otherwise the include statements used by PHPExcel internally will not be working.

Jun 30, 2009 at 12:00 AM

Hi Koyoma.

Problem is, if I make that change, then when I run it, I will get error that PHPExcel does not exist.

Here is how it is,

My site folder is call NEWACT, inside, you will find the page I am having the problem with, also the folder CLASSES and in this folder you will  have the PHPExcel folder and the PHPExcel.php file.

Wish I can send you a picture.

Jun 30, 2009 at 2:16 AM

Can you try to run this and tell me what error message you get, if any?

/* This is htdocs/NewAct/index.php */

set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/Classes/');

include 'PHPExcel.php';
include 'PHPExcel/IOFactory.php';

$objPHPExcel = new PHPExcel();


Jun 30, 2009 at 4:50 PM

Hi Koyama,

Here is the error I get.

string(91) ".;C:\php5\pear;C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\NewAct/Classes/"
Fatal error: Class 'Classes/PHPExcel_Writer_Excel2007' not found in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\NewAct\Classes\PHPExcel\IOFactory.php on line 130

Now, I noticed it says C:\php5, and my PHP is installed on C:\php so I do not know if this is causing the problem.


Jun 30, 2009 at 10:34 PM

Everything looks fine, so I can't understand why you that fatal error?

Can you try to debug by replacing line 130 in that IOFactory.php file. Instead of this

$instance = new $className($phpExcel);

Put in this line:

var_dump($className); $instance = new $className($phpExcel);

and see what you get for $className.