PHPExcel文件格式或扩展名无效 [英] PHPExcel File format or extension is not valid

查看:458
本文介绍了PHPExcel文件格式或扩展名无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用phpexcel将查询导出到excel文件中;但是,在创建文件(xslx格式)之后,无法在excel中打开文件.它给出文件格式或扩展名无效.验证文件未损坏且文件扩展名与文件的文件格式匹配"错误.当我在texteditor(mine是npp)中打开文件时,我看到了php文件的css代码和html代码的某些部分.我的代码就是这样;

I'm using phpexcel for export my query in excel file; however after I created file(which is xslx format), I can not open my file in excel. It gives "the file format or extension is not valid. Verify that the file has not been corrupted and that the file extension matches the file format of the file" error. When I open the file in texteditor(mine is npp) I see my php file's css codes and some part of my html codes. My code is like that;

if( ! empty($_POST['export'])){
  $query = "SELECT * FROM asd ORDER BY asdf LIMIT 10";
  $headings = array('Timestamp', 'h1','h2');
      require 'Classes/PHPExcel.php';

  $objPHPExcel = new PHPExcel();
  $objPHPExcel->getActiveSheet()->setTitle('List of Users');

  $rowNumber = 1;
  $col = 'A';
  foreach($headings as $heading) {
    $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
    $col++;
  }

  $rowNumber = 2;
  while ($row = mysql_fetch_row($result)) {
    $col = 'A';
    foreach($row as $cell) {
        $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell); 
        $col++;
    }
    $rowNumber++;
  }

$objPHPExcel->getActiveSheet()->freezePane('A2');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="userList.xlsx"');
header("Content-Transfer-Encoding: binary ");
//ob_end_clean();
//header('Cache-Control: max-age=0');

$objWriter->save('php://output');
exit();
}

我被卡住了,请帮忙.谢谢.

I'm stucked please help. Thank you.

推荐答案

如手册中所述....如果正在向浏览器输出 任何内容 ,这将损坏输出文件.

As described in the manual.... if anything else is being output to the browser, this will corrupt the output file.

在文本编辑器中打开文件,并在输出的开头查找开头或结尾的空格字符(空格,制表符,换行符)或BOM标记,或在内容中查找任何明显的PHP纯文本错误消息.这些是此问题的最明显原因.确定伪造字符后,请检查脚本以查看生成该输出的位置,然后将其删除.

Open the file in a text editor, and look for leading or trailing whitespace characters (spaces, tabs, newlines) or a BOM marker at the beginning of the output, or for any obvious PHP plaintext error messages in the content. These are the most obvious causes of this problem. Once you've identified the spurious characters, check through your script to see where that output is being generated, and remove it.

对于您而言,这意味着 不要 输出您的CSS和html.

In your case, that means don't output your css and html.

编辑

xlsx是OfficeOpenXML Excel2007文件的扩展名,而不是BIFF 8 xls文件的扩展名....标头(MIME类型和文件扩展名)和Writer保持一致

xlsx is the extension for an OfficeOpenXML Excel2007 file, not for a BIFF 8 xls file.... be consistent in your headers (mime type and file extension) and Writer

要么:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="userList.xls"');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="userList.xlsx"');

这篇关于PHPExcel文件格式或扩展名无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆