Search code examples
phpencodingdownloadphpexcel

PHPExcel download file, excel file hieroglyphics


i have a problem to download excel file. When im saving on server its worked ok. But when i try to download things goes wrong.This is my code:

$result = getSoapResult($soapURL, $soapWSDL, $soapMETHOD, $inputParameters);
header("Content-Type: application/vnd.ms-excel; charset=utf-8");
header('Content-Disposition: attachment;filename="file_name.xls"');
header('Cache-Control: max-age=0');
$objExcel = new PHPExcel();

$objExcel->setActiveSheetIndex(0);
$rowCount = 1;

$column = 'A';
foreach($result as $key => $value){
        if($rowCount == 1){
            foreach($value as $k => $v){
                $objExcel->getActiveSheet()->SetCellValue($column.$rowCount, $k);
                $column++;
            }
            $rowCount++;
        }
        $column = 'A';
        foreach($value as $k => $v){
            $objExcel->getActiveSheet()->SetCellValue($column.$rowCount, $v);
            $column++;
        }
    $rowCount++;
}

$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
ob_clean();
$objWriter->save('php://output');

it gives an excel file with this content:

enter image description here

I cant find answer from another questions with this problem.


Solution

  • The PK at the begining gives away it's a zip file. Coincidentally, xlsx is a special kind of zip file. Try to save the file as .xlsx instead of .xls and Excel should handle it correctly.

    Given you're specifying Excel2007 as the format, you're getting an .xlsx file. If you need a .xls file, maybe you can use Excel2003 instead.