0

I have made a script that outputs a XLS file with data brought from my database. Problem is that when you view the file on OSX and Linux it looks as it is supposed to.

Behaviour on Windows

On Windows excel shows the following message.

The file format and extension 'nameofthefile.xls' don't match. The file could be corrupted or unsafe. Unless you trust.....

Have you ever faced this problem?

$sql    = "MY SQL QUERY";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    $output .= '<table class="table" border="1"
                <tr>
                <th>MY Table heads</th>
                <th>MY Table heads</th> 
               </tr>';

    $count = 1;

    while ($row = mysqli_fetch_array($result)) {
        $data        = strtotime($row['reg_date']);
        $format_date = date("d.M.Y", $data);

        $output .= '
                    <tr>
                    <td>' . $count++ . '</td>
                    <td>' . $row["id"] . '</td>
                       <td>' . $row["firstname"] . '</td>
                          <td>' . $row["lastname"] . '</td>
                                  <td>' . $format_date . '</td>
                    </tr>';
    }
    $output .= '</table>';

    header("Content-Type: application/xls");
    header("Content-Disposition: attachement; filename=download.xls");

    echo $output;


    }
}
phaberest
  • 3,140
  • 3
  • 32
  • 40
Daniel
  • 311
  • 3
  • 5
  • 16
  • I doubt it could be the reason, but there's a typo in your code. `header("Content-Disposition: attachement; filename=download.xls");` should be `header("Content-Disposition: attachment; filename=download.xls");` – phaberest Oct 11 '16 at 20:07
  • Fixed the typo. The problem is still reproducing...Also thank you for pointing it out. – Daniel Oct 11 '16 at 20:19
  • I'd try with another Content-Type, check http://stackoverflow.com/questions/974079/setting-mime-type-for-excel-document – phaberest Oct 11 '16 at 20:23
  • Tried. Same result. Also I have removed parts of HTML to have it as rudimentary as possible. – Daniel Oct 11 '16 at 20:55

2 Answers2

1

I've had the same problem using Laravel with http://www.maatwebsite.nl/laravel-excel/docs and i solve the problem by checking if there was a clean code, i had a apostrophe (') character somewhere in my code and when i detected it i just erased that character and the file was exported with success. Good luck!

Armenta
  • 11
  • 1
  • I found this.Which basically says that its because we try to add HTML in the excel file http://salesforce.stackexchange.com/questions/114584/warning-propmt-on-downloaded-excel-file-format-and-extension-do-not-match-the – Daniel Oct 11 '16 at 21:09
0

I had the same problem with this package for laravel and after researching, i knew that the problem was the web.php file's encoding! in my case Encoding file was UTF-8-BOM and when changed it to UTF-8 problem solved! i did it with notepad++

Hossein
  • 1
  • 1