download from $res


<?
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

require_once($_SERVER['DOCUMENT_ROOT'] . "/php_excel/PHPExcel.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/php_excel/PHPExcel/Writer/Excel2007.php");
require(getcwd() . "/data.php");




function conv($l){
    return iconv("windows-1251", "utf-8", $l);
}

function dTol($di) {
    return chr(64+ $di) ;
}


    $objPHPExcel     = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('one');

$l = $objPHPExcel->setActiveSheetIndex(0) ;


$row = 1 ;
$col = 0 ;


$l->setCellValueByColumnAndRow(  $col++, $row , conv(  " Наименование налога/льготы" ) );
$l->getStyle( "A$row:" ."A$row")->applyFromArray(array('font' => array('size' => 10,'bold' => true,'color' => array('rgb' => '0000DD'))));

foreach ($resHead as $key4 => $value4) {
    $l->setCellValueByColumnAndRow(  $col++, $row , conv($value4 .  " млн.руб." ) );
}
$row++;

foreach ($res as $k=>$v){
    $col = 0 ;
    $l->setCellValueByColumnAndRow(  $col++, $row , conv($v['name']) );
    foreach ($v['vals'] as $k2=>$v2){
        $l->setCellValueByColumnAndRow(  $col++, $row , conv($v2) );
    }
    $l->getStyle( "A$row:" . dTol($col) ."$row")->applyFromArray(array('font' => array('size' => 10,'bold' => true,'color' => array('rgb' => '0000DD'))));
    $row++;

    foreach ($v['sub'] as $k3=>$v3){
        $col = 0 ;
        $l->setCellValueByColumnAndRow(  $col++, $row , conv($v3['name']) );
        foreach ($v3['vals'] as $k4 => $v4 )
            $l->setCellValueByColumnAndRow(  $col++, $row , conv($v4) );
        $row++;
    }
}


// Add some data



//file_put_contents(   "/log/1.txt", "\n\n\n".print_r($res,true)  );



header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // header for .xlxs file
header('Content-Disposition: attachment;filename=' . "report.xlsx"); // specify the download file name
header('Cache-Control: max-age=0');

// Creates a writer to output the $objPHPExcel's content
$writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$writer->save('php://output');

exit;