导出到excel文件时,尝试打开时会给出警告消息

任何人都可以帮助我在使用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("Ymd", 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> 

它给我的数据,但问题是,当我尝试打开它显示警告消息also.Please帮助

看起来你正在输出HTML到一个Excel文件。 我build议你使用fputcsv函数输出CSV值。 并使用以下内容types:

  header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename= export.csv'); 

您可以使用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("Ymd").".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