0

当我打开 Excel 文件时出现消息:您尝试打开的文件“文件名”.xls 的格式与文件扩展名指定的格式不同。在打开之前验证文件没有损坏并且来自受信任的来源文件。”

The output is like this: ÐÏࡱá;þÿ þÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ >¶@d‹‹dggÿÿÿÿÿ .....

这是我的代码..

<?php
require_once 'database.php';

include 'PHPExcel.php';

$phpExcel = new PHPExcel();

$phpExcel->getActiveSheet()->setTitle("My Sheet");

$phpExcel->setActiveSheetIndex(0)
     ->setCellValue('A1', 'Name.')
     ->setCellValue('B1', 'Age');

$qry_table = ("SELECT * FROM MEMBERS");

$inc=2;

while($data_array = mysql_fetch_array($qry_table))
{

$name   = $data_array['Name'];

$age    = $data_array['Age'];

$$phpExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.$inc, $name)
            ->setCellValue('B'.$inc, $age);
$inc++;
}

$phpExcel->setActiveSheetIndex(0);

header("Content-Type: application/vnd.ms-excel");

header("Content-Disposition: attachment; filename=\"filename.xls\"");

header("Cache-Control: max-age=0");

$objWriter = PHPExcel_IOFactory::createWriter($phpExcel, "Excel5");

$objWriter->save("php://output");
exit;
4

2 回答 2

0

Where's your "mysql_query()" to query the database? Change

$qry_table = ("SELECT * FROM MEMBERS");

to

$qry_table = mysql_query("SELECT * FROM MEMBERS");

//Edit: And you've got a pointer ref. to an a variable which does not exsits Change:

$$phpExcel->setActiveSheetIndex(0)
        ->setCellValue('A'.$inc, $name)
        ->setCellValue('B'.$inc, $age);

to:

$phpExcel->setActiveSheetIndex(0)
        ->setCellValue('A'.$inc, $name);

$phpExcel->setActiveSheetIndex(0)
        ->setCellValue('B'.$inc, $age);
于 2013-06-24T08:42:00.823 回答
0

当您收到此错误时,我们始终建议您在文本编辑器中打开文件并检查文件中的前导或尾随空格(空格、制表符、换行符)、BOM 标记或任何明显的纯文本 PHP 错误消息。

尝试将文件保存到您的网络服务器,然后打开它并查看是否出现相同的错误

于 2013-06-24T18:52:10.250 回答