user880386
user880386

Reputation: 2827

Error exporting to excel in PHP

When I execute this:

<?php
        header("Content-type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=test.xls");
        header("Pragma: no-cache");
        header("Expires: 0");

       $flag = false;

       foreach($data as $row) {

         if(!$flag) {
           echo implode("\t", array_keys($row)) . "\n";
           $flag = true;
         }
        array_walk($row, __NAMESPACE__ . '\cleanData');
        echo implode("\t", array_values($row)) . "\n";
      }
      exit;

I got error like below when am opening the downloaded .xls file.

The file You are trying to open Facemappsoft.xls is in a different format than file extension

Can any one help me to resolve this right now?

Upvotes: 1

Views: 1141

Answers (1)

Phylogenesis
Phylogenesis

Reputation: 7890

You are generating a tab-separated values file and giving it a Excel 97-2003 compatible file extension (*.xls).

Excel is complaining about this mismatch when opening the file.

To fix the issue, you will need to give the file an appropriate extension (*.tsv) and update the Content-Type header to one properly describing the file format, such as:

Content-Type: text/tab-separated-values

Upvotes: 3

Related Questions