0

I made a CSV as an example:

Type, जिल्ला शिक्षा कार्यालय डडेलधुरा,NGOI,NGO Request,21,0,0 Response, 4,0,0

and it has utf-8 characters and i want to export it to excel so i posted it and to export it to excel and after referring to several questions on stackoverflow i did as follow:

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Type: text/csv; charset=utf-8");
header('Content-Disposition: attachment; filename=file.csv');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
print $_POST['csv_data'];exit;

the csv is exported to excel nicely but character encoding in not converted and shows something like this: जिलà¥à¤²à¤¾ शिकà¥à¤·à¤¾ कारà¥à¤¯à¤¾à¤²à¤¯ डडेलधà¥à¤°à¤¾ and when i add

echo "\xEF\xBB\xBF";

the character encoding is converted but the data appears in one column like this:

Type, जिल्ला शिक्षा कार्यालय डडेलधुरा,NGOI,NGO
Request,21,0,0
Response, 4,0,0
3
  • Use PHPExcel! Self-made solutions are definitly not useful. Commented Dec 20, 2013 at 12:25
  • You can try to add utf8_encode() to the $_POST['csv_data'] Commented Dec 20, 2013 at 12:26
  • @JackPoint the result comes like this जिलà¥Âला शिकà¥Âषा कारà¥Âयालय डडेलधà¥Âरा Commented Dec 20, 2013 at 12:28

1 Answer 1

1

UTF8 + CSV + Excel is known to be a problematic combination. Excel just isn't very good at importing CSV.

One thing you should try is to add a UTF8 BOM character at the start of your CSV file; that might help Excel work out what to do with it.

I'm optimistic that will help, but if it doesn't, you may have to re-think using CSV for this.

Other options you could use instead of CSV would include XML, and creating an actual Excel file from within PHP (there are a number of libraries available which can do this, the best one being PHPExcel).

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.