Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在使用php导出xls后,当我打开excel文件时,它会将消息显示为';不同格式';_Php_Excel_Export To Excel - Fatal编程技术网

在使用php导出xls后,当我打开excel文件时,它会将消息显示为';不同格式';

在使用php导出xls后,当我打开excel文件时,它会将消息显示为';不同格式';,php,excel,export-to-excel,Php,Excel,Export To Excel,我正在使用php导出excel(.xls)文件。导出特定文件后 当我打开文件时,它的意思是“ **您试图打开的文件 “file.xls”的格式与文件扩展名指定的格式不同 验证文件是否未损坏,并且来自受信任的源 在打开文件之前** “ 这是我的密码 $data = array( array("firstname" => "Mary", "lastname" => "Johnson", "age" => 25), array("firstname" =>

我正在使用php导出excel(.xls)文件。导出特定文件后

当我打开文件时,它的意思是“

**您试图打开的文件

“file.xls”的格式与文件扩展名指定的格式不同

验证文件是否未损坏,并且来自受信任的源

在打开文件之前**

这是我的密码

  $data = array(
    array("firstname" => "Mary", "lastname" => "Johnson", "age" => 25),
    array("firstname" => "Amanda", "lastname" => "Miller", "age" => 18),
  );   


  function cleanData(&$str)
      {
        $str = preg_replace("/\t/", "\\t", $str);
        $str = preg_replace("/\r?\n/", "\\n", $str);
        if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
      } 

      // filename for download
      $filename = $formname."_".$form_id."_". date('Ymd') . ".xls";

      header("Content-Disposition: attachment; filename=\"$filename\"");
      header("Content-Type: application/vnd.ms-excel");

      $flag = false;
      foreach($data as $row) {
        if(!$flag) {
          // display field/column names as first row
          //echo implode("\t", array_keys($row)) . "\r\n";
          $flag = true;
        }
        array_walk($row, 'cleanData');
        echo implode("\t", array_values($row)) . "\r\n";
      }
      exit;

您正在创建Excel 2007文件吗


如果是这样,请尝试使用mimetype应用程序/vnd.openxmlformats-officedocument.spreadsheetml.sheet和文件扩展名.xslx

您不是在创建XLS文件,而是在创建以制表符分隔的文本文件。这就是Excel抱怨文件结尾与文件内容不一致的原因


要创建真正的XLS文件,您需要一个能够写入此类文件的库(有一些库可以写入旧的Excel XLS格式、Excel XML(2003)格式和新的Excel XLSX格式)。

否。我需要创建2003 Excel文件。我尝试了“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet和文件扩展名.xlsx。”它显示相同的错误和空白文件。