I am trying to generate an Excel file from PHP. I tried copying code from various sources on the internet, but they give me an error "Fatal error: Cannot redeclare class PHPExcel in ....../PHPExcel.php on line 44". My code is as follows:

// include PHPExcel
require_once 'PHPExcel.php';

ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE); 
echo "<br> create new PHPExcel object";
$objPHPExcel = new PHPExcel;
echo "<br> set default font".
echo "<br> set default font size";
echo "<br> create the writer";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
echo "<br> * Define currency and number format. ";

// currency format, € with < 0 being in red color
$currencyFormat = '#,#0.## \€;[Red]-#,#0.## \€';
// number format, with thousands separator and two decimal points.
$numberFormat = '#,#0.##;[Red]-#,#0.##';
// writer already created the first sheet for us, let's get it
$objSheet = $objPHPExcel->getActiveSheet();
// rename the sheet
$objSheet->setTitle('My sales report');
// let's bold and size the header font and write the header
// as you can see, we can specify a range of cells, like here: cells from A1 to A4
// write header
$objSheet->getCell('D1')->setValue('Total Price');
// we could get this data from database, but for simplicty, let's just write it
// bold and resize the font of the last row
// set number and currency format to columns
// create some borders
// first, create the whole grid around the table
// create medium border around the table
// create a double border above total line
// create a medium border on the header line
// autosize the columns
// write the file


I included the echo commands to be sure of where the error was generated. In fact, it is
the line "$objPHPExcel = new PHPExcel;", which suggests to me that this has been declared twice, which is not the case. I cannot work out what the problem is. Can anyone help? I am running this in MAMP.
Thanks in advance.

2 Years
Discussion Span
Last Post by rubberman
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.