Error with PHP PEAR Excel Writer with Window: file format or file extensions is not valid

I receive the following error when I try to open an Excel file created with PEAR Excel Writer:

Excel cannot open the file 'filename.xlsx' because the file format or the file extension is not valid.  Verify that the file has not been corrupted and that the file extension matches the format of the file.

Same error if I switch the extension to .xls.

This only happens with windows 7 and 8 and with Excel 2010 and 2013.
I am using PHP version: 5.3, PEAR version: 1.9.4, OLE version: 1.0.0RC2, Excel Wirter version: 0.9.3

Here is the code:
require_once 'Spreadsheet/Excel/Writer.php';

$year= $_POST['year'];
$month= $_POST['month'];

$endyear= $_POST['endyear'];
if($endyear=="" || $endyear==0)
$endmonth= $_POST['endmonth'];
if($endmonth=="" || $endmonth==0)
$startdate=date( 'Y-m-d H:i:s',mktime(0, 0, 0, $month, 1, $year));
//$enddate=date( 'Y-m-d H:i:s',mktime(23, 59, 59, $month, 31, $year));
$enddate=date('Y-m-d H:i:s',mktime(0,0-1,0,$endmonth+1,1,$endyear));

if($month=="01") {
elseif($month=="02") {
elseif($month=="03") {
elseif($month=="04") {
elseif($month=="05") {
elseif($month=="06") {
elseif($month=="07") {
elseif($month=="08") {
elseif($month=="09") {
elseif($month=="10") {
elseif($month=="11") {
elseif($month=="12") {
else {

$dbh=mysql_connect (<connection-info>) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("<database>") or die('Could not select database because' . mysql_error());

$queryb="SELECT * FROM client";

// Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();

// sending HTTP headers
$workbook->send('monthly report - '.$fullmonth.' '.$year.'.xlsx');

// Creating a worksheet
$worksheet =& $workbook->addWorksheet('worksheet');

/*$worksheet->setColumn(0, 1, 10);
$worksheet->setColumn(2, 2, 15);
$worksheet->setColumn(3, 4, 20);
$worksheet->setColumn(5, 5, 15);
$worksheet->setColumn(6, 6, 4);
$worksheet->setColumn(7, 7, 7);
$worksheet->setColumn(8, 10, 13);
$worksheet->setColumn(11, 11, 25);
$worksheet->setColumn(12, 12, 7);
$worksheet->setColumn(13, 13, 7);
$worksheet->setColumn(14, 14, 25);*/

$dollar_format =& $workbook->addFormat();

$bold =& $workbook->addFormat();

$text =& $workbook->addFormat();

// The actual data
if($month == $endmonth)
    $worksheet->write(0, 0, 'Clients Served during '.$fullmonth.' '.$year, $bold);
    $worksheet->write(0, 0, 'Clients Served from '.$fullmonth.' '.$year.' to '.$fullmonth.' '.$year, $bold);

$worksheet->write(2, 0, 'FName1', $bold);
$worksheet->write(2, 1, 'LName1', $bold);
$worksheet->write(2, 2, 'House', $bold);
$worksheet->write(2, 3, 'Street', $bold);
$worksheet->write(2, 4, 'StrType', $bold);
$worksheet->write(2, 5, 'Apt', $bold);
$worksheet->write(2, 6, 'CityName', $bold);
$worksheet->write(2, 7, 'Zip', $bold);
$worksheet->write(2, 8, 'FamilySize', $bold);
$worksheet->write(2, 9, 'TimesHelped', $bold);
$worksheet->write(2, 10, 'EntryDate', $bold);
$worksheet->write(2, 11, 'New Client Flag', $bold);

//$worksheet->write(0, 10, $startdate, $bold);
//$worksheet->write(0, 11, $enddate, $bold);

while($j<$numb) {

if($creationdate[$j] >= $startdate && $creationdate[$j] <= $enddate) {
	$newclient[$j] = "yes";
} else {
	$newclient[$j] ="no";

$queryc="SELECT * FROM help WHERE clientid=$clientid[$j] AND creationdate >= '$startdate' AND creationdate <= '$enddate' AND (need = 'PANTRY' OR need = 'DRIVE') ORDER BY creationdate DESC";
$resultc=mysql_query($queryc) or die ('I can not find the record because:' . mysql_error());

if($numc>0) {


$hour = substr($dated[$j],11,2);

$minute = substr($dated[$j],14,2);

$second = substr($dated[$j],17,2);

$month = substr($dated[$j],5,2);

$day = substr($dated[$j],8,2);

$year = substr($dated[$j],0,4);

$mktime = mktime($hour, $minute, $second, $month, $day, $year);
$entrydate[$j] = date("Y-m-d",$mktime); 


$queryd="SELECT * FROM family WHERE clientid=$clientid[$j]";

if($last2[$j]!="") {

} else {


$worksheet->write($k, 0, $first1[$j]);
$worksheet->write($k, 1, $last1[$j]);
$worksheet->write($k, 2, $house[$j]);
$worksheet->write($k, 3, $street[$j]);
$worksheet->write($k, 4, $strtype[$j]);
$worksheet->write($k, 5, $apt[$j]);

$worksheet->write($k, 6, $city[$j]);
$worksheet->write($k, 7, $zip[$j]);

$worksheet->write($k, 8, $familysize[$j]);
$worksheet->write($k, 9, $timeshelped[$j]);
$worksheet->write($k, 10, $entrydate[$j]);
$worksheet->write($k, 11, $newclient[$j]);


// Let's send the file

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lukasz ChmielewskiCommented:
Did you try to use the filename without the space
'monthly report' -> 'monthly_report...'

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mstoolmiAuthor Commented:
The problem ended up being a know problem, and I needed to use a different solution.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.