简体   繁体   中英

æøå in csv export

I'm trying to export some data into an CSV file i can download.

if im hitting my API in postman the data looks fine like:

Notat,Vare,Type,Størrelse,Tekst,Antal,Pris,Rabatprocent,Totalbeløb,Fakturerbar,"Lever nu",Faktureret,Lagersted,Placering,Lagerstyring
,,,,"Fadol 0,5 - 6 stk. ",4,,"0,00",1000,Checked,0,"0,00",,,Intet
,,,,"Classic 0,5",34,,"0,00",1700,Checked,0,"0,00",,,Intet
,,,,"Classic 0,5 - 6 stk ",3,,"0,00",750,Checked,0,"0,00",,,Intet
,,,,"Grimbergen 0,3",6,,"0,00",240,Checked,0,"0,00",,,Intet
,,,,"Nordic flaske ",0,,"0,00",0,Checked,0,"0,00",,,Intet
,,,,Kildevand,109,,"0,00",2160,Checked,0,"0,00",,,Intet
,,,,"Smirnoff 6 stk",0,,"0,00",0,Checked,0,"0,00",,,Intet
,,,,"Sæson 0,3",2,,"0,00",80,Checked,0,"0,00",,,Intet
,,,,"Sodavand 0,3",5,,"0,00",150,Checked,0,"0,00",,,Intet
,,,,"Sodavand 0,5",102,,"0,00",4040,Checked,0,"0,00",,,Intet
,,,,"Fadøl 0,5",20,,"0,00",1000,Checked,0,"0,00",,,Intet
,,,,"Kop varme drikke",17,,"0,00",255,Checked,0,"0,00",,,Intet

But when i open the CSV file after downloading it will show Størrelse as Størrelse. Så it works fine in postman but not in the actual CSV file. someone can point me in the right direction for where my problem is?

Here is my code that creates the export:

//Open file pointer.        
        $fp = fopen('php://output', 'w');

        //Loop through the associative array.
        foreach($sortedData as $row){
            //Write the row to the CSV file.

            fputcsv($fp, $row);
        }

        //Finally, close the file pointer.
        fclose($fp);

        $response = $response->withHeader('Content-Type', 'text/csv; charset=utf-8');
        $response = $response->withHeader('Content-Disposition', 'attachment; filename="file.csv"');

        $stream = fopen('php://memory', 'r+');      

        return $response->withBody(new \Slim\Http\Stream($stream));

depends on encoding type. I got similar issue with csv file.I converted the row to "ISO-8859-1" ( for german characters) .

 iconv(mb_detect_encoding($text), "ISO-8859-1", $text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM