当我使用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>
它给了我数据,但问题是当我尝试打开它时也显示警告信息。请帮助
答案 0 :(得分:1)
看起来您正在将HTML输出到Excel文件中。我建议你使用fputcsv函数输出CSV值。并使用以下内容类型:
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename= export.csv');
答案 1 :(得分: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://krasimirtsonev.com/blog/article/php-export-mysql-data-to-xls-file
的参考资料