从php创建的“xlsx”文件在excel中打开时会出现格式错误

时间:2015-06-06 07:39:19

标签: php excel

最初我从php创建了可下载的“xls”文件,它在Excel中打开。但是当我创建xlsx文件时,它会以正确的文件名和扩展名存储,但它会出现错误“Excel无法打开文件'myfile.xlsx',因为文件格式或文件扩展名无效。请确认该文件尚未存在腐败的“

我的PHP的标题信息设置代码是:

    header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    header("Content-Disposition: attachment;filename=\"filename.xlsx\"");
    header("Pragma: no-cache");
    header("Expires: 0");

在excel文件中输入值的代码是:

 // Define separator (defines columns in excel; tabs in word)
 $sep = "\t"; // tabbed character

// Start of printing column names as names of MySQL fields
for ($i = 0; $i<mysql_num_fields($result); $i++) {
 echo mysql_field_name($result, $i) . "\t";
}
print("\n");
// End of printing column names

// Start while loop to get data
while($row = mysql_fetch_row($result))
{

$schema_insert = "";
for($j=0; $j<mysql_num_fields($result); $j++)
{
if(!isset($row[$j])) {
  $schema_insert .= "NULL".$sep;
}
elseif ($row[$j] != "") {
  $schema_insert .= "$row[$j]".$sep;
}
else {
  $schema_insert .= "".$sep;
}
 }
 $schema_insert = str_replace($sep."$", "", $schema_insert);
 $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
 $schema_insert .= "\t";
 print(trim($schema_insert));
 print "\n";
  }

它可能是什么问题?

0 个答案:

没有答案