Q:

php spreadsheet- unable to view excel file

Hi I am working in yii2. I have a function in which I am making an excel file through my mysql query. 
 $sql = "SELECT SUM(OGP_Created)  AS OGP_Created,SUM(UnVerifiedMSN) AS 
 Un_Verified_Meters,SUM(VerifiedMeters) AS Verified_Meters,SDCode AS 
 Sub_Div_Code
,SDName AS Sub_Div_Name FROM(
SELECT COUNT(DISTINCT od.`meter_id`) AS OGP_Created,0 AS UnVerifiedMSN,0 AS 
VerifiedMeters, sd.`sub_div_code` AS 'SDCode', sd.`name` AS 'SDName' 
FROM `ogp_detail` od
LEFT JOIN `survey_hesco_subdivision` sd ON od.`sub_div` = sd.`sub_div_code`
WHERE od.`meter_type` = '3-Phase'
GROUP BY sd.`name`

UNION ALL

SELECT 0 AS OGP_Created,COUNT(DISTINCT mp.`meter_id`) AS UnVerifiedMSN,0 AS 
VerifiedMeters, sd.`sub_div_code` AS 'SDCode', sd.`name` AS 'SDName'
FROM `meter_ping` mp 
INNER JOIN `meters` m ON mp.`meter_id` = m.`id`
INNER JOIN `survey_hesco_subdivision` sd ON mp.`sub_div_code` = 
sd.`sub_div_code`
WHERE mp.`meter_type`= '3-Phase' AND mp.`meter_status` = 'Un Verified'
GROUP BY sd.`name`

UNION ALL

SELECT 0 AS OGP_Created,0 AS UnVerifiedMSN,COUNT(DISTINCT m.`id`) AS 
VerifiedMeters, sd.`sub_div_code` AS 'SDCode', sd.`name` AS 'SDName'
FROM `meters` m
INNER JOIN `survey_hesco_subdivision` sd ON m.`sub_div` = sd.`sub_div_code`
WHERE m.`meter_type` = '3-Phase' AND m.`meter_status` = 'Installed'
GROUP BY sd.`name`
)z 
GROUP BY SDName";

    $result = Yii::$app->db->createCommand($sql)->queryAll();

Passing the $result
    //create a xlsx file
    $filename   =   $this->getAttachment($result);

getAttachment function
 $spreadsheet = new Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue('A1', 'OGP Created')
          ->setCellValue('B1','Un Verified Meters')
          ->setCellValue('C1','Verified Meters')
          ->setCellValue('D1','Sub Div Code')
          ->setCellValue('E1','Sub Div Name');

    foreach($results as $key => $result_data) {
        $x = $key + 2;
        $spreadsheet->setActiveSheetIndex(0)
            ->setCellValue("A$x", $result_data['OGP_Created'])
            ->setCellValue("B$x", $result_data['Un_Verified_Meters'])
            ->setCellValue("C$x", $result_data['Verified_Meters'])
            ->setCellValue("D$x", $result_data['Sub_Div_Code'])
            ->setCellValue("E$x", $result_data['Sub_Div_Name']);
    }
    $filename = 'Output.xlsx'; //save our workbook as this file name
    // Redirect output to a client’s web browser (Xlsx)
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="'.$filename.'"');
    header('Cache-Control: max-age=0');
    // If you're serving to IE 9, then the following may be needed
    header('Cache-Control: max-age=1');

    die();

Now I when I run the above code. The file is downloaded but when I open the file it says 

How can I make a correct excel file? Any help would be highly appreciated

A:

Refer PhpSpreadsheet
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'OGP Created')
      ->setCellValue('B1','Un Verified Meters')
      ->setCellValue('C1','Verified Meters')
      ->setCellValue('D1','Sub Div Code')
      ->setCellValue('E1','Sub Div Name');

foreach($results as $key => $result_data) {
    $x = $key + 2;
    $spreadsheet->setActiveSheetIndex(0)
        ->setCellValue("A$x", $result_data['OGP_Created'])
        ->setCellValue("B$x", $result_data['Un_Verified_Meters'])
        ->setCellValue("C$x", $result_data['Verified_Meters'])
        ->setCellValue("D$x", $result_data['Sub_Div_Code'])
        ->setCellValue("E$x", $result_data['Sub_Div_Name']);
}
$filename = 'Output.xlsx'; //save our workbook as this file name
// Redirect output to a client’s web browser (Xlsx)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
die();

