0

hi i am using PHP to save some data in xls format, and i need to use UTF-8 because some of the characters that the user input could be ÆØÅ ive tried several solutions there were written on the forums, but without any luck.

originally i used this code to save my string :

myfile = fopen("$filnavn$dato.xls","w") or die ("Fejl fil ikke gemt");

fwrite($myfile, $txt);
fclose($myfile);

but learned that it was better to use

function WriteStringToFile($myfile,$txt){
    $f=fopen($myfile);
    $myfile= "\xEF\xBB\xBF".$myfile;
    fputs($f,$txt);
    fclose($f);
}

and yet this didnt work either. I have tried doing the

header('Content-type : text/plain; charset=utf-8');

but that doesnt seem to help either. Does anybody have any clue why my code wont work ? :)

Qirel
  • 25,449
  • 7
  • 45
  • 62
  • 1
    None of this will save a file that is in any spreadsheet file format; you're simply creating a plain text file – Mark Baker Dec 15 '16 at 13:25
  • it does save it as a xls file, which is all i need at the moment, i just want to understand why the UTF-8 charset isnt applied, and i know this isnt the proper code to save as excel files, but it suits the purpose for now, all i need is to figure out how the UTF-8 charset can be applied. – Phillip fyhn Dec 15 '16 at 13:27
  • Are you actually writing an ***Excel*** file, or a ***CSV*** file dressed up in a file with a .xls extension?! – deceze Dec 15 '16 at 13:28
  • no its basicly a txt file, moved into xls simply because it looks better that way, but eitherway the UTF charset wont be applied in notepad either. – Phillip fyhn Dec 15 '16 at 13:30
  • Lesson 1: Excel notoriously sucks at dealing with plaintext/CSV encodings, even more so if you try to sell your file using the wrong extension. Look at the duplicate for a plaintext solution, or actually use a library to produce an actual .xls file. – deceze Dec 15 '16 at 13:31
  • It saves it as a file with a `.xls` extension.... but unless you're using PHP's magic pixie dust module, then it won't automagically be converted to an Excel file; it remains purely a text file – Mark Baker Dec 15 '16 at 13:31
  • @Mark That module gave me a hell of a time compiling it the last time I tried… – deceze Dec 15 '16 at 13:32
  • @deceze - perhaps you didn't sacrifice enough pikachus to the dark lord Dmitry of PHP; and they do need to be freshly caught at midnight under a fool moon – Mark Baker Dec 15 '16 at 13:34

0 Answers0