2

I have a API,which returns csv Content as response, i wanted to download at as Excel, What i tried:

HTML:

<form id="test_form" accept-charset="utf-8" method="POST" action="http://prime.dev/index.php//data/reporting/downloadCSVFile.json"></form>

<a id="downloadcsv" class="min-button-silver">Download to Excel</a>

JS:

jQuery("#downloadcsv").click(function(e){                       jQuery('#test_form').submit();                  
});

PHP

public function downloadCSVFile()
{
    $data = $this->reporting_service->fetchCSV($request); //API request
    header("Content-Type: application/csv");        
    header("Content-Disposition: attachment; filename=test.csv");
    //header("Content-Length: ".$thesize);
    header("Pragma: no-cache");
    header("Expires: 0");
    //Will get the response from API like the below;    
    $data = 'clicks|conversions|ctr|cvr|impressions|invalid_clicks|currency.id
    4220894|127984|.1962348497109191647|.0059484688672145487|2150940063|557193|163';

    echo $data;
}

File is downlaoding as test.csv, but opens in notepad, Can any one of you, please tell me, what is going wrong here.

Thanks and regards in advance

Manojkumar
  • 1,313
  • 4
  • 34
  • 59
  • Excel !== CSV 9regardless of whether the separator is a pipe or a comma. Look at any of the myriad libraries for PHP that can write __actual__ Excel files if you've been told to use xls - http://stackoverflow.com/questions/3930975/alternative-for-php-excel – Mark Baker Nov 22 '12 at 09:23
  • 2
    If you want to use CSV instead, the correct mime type is 'text/csv', not 'application/csv' – Mark Baker Nov 22 '12 at 09:25
  • hey Mark Baker, thanks for correcting. – Manojkumar Nov 23 '12 at 05:46

1 Answers1

0

If you want a download as Excel you need extra software, something like PEAR_Spreadsheet_Excel, because you have to generate extra headers and many other crap! But normally if you create a correct CSV-File you can also open it with Excel and it will show all columns seperated like in a normal excel file.