php 电子表格 - 无法查看 excel 文件
php spreadsheet- unable to view excel file
您好,我在 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();
现在我当我运行上面的代码。文件已下载,但当我打开文件时显示
如何制作正确的 excel 文件?任何帮助将不胜感激
$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();
您好,我在 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();
现在我当我运行上面的代码。文件已下载,但当我打开文件时显示
如何制作正确的 excel 文件?任何帮助将不胜感激
$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();