php电子表格 - 无法查看excel文件

时间:2018-05-24 10:15:36

标签: yii2 yii2-advanced-app phpspreadsheet

您好我在yii2工作。我有一个功能,我通过mysql query制作一个excel文件。

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

传递$result

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

getAttachment功能

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

现在我运行上面的代码。文件已下载,但是当我打开文件时,它会显示

enter image description here

如何制作正确的Excel文件?任何帮助都将受到高度赞赏

1 个答案:

答案 0 :(得分:2)

参考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();