Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP to xls file and UTF-8 encoding

Tags:

php

header

xls

$filename = "file.xls";

header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");

$content = "asd";
echo $content;

If $content contains some unicode symbols, in excel file, result is something like this : áƒáƒ¡áƒ“

How to solve this? How to set this php file UTF8 encoding ?

I am trying: header("Content-Type: application/vnd.ms-excel; charset=utf-8"); but not works

like image 999
Oto Shavadze Avatar asked Aug 23 '26 01:08

Oto Shavadze


1 Answers

If you are generating file by simply echoing strings, you should create it as xml then doing an echo you need to add encoding to file.

$content = '<?xml version="1.0" encoding="UTF-8"?>'

You need to create your file in xml as its expected.
Ex:
<?xml version="1.0" encoding="UTF-8"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet>
<Table>
<Row>
<Cell><Data>$content</Data></Cell>
</Row>
</Table>
</Worksheet>
</Workbook>

like image 184
PoX Avatar answered Aug 25 '26 17:08

PoX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!