这篇文章主要介绍“php excel如设置表格编码”,在日常操作中,相信很多人在php excel如设置表格编码问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”php excel如设置表格编码”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
php excel设置表格编码的方法:首先下载PHPExcel;然后创建一个EXCEL;最后通过“function gbkToUtf8($gbk){...}”方法设置编码并解决乱码问题即可。
本文操作环境:Windows7 系统、PHP7.1版,DELL G3电脑
php excel怎么设置表格编码?
PHPExcel导出excel表格及中文乱码解决办法
这是我写来自己看的,会不断更新,能搜到的都是有缘人~
GitHub下载地址:https://github.com/PHPOffice/PHPExcel
<?phprequire_once './PHPExcel/Classes/PHPExcel.php';//创建一个EXCEL$objPHPExcel = new PHPExcel(); //设置当前的sheet$objPHPExcel->setActiveSheetIndex(0);//设置sheet的name$getActiveSheet = $objPHPExcel->getActiveSheet();$getActiveSheet->setTitle('Sheet名称');//设置单元格的值$getActiveSheet->setCellValue('A1', '姓名');$getActiveSheet->setCellValue('B1', '部门');$getActiveSheet->setCellValue('C1', '工资');//合并单元格$getActiveSheet->mergeCells('C1:D1');//横向合并 纵向同理//设置单元格填充颜色$getActiveSheet->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);$getActiveSheet->getStyle('A1')->getFill()->getStartColor()->setARGB('000099FF');//设置字体样式$getActiveSheet->setCellValue('A2', setMyFontStyle('张三'));$getActiveSheet->setCellValue('B2', setMyFontStyle('开发部'));$getActiveSheet->setCellValue('C2', setMyFontStyle('9999'));//设置文字水平居左(HORIZONTAL_LEFT,默认)、中(HORIZONTAL_CENTER)、右(HORIZONTAL_RIGHT)$getActiveSheet->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//设置文字垂直居中$getActiveSheet->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);//设置自适应宽度$letter = ['A', 'B', 'C'];for($z=0;$z<count($letter);$z++){$getActiveSheet->getColumnDimension($letter[$z])->setAutoSize(true);}//设置固定宽度$getActiveSheet->getColumnDimension('A')->setWidth(20);//清除缓冲区,不加这句会报错ob_end_clean();//输出header("Pragma: public");header("Expires: 0");header("Cache-Control:must-revalidate, post-check=0, pre-check=0");header("Content-Type:application/force-download");header("Content-Type:application/vnd.ms-execl");header("Content-Type:application/octet-stream");header("Content-Type:application/download");header('Content-Type:text/html;Charset=utf-8;');header('Content-Disposition:attachment;filename=表格.xlsx"');header("Content-Transfer-Encoding:binary");$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');$objWriter->save('php://output');exit;//设置字体样式function setMyFontStyle($text){$objRichText = new PHPExcel_RichText();$objRichText->createText("");$objPayable = $objRichText->createTextRun($text);$objPayable->getFont()->setBold(true);$objPayable->getFont()->setItalic(true);$objPayable->getFont()->setColor( new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_RED ) );return $objRichText;}//解决导出中文乱码,按需使用function gbkToUtf8($gbk){$utf8 = iconv('GB2312', 'UTF-8', $gbk);return $utf8;}//解决导出中文乱码,按需使用function utf8ToGbk($utf8){$gbk = iconv('UTF-8', 'GB2312', $utf8);return $gbk;}
到此,关于“php excel如设置表格编码”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!