Unreadable file when setting cell value in a loop over a certain amount

Topics: Developer Forum, User Forum
Nov 19, 2013 at 2:29 AM
Edited Nov 19, 2013 at 3:07 AM
I'm having problems generating a kind of complex Excel file that uses a couple of for loops. I've troubleshoot the problem and it boils down to this:

This code produces a valid Excel file:
<?php
require_once 'PHPExcel/PHPExcel.php';

$objPHPExcel = new PHPExcel();

for($i = 0; $i < 17; $i++)
{
    $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue("A$i", $i);
}

$objPHPExcel->setActiveSheetIndex(0);

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="test.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
But this one doesn't:
<?php
require_once 'PHPExcel/PHPExcel.php';

$objPHPExcel = new PHPExcel();

for($i = 0; $i < 18; $i++)
{
    $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue("A$i", $i);
}

$objPHPExcel->setActiveSheetIndex(0);

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="test.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
Hint: over 17 loops, the file gets corrupted...


But this works without a problem:
<?php
require_once 'PHPExcel/PHPExcel.php';

$objPHPExcel = new PHPExcel();

for($i = 0; $i < 100; $i++)
{
    $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue("A$i", 'blaba');
}

$objPHPExcel->setActiveSheetIndex(0);

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="test.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
So the problem is only with integers, at least from my experience. I'm originally outputting a simple line number coming from another system, which exposes the same problem.

My environment:
  • Window Server 2008 SP2
  • IIS 6.0 SP2
  • PHP 5.2.8 (FastCGI)
  • PHPExcel 1.7.9
Could it be IIS? I'm really puzzled!

Thanks if advance for your help!

EDIT: Forgot to specify that once recovered, the excel file is okay except that line 17, 19, 29, 39, etc. have ".:" added to the end. It's always the same numbers but I can't make sense of them...

EDIT 2: Wow, after spending 5 minutes writing this post, I went back and now it's outputting properly! This is crazy, I've been troubleshooting for an hour and I was always able to reproduce the problem. It's got to be IIS but I have no clue what exactly could be causing this. Would anybody have a clue? I know it's getting off topic..

EDIT 3: Started acting up again and "iisreset" fixed the problem. Definitely an IIS problem... sorry for polluting the forum!
Marked as answer by ekred on 11/18/2013 at 6:53 PM