使用phpexcel类库导出excel文件,文件名为中文时,在chrome浏览器下导出正常,在IE内核浏览器下导出文件名显示乱码,解决办法就是通过iconv函数转换编码,从utf-8转为gb2312,具体代码如下:
publicfunctionexportExcel($expTitle,$expCellName,$expTableData){
$xlsTitle=iconv('utf-8','gb2312',$expTitle);//文件名称,防止中文乱码
$fileName=$expTitle.date('_YmdHis');//自定义导出文件的中文名称
$fileName=iconv('utf-8','gb2312',$fileName);//显示的导出文件名称,如果不加这句则中文文件名乱码
$cellNum=count($expCellName);
$dataNum=count($expTableData);
$objPHPExcel=newPHPExcel();
$cellName=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
$objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1',$expTitle.' Exporttime:'.date('Y-m-dH:i:s'));
for($i=0;$i<$cellNum;$i++){
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2',$expCellName[$i][1]);
}
//Miscellaneousglyphs,UTF-8
for($i=0;$i<$dataNum;$i++){
for($j=0;$j<$cellNum;$j++){
$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3),$expTableData[$i][$expCellName[$j][0]]);
}
}
ob_end_clean();//用来清除缓冲区防止导出的excel文件内容乱码
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
header("Content-Disposition:attachment;filename=$fileName.xls");
$objWriter=\PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');
$objWriter->save('php://output');
exit;
}