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视频教程》
以上就是php excel怎么设置表格编码的详细内容。