1

当我使用 PHP 导出 Excel 时,任何人都可以帮助我如何删除此警告。

您尝试打开的文件“example.xls 的格式与文件扩展名指定的格式不同。

这是我的代码,请说明哪里做错了

<?php

// Start a session
session_start();

// Define variables from $_SESSION and $_GET
$firstname = $_SESSION['firstname']; 
$lastname = $_SESSION['lastname'];
$username = $_SESSION['username'];

$start = $_GET['start'];
$end = $_GET['end'];
$ProviderID = $_GET['ProviderID'];
$summarytype = $_GET['summarytype'];
$subspeciality = $_GET['subspeciality'];

$export = $_GET['export'];

// Connect to mysql db
include ("access.php");

header("Content-type: application/vnd.ms-excel; name='excel'");
header("Content-Disposition: filename=export.xls");
// Fix for crappy IE bug in download.
header("Pragma: ");
header("Cache-Control: ");

error_reporting("E_ALL");

$sql = "SELECT *, master.EmtracSigChange AS 'SigChange' FROM master, nightrad WHERE master.EmtracSigChange='Yes' AND master.MaryGrade!='' AND master.InternalExamID=nightrad.InternalExamID AND master.TranscriptionDTTM <= '$end' AND master.TranscriptionDTTM >= '$start'";

$result = mysql_db_query('testDb',$sql);

if(!$result) {
echo "<p>No matches to your query</p>";
echo "<p>Click back on your browser to change your query parameters.</p>";
die(mysql_error());
}

?>
<html>
<head></head>
<body>
<table border="1" cellspacing="0" cellpadding="3">
<tr>
<th bgcolor="#0099FF" scope="col">Accession</th>
<th bgcolor="#0099FF" scope="col">Transcribed</th>
<th bgcolor="#0099FF" scope="col">Turnaround time</th>
<th bgcolor="#0099FF" scope="col">Attending</th>
<th bgcolor="#0099FF" scope="col">Res or fellow</th>
<th bgcolor="#0099FF" scope="col">Modality</th>
<th bgcolor="#0099FF" scope="col">Exam</th>
<th bgcolor="#0099FF" scope="col">Discrepancy</th>
<th bgcolor="#0099FF" scope="col">Folder</th>
<th bgcolor="#0099FF" scope="col">Comment</th>
</tr>
<?php 
// Add all values in the table to $out.
while ($row = mysql_fetch_assoc($result)) {
$CompletedDTTM = $row['CompletedDTTM'];
$TranscriptionDTTM = $row['TranscriptionDTTM'];
$date = date("Y-m-d", strtotime($TranscriptionDTTM));

// Converting turnaround time from DTTM to time
$parsedtime = strtotime($CompletedDTTM . " GMT");
$convertedtime = gmdate("H:i:s", $parsedtime);

$parsedtime1 = strtotime($TranscriptionDTTM . " GMT");
$convertedtime1 = gmdate("H:i:s", $parsedtime1);
$parsedtat = $parsedtime1 - $parsedtime;
$turnaroundtime = gmdate("H:i", $parsedtat);

?>
<tr >
<td><?php echo $row['AccessionNumber']; ?></td>
<td><?php echo $date; ?></td>
<td><?php echo $turnaroundtime; ?></td>
<td><?php echo $row['AttendingLastName']; ?></td>
<td><?php echo $row['LastName']; ?></td>
<td><?php echo $row['Modality']; ?></td>
<td><?php echo $row['ExamDesc']; ?></td>
<td><?php echo $row['MaryGrade']; ?></td>
<td><?php echo $row['MaryFolder']; ?></td>
<td><?php echo $row['MaryComment'] ?></td>

</tr>

<?php
}      
?>

</table>
</body>
</html>

它给了我数据,但问题是当我尝试打开它时也会显示警告消息。请帮助

4

2 回答 2

1

看起来您正在将 HTML 输出到 excel 文件中。我建议您使用fputcsv函数来输出 CSV 值。并使用以下内容类型:

  header('Content-Type: text/csv; charset=utf-8');
  header('Content-Disposition: attachment; filename= export.csv');
于 2013-02-11T06:42:25.417 回答
0

您可以使用 CSV 下载。但是,如果您想要精确的 Excel,请查看以下参考 http://phpexcel.codeplex.com/wikipage?title=Examples&referringTitle=Home

phpexcel 会让你以不同的格式下载。但是在大数据上仍然很慢。那么,您是 ccorePHP 代码构建器,然后远远低于示例。

<?php

function xlsBOF() {
    echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
}
function xlsEOF() {
    echo pack("ss", 0x0A, 0x00);
}
function xlsWriteNumber($Row, $Col, $Value) {
    echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
    echo pack("d", $Value);
}
function xlsWriteLabel($Row, $Col, $Value) {
    $L = strlen($Value);
    echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
    echo $Value;
} 
// prepare headers information
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=\"export_".date("Y-m-d").".xls\"");
header("Content-Transfer-Encoding: binary");
header("Pragma: no-cache");
header("Expires: 0");
// start exporting
xlsBOF();
// first row 
xlsWriteLabel(0, 0, "id");
xlsWriteLabel(0, 1, "name");
xlsWriteLabel(0, 2, "email");
// second row 
xlsWriteNumber(1, 0, 230);
xlsWriteLabel(1, 1, "John");
xlsWriteLabel(1, 2, "john@yahoo.com");
// third row 
xlsWriteNumber(2, 0, 350);
xlsWriteLabel(2, 1, "Mark");
xlsWriteLabel(2, 2, "mark@yahoo.com");
// end exporting
xlsEOF();
exit;

?>

来自http://krasimrtsonev.com/blog/article/php-export-mysql-data-to-xls-file的参考资料

于 2017-03-03T03:42:14.480 回答