Wrong encode string when export the excel using PHP
Instead of a custom class, used PHPExcel and works perfectly
$this->load->library('PHPExcel'); $this->load->library('PHPExcel/IOFactory'); $objPHPExcel = new PHPExcel(); $objPHPExcel->getProperties()->setTitle("Trial Report"); // Assign cell values $objPHPExcel->setActiveSheetIndex(0); //Filter all keys, they'll be table headers $h = array(); foreach ($result as $row) { foreach ($row as $key => $val) { if (!in_array($key, $h)) { $h[] = $key; } } } //Writing the first header row foreach ($h as $key => $val) { $val = ucwords($val); $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($key, 1, $val); //first row is 1 } $pos = 0; foreach ($result as $r_key => $row) { foreach ($row as $col) { $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($pos, ($r_key + 2), $col); //skip the first row $pos++; } $pos = 0; } $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5'); header('Content-type: application/vnd.ms-excel'); header('Content-Disposition: attachment; filename="export.xls"'); $objWriter->save('php://output');
For Chinese Traditional (Big5)
header("Content-type: text/html; charset=big5");
few more :
Chinese Simplified (EUC) ##charset=EUC-CN Chinese Simplified (GB2312) ##charset=gb2312 Chinese Simplified (HZ) ##charset=hz-gb-2312 Chinese Simplified (Mac) ##charset=x-mac-chinesesimp Chinese Traditional (Big5) ##charset=big5 Chinese Traditional (CNS) ##charset=x-Chinese-CNS Chinese Traditional (Eten) ##charset=x-Chinese-Eten Chinese Traditional (Mac) ##charset=x-mac-chinesetrad ##charset=950