0

So I have problems opening .csv file which is downloaded with IE11 and If I use chrome or mozilla etc. file is formatted well.

Case: In php I am generating list and export as .csv. If I download that file with ex. Chrome and open in Excel, each column is formatted well, but same process in IE11 and output is unformatted text.

Below you can find code, and outputs:

Here is the php code enter image description here

Here is .csv file (downloaded using chrome, mozilla, opera, safari) enter image description here

And here is output from IE11

enter image description here

Does anybody have idea how to solve this issue?

JohnWayne
  • 651
  • 9
  • 28
  • To do that you have to import data into excel via wizard and choose column separator semicolon instead of comma – Alex Slipknot Sep 07 '17 at 08:21
  • 1
    See this question - may be related and help: https://stackoverflow.com/questions/2232103/how-do-i-get-csv-file-to-download-on-ie-works-on-firefox – Bananaapple Sep 07 '17 at 08:22
  • @AlexSlipknot but why it works in Chrome and other browser with normal openning? I know that I can do this but its hard to explain to customers this process. There is also option to change settings in Control Panel but it must be another solution (add something to header etc.) – JohnWayne Sep 07 '17 at 08:23
  • Code;Place;Country **,** Price ? Looks like you've got semi-colons as column separators everywhere except between Country and Price - unless the WebKit/Firefox browsers are magically fixing that somehow I don't think it should work quite right as-is anyway. – CD001 Sep 07 '17 at 08:23
  • can i share code what i am having in php which is working fine in IE11 too .. but its little bit different from yours – Veshraj Joshi Sep 07 '17 at 08:29

2 Answers2

1

See this answer to a similar problem.

Try using those headers:

  header("Pragma: public");
  header("Expires: 0");
  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  header("Cache-Control: private",false);
  header("Content-Type: application/octet-stream");
  header("Content-Disposition: attachment; filename=\"exportevent.csv\";" );
  header("Content-Transfer-Encoding: binary"); 

I think that the octet-stream content type forces IE to download the file.

Bananaapple
  • 2,984
  • 2
  • 25
  • 38
0

This code is what working fine for me

 // fetching data from database 
 $products = Product::undeletedProducts();
 // heading in array
 $columns = array('Code','Place','Country','Price');
 header('Content-Type: text/csv; charset=utf-8');
 header('Content-Disposition: attachment;filenameproduct_list_'.time().'.csv');
    // file creation with write mode
    $output = fopen('php://output', 'w');
    // putting headings in csv file
    fputcsv($output, $columns);
    foreach($products as $product)
    {
        $outRow = [];
        // for dynamic columns
        foreach($columns as $column)
        {
          array_push($outRow,$user->{$column});
        }
        fputcsv($output, $outRow);
    }
Veshraj Joshi
  • 3,544
  • 3
  • 27
  • 45
  • this is perfectly working for me.. let me know whether it works for you or not – Veshraj Joshi Sep 07 '17 at 08:37
  • Actually another part of my code create dynamically columns and values and it works good, only problem was with downloading and opening file using IE11. But however, thank you for your time! – JohnWayne Sep 07 '17 at 08:41
  • you can test it sir, I will be glad if this is helpful to you... and welcome sir – Veshraj Joshi Sep 07 '17 at 08:43