PHP excel導出亂碼問題解決辦法

在2003及其它版本導出一切正常,可是在excel2007上導出亂碼,處理方法:php

string format for excel to export. Valid value are 'Excel5','Excel2007','Excel2003XML','00Calc','Gnumeric'.

找到createWrite方法,將第二個參數修改爲須要生成的EXCEL版本型號:git

如:github

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');

固然了,若是用的是框架,如YII2框架,那麼建議封裝成一個小物件,推薦:yii2

https://github.com/moonlandsoft/yii2-phpexcel 框架

小物件使用方法:yii

use moonland\phpexcel\Excel;

public function actionT()
{
    $result = Test::find()->all();
    Excel::widget([
        'models' => $result,
        'mode' => 'export',
        'format' => 'Excel5',
        'columns' => ['itemid', 'username', 'company'],
        'headers' => ['itemid' => 'ID號', 'username' => '用戶名', 'company' => '公司'],
    ]);
}
相關文章
相關標籤/搜索