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:
<?php
require_once 'Spreadsheet/Excel/Writer.php';
date_default_timezone_set('America/New_York');

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

$endyear= $_POST['endyear'];
if($endyear=="" || $endyear==0)
	$endyear=$year;
	
$endmonth= $_POST['endmonth'];
if($endmonth=="" || $endmonth==0)
	$endmonth=$month;	
	
//$startdate=$year.'-'.$month.'-01';
//$enddate=$year.'-'.$month.'-31';
$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") {
	$fullmonth="January";
}
elseif($month=="02") {
	$fullmonth="February";
}
elseif($month=="03") {
	$fullmonth="March";
}
elseif($month=="04") {
	$fullmonth="April";
}
elseif($month=="05") {
	$fullmonth="May";
}
elseif($month=="06") {
	$fullmonth="June";
}
elseif($month=="07") {
	$fullmonth="July";
}
elseif($month=="08") {
	$fullmonth="August";
}
elseif($month=="09") {
	$fullmonth="September";
}
elseif($month=="10") {
	$fullmonth="October";
}
elseif($month=="11") {
	$fullmonth="November";
}
elseif($month=="12") {
	$fullmonth="December";
}
else {
	$fullmonth="";
}


$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";
$resultb=mysql_query($queryb);
$numb=mysql_numrows($resultb);

// 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();
$dollar_format->setNumFormat('$#.#;[Red]($#.#)');

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

$text =& $workbook->addFormat();
$text->setNumFormat('$#.#;[Red]($#.#)');




// The actual data
if($month == $endmonth)
    $worksheet->write(0, 0, 'Clients Served during '.$fullmonth.' '.$year, $bold);
else
    $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);


$j=0;
$k=3;
while($j<$numb) {

$first1[$j]=mysql_result($resultb,$j,"client.first1");
$last1[$j]=mysql_result($resultb,$j,"client.last1");
$first2[$j]=mysql_result($resultb,$j,"client.first2");
$last2[$j]=mysql_result($resultb,$j,"client.last2");
$house[$j]=mysql_result($resultb,$j,"client.streetnum");
$street[$j]=mysql_result($resultb,$j,"client.streetname");
$strtype[$j]=mysql_result($resultb,$j,"client.streettype");
$apt[$j]=mysql_result($resultb,$j,"client.apt");
$city[$j]=mysql_result($resultb,$j,"client.city");
$zip[$j]=mysql_result($resultb,$j,"client.zip");
$clientid[$j]=mysql_result($resultb,$j,"client.id");
$creationdate[$j]=mysql_result($resultb,$j,"client.creationdate");
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());
$numc=mysql_numrows($resultc);



if($numc>0) {


$timeshelped[$j]=$numc;
$dated[$j]=mysql_result($resultc,0,"creationdate");

$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); 



//$newclient[$j]=0;

$queryd="SELECT * FROM family WHERE clientid=$clientid[$j]";
$resultd=mysql_query($queryd);
$numd=mysql_numrows($resultd);

if($last2[$j]!="") {
	$familysize[$j]=2+$numd;

} else {
	$familysize[$j]=1+$numd;

}

$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]);
$k++;
}
$j++;

}

// Let's send the file
mysql_close();
$workbook->close();
?>

Open in new window

mstoolmiAsked:
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...'
?
0

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.
0
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
PHP

From novice to tech pro — start learning today.