如何解决PhpSpreadsheet上无法读取的内容?

时间:2019-07-10 07:54:07

标签: php excel spreadsheet

我具有使用phpspreadsheet生成我的excel报告的功能。我的问题是,每当生成文件并使用较低版本的MsExcel时,它说Excel found unreadable content in 'sample.xlsx'. Do you want to recover...,幸运的是,如果我继续它会显示报告,但是如果我在较高版本的Excel上打开它,则说Excel cannot open the file 'sample.xlsx' because the file format or file extension is not valid. Verify ...。当我尝试在texteditor上打开它时,发现

<!DOCTYPE html>
<html>
<head>
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
    <link href="../../includes/static/css/styles.min.css" rel="stylesheet">
    <link rel="shortcut icon" type="image/x-icon" href="../../favicon.ico">
    <title>Employee Mgt System</title>
</head>
<html>
...
</html>

关于我的功能,我有这个:

function generateReport($flag, $nationality, $month, $year1, $year2, $job, $department, $section, $category, $employee_type, $gender, $contract_type){
   //sql query
   $this->generateExcelReport($data);
}

这是我的generateExcelReport函数:

function generateExcelReport($data){
   $spreadsheet = new Spreadsheet();
   $sheet = $spreadsheet->getActiveSheet();

   foreach ($data as $emp) {
      $spreadsheet->getActiveSheet()
      ->setCellValue('A'.$row, $emp['emp_number'])
      ->setCellValue('B'.$row, $emp['full_name']);
   }

   header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

   header('Content-Disposition: attachment;filename="My Report.xlsx"');

   $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
   $writer->save('php://output');
}

我无法弄清楚我的错误,非常感谢您的帮助。

0 个答案:

没有答案