1

My export script:

if($_POST){
    $file = 'FKDB_XLS';
    $filename = $file."_".date("Y-m-d_H-i",time()); 
    header("Content-type: application/vnd.ms-excel; charset=utf-8"); // dunno if the charset does anything for this application type..
    header("Content-Disposition: attachment; filename=$filename");
    echo stripslashes($_SESSION['item']); unset($_SESSION['item']);
    exit;
}

Basicly, it just turns HTML ($_SESSION['item'] as <table>{rows}</table>) into *.xls and allows Excel to draw tables from that data, as most of you know, probably.

The problem is, when opened in Excel, everything's OK, except... all the values are written incorrectly, with all the Ä, ņŠand so on instead of 'Ā' or so, again.

What should I do to prevent that?

Thanks in advance!

tomsseisums
  • 13,168
  • 19
  • 83
  • 145

2 Answers2

4

If you're writing HTML markup for import into excel, then you need to ensure that you're setting the appropriate meta tags in the HTML header. specifying charset in the Content type will have absolutely no effect at all. If you're simply writing a table, then wrap it in HTML/HEAD/BODY, etc so that you can set the HEAD meta tags.

I still don't understand why people write HTML and give the file an xls extension when it's so easy to create a real Excel file!

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • 1
    Mind sharing info on "easy to create a real Excel file"? – tomsseisums Apr 10 '11 at 21:55
  • @Tom - What I mean is that there's a whole host of PHP libraries to create a genuine BIFF .xls file or Office Open XML .xlsx file without simply writing a csv or html file and giving it an extension of .xls. In addition to PHPExcel (http://www.phpexcel.net), which is my own library, there's other options listed in the answer to this SO question http://stackoverflow.com/questions/3930975/alternative-for-php-excel – Mark Baker Apr 10 '11 at 22:04
  • Thanks, I use to create xls files using \t and \n, but I had a lot problems with encoding, with tables is a lot more easer and work much better. Thanks again Mark. – Lucas Serafim Sep 02 '13 at 16:57
3

Add this to your HTML table, so Excel will read it on the fly:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
PadraigD
  • 641
  • 5
  • 13