Home >Backend Development >PHP Tutorial >php excel 导出乱码有关问题
php excel 导出乱码问题
/**<br /> * 用于生成excel文件的函数<br /> * author:walker<br /> * @param $data 生成excel的数据(二维数组形式)<br /> * @param null $savefile 生成excel的文件名(保不指定,则为当前时间戳)<br /> * @param null $title 生成excel的表头(一维数组形式)<br /> * @param string $sheetname 生成excel的sheet名称(缺省为sheet1)<br /> */<br /> function exportExcel($data,$savefile=null,$title=null,$sheetname='sheet1'){<br /> //若没有指定文件名则为当前时间戳<br /> if(is_null($savefile)){<br /> $savefile=time();<br /> }<br /> //若指字了excel表头,则把表单追加到正文内容前面去<br /> if(is_array($title)){<br /> array_unshift($data,$title);<br /> }<br /> import('Org.Util.PHPExcel');<br /> import('Org.Util.PHPExcel.IOFactory');<br /> import('PHPExcel.Util.PHPExcel.Reader.Excel5');<br /> $objPHPExcel = new \PHPExcel();<br /> //Excel内容<br /> foreach($data as $k => $v){<br /> $obj=$objPHPExcel->setActiveSheetIndex(0);<br /> $row=$k+1;//行<br /> $nn=0;<br /> foreach($v as $vv){<br /> $col=chr(65+$nn);//列<br /> $vv = iconv("UTF-8", "GB2312//IGNORE",$vv);<br /> $obj->setCellValue($col.$row,$vv);//列,行,值<br /> $nn++;<br /> }<br /> }<br /> $objPHPExcel->getActiveSheet()->setTitle($sheetname);<br /> $objPHPExcel->setActiveSheetIndex(0);<br /> header('Content-Type: application/vnd.ms-excel');<br /> header('Content-Disposition: attachment;filename="'.$savefile.'.xls"');<br /> header("Content-Type: application/vnd.ms-excel; charset=UTF-8");<br /> header('Cache-Control: max-age=0');<br /> $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');<br /> $objWriter->save('php://output');<br /> exit;<br /> }<br /> <br /> /**<br /> *<br /> * 导出Excel<br /> */<br /> function expUser(){//导出Excel<br /> $xlstitle = array('id','商品编码','模板编号','商品名称','总库存','当前库存','供应商');<br /> $xlssql = M('erp_inventory');<br /> $xlsData = $xlssql->getField('id,p_code,mid,pname,banlance,cur_banlance,factory');<br />// show_bug($xlsData);die;<br /> $this->exportExcel($xlsData,time(),$xlstitle);<br /> <br /> }
include 'Plugin/PHPExcel/Classes/PHPExcel.php';<br>// import('Org.Util.PHPExcel.IOFactory');<br>// import('PHPExcel.Util.PHPExcel.Reader.Excel5');<div class="clear"> </div>