0

I want to export my table to Excel but I have a problem, only in smarthphones don't go worked and keep showing me the same error.

In addition to saying that the file is corrupted, it keeps giving an error on the form when I download it for cell phones.

list.php



<form method="post" action="export.php">
     <input type="submit" name="export" class="btn btn-success" value="Export" />
    </form>



export.php

<?php  
//export.php  
$connect = mysqli_connect("localhost", "root", "", "contador");
$output = '';
if(isset($_POST["export"]))
{
 $query = "SELECT * FROM contador";
 $result = mysqli_query($connect, $query);
 if(mysqli_num_rows($result) > 0)
 {
  $output .= '
   <table class="table" bordered="1">  
                    <tr>  
                         <th>ID</th>  
                         <th>RequestID</th>  
                         <th>Acessos</th>  
       <th>Cidade</th>
       <th>Data</th>
                    </tr>
  ';
  while($row = mysqli_fetch_array($result))
  {
   $output .= '
    <tr>  
                         <td>'.$row["ID"].'</td>  
                         <td>'.$row["Nome"].'</td>  
                         <td>'.$row["Acessos"].'</td>  
       <td>'.$row["Cidade"].'</td>  
       <td>'.$row["Data"].'</td>
                    </tr>
   ';
  }
  $output .= '</table>';
  header('Content-Type: application/pdf');
  header('Content-Disposition: attachment; filename=listaAcessos.xls');
  echo $output;
 }
}
?>

Olivier
  • 13,283
  • 1
  • 8
  • 24
  • 3
    Your header says the output contains a PDF file, but the actual content is HTML. Then just giving it a filename with the extension .xls doesn't make it an Excel file. – KIKO Software Aug 20 '22 at 11:46
  • 2
    _"keep showing me the same error"_ - When posting a question, please always add any errors you get (in full). – M. Eriksson Aug 20 '22 at 11:46
  • 1
    Use an Excel library to generate real Excel files. – Olivier Aug 20 '22 at 11:59
  • @say "says this file type is not supported for this office version" – LostFriend Aug 20 '22 at 12:10
  • “not worked” - please try to put more effort into this if you expect others to do the same. The code and question show MySQL, HTML, Excel and PDF, which do you want help with? Is it converting an HTML table to an Excel spreadsheet? Convert SQL to a spreadsheet? Converting HTML to a PDF? – Chris Haas Aug 20 '22 at 14:47
  • Consider creating a valid .xls-file, e.g. with PHPSpreadsheet like described here: https://stackoverflow.com/a/61619881/4274040 – Christian Abila Aug 21 '22 at 05:01

0 Answers0