Exporter vers le file excel, donner un message d'avertissement lorsque vous essayez de l'ouvrir

Quelqu'un pourrait-il m'aider sur la façon d'éliminer cet avertissement lorsque j'exporte Excel en utilisant PHP.

Le file que vous essayez d'ouvrir "example.xls est dans le format différent de celui spécifié par l'extension de file.

Voici mon code, veuillez préciser où est-ce que je fais le mauvais

<?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> 

Cela me donne des données, mais le problème est quand j'essaie d'ouvrir le message d'avertissement.

On dirait que vous produisez du HTML dans un file excel. Je vous suggère d'utiliser la fonction fputcsv pour générer la valeur CSV. Et utilisez le suivant pour le type de contenu:

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

Vous pouvez download en utilisant CSV. Mais si vous voulez un Excel précis, consultez la reference ci-dessous http://phpexcel.codeplex.com/wikipage?title=Examples&referringTitle=Home

phpexcel vous permettra de download en format différent. Mais il était encore lent sur de grandes données. Vous êtes donc le constructor de code ccorePHP, puis allez bien au-dessous de l'exemple.

 <?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, "[email protected]"); // third row xlsWriteNumber(2, 0, 350); xlsWriteLabel(2, 1, "Mark"); xlsWriteLabel(2, 2, "[email protected]"); // end exporting xlsEOF(); exit; ?> <? php <?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, "[email protected]"); // third row xlsWriteNumber(2, 0, 350); xlsWriteLabel(2, 1, "Mark"); xlsWriteLabel(2, 2, "[email protected]"); // end exporting xlsEOF(); exit; ?> 

Référence à partir de http://krasimirtsonev.com/blog/article/php-export-mysql-data-to-xls-file