0

I'm trying to make a functionality that exports a part of my database as readable excel file. Everything works fine but every time I open the downloaded file I keep getting the following error:

The file format and extension of [filename] don't match. The file could be corrupted or unsafe. Unless you trust its source, don't open it. Do you want to open it anyway?

screenshot

the code:

//Get site location
    $siteLocation = Db::getInstance()->queryResults('SELECT place FROM site WHERE site_number=?', array($siteNumber));


// The function header by sending raw excel
header("Content-type: application/vnd-ms-excel");

$documentName = 'Content-Disposition: attachment; filename='. $siteNumber . ' ' . $siteLocation[0]->place .'.xls';

// Defines the name of the export file (siteNumber + site Location)
header($documentName);

// Add data table
include 'exceldata.php';

How can I fix these trust issues Excel has on my compiled excel files?

Ps. when clicking 'yes' the file opens fine and I don't get the same message with other excelfiles.

Thanks in advance!

Frank W.
  • 777
  • 3
  • 14
  • 33
  • Maybe try the other file extensions? .xlt .xls **.xlsx** .xlsm – Radmation Jun 30 '16 at 18:34
  • 1
    If you're not using native Excel BIFF format for your file (e.g. your'e creating html markup, or a csv file), but still using an xls extension, then the latest versions of MS Excel will display this message, because the extension doesn't match the file format.... either create a native format Excel (BIFF) file with the appropriate extension, or change the extension to match the format that you are actually using – Mark Baker Jun 30 '16 at 18:39
  • So what file format does your exceldata.php file actually create? – Mark Baker Jun 30 '16 at 18:40
  • It creates html format. Some html tables with added style – Frank W. Jun 30 '16 at 18:42
  • 1
    So there's your answer.... html markup is not the same as a native-format BIFF-format xls file; and MS Excel will keep giving that message unless either you write the file as a native-format BIFF-format `xls` file, or give it an `html` extension – Mark Baker Jul 01 '16 at 08:34
  • Allright, thanks for your comments. I'm trying to convert my code to phpexcel. Thanks again! – Frank W. Jul 01 '16 at 08:35
  • @MarkBaker, could I kindly ask you to have a look at this question? http://stackoverflow.com/questions/38185924/populate-dropdown-with-phpexcel Thanks! – Frank W. Jul 07 '16 at 09:19

1 Answers1

0
$documentName = 'Content-Disposition: attachment; filename='. $siteNumber .
' ' . $siteLocation[0]->place .'.xlsx'; // try other extension here

Try that?? Hope it works.

Radmation
  • 1,486
  • 1
  • 13
  • 30