首頁 >後端開發 >PHP問題 >php excel怎麼設定表格編碼

php excel怎麼設定表格編碼

藏色散人
藏色散人原創
2021-07-15 09:11:462510瀏覽

php excel設定表格編碼的方法:先下載PHPExcel;然後建立一個EXCEL;最後透過「function gbkToUtf8($gbk){...}」方法設定編碼並解決亂碼問題即可。

php excel怎麼設定表格編碼

本文操作環境:Windows7 系統、PHP7.1版,DELL G3電腦

php excel怎麼設定表格編碼?

PHPExcel匯出excel表格及中文亂碼解決方法

這是我寫來自己看的,會不斷更新,能搜到的都是有緣人~

GitHub下載網址:https://github.com/PHPOffice/PHPExcel

<?php
require_once &#39;./PHPExcel/Classes/PHPExcel.php&#39;;
//创建一个EXCEL
$objPHPExcel = new PHPExcel(); 
//设置当前的sheet
$objPHPExcel->setActiveSheetIndex(0);
//设置sheet的name
$getActiveSheet = $objPHPExcel->getActiveSheet();
$getActiveSheet->setTitle(&#39;Sheet名称&#39;);
//设置单元格的值
$getActiveSheet->setCellValue(&#39;A1&#39;, &#39;姓名&#39;);
$getActiveSheet->setCellValue(&#39;B1&#39;, &#39;部门&#39;);
$getActiveSheet->setCellValue(&#39;C1&#39;, &#39;工资&#39;);
//合并单元格
$getActiveSheet->mergeCells(&#39;C1:D1&#39;);//横向合并 纵向同理
//设置单元格填充颜色
$getActiveSheet->getStyle(&#39;A1&#39;)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$getActiveSheet->getStyle(&#39;A1&#39;)->getFill()->getStartColor()->setARGB(&#39;000099FF&#39;);
//设置字体样式
$getActiveSheet->setCellValue(&#39;A2&#39;, setMyFontStyle(&#39;张三&#39;));
$getActiveSheet->setCellValue(&#39;B2&#39;, setMyFontStyle(&#39;开发部&#39;));
$getActiveSheet->setCellValue(&#39;C2&#39;, setMyFontStyle(&#39;9999&#39;));
//设置文字水平居左(HORIZONTAL_LEFT,默认)、中(HORIZONTAL_CENTER)、右(HORIZONTAL_RIGHT)
$getActiveSheet->getStyle(&#39;A1&#39;)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//设置文字垂直居中
$getActiveSheet->getStyle(&#39;A1&#39;)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
//设置自适应宽度
$letter = [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;];
for($z=0;$z<count($letter);$z++){
$getActiveSheet->getColumnDimension($letter[$z])->setAutoSize(true);
}
//设置固定宽度
$getActiveSheet->getColumnDimension(&#39;A&#39;)->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(&#39;Content-Type:text/html;Charset=utf-8;&#39;);
header(&#39;Content-Disposition:attachment;filename=表格.xlsx"&#39;);
header("Content-Transfer-Encoding:binary");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, &#39;Excel2007&#39;);
$objWriter->save(&#39;php://output&#39;);
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(&#39;GB2312&#39;, &#39;UTF-8&#39;, $gbk);
return $utf8;
}
//解决导出中文乱码,按需使用
function utf8ToGbk($utf8){
$gbk = iconv(&#39;UTF-8&#39;, &#39;GB2312&#39;, $utf8);
return $gbk;
}

推薦學習:《PHP影片教學

以上是php excel怎麼設定表格編碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn