diff --git a/CHANGE.md b/CHANGE.md index 78233aa..b607e27 100755 --- a/CHANGE.md +++ b/CHANGE.md @@ -5,6 +5,7 @@ Change Log: `yii2-export` **Date:** _under development_ +- (enh #272): UTF-8 encoding for HTML, CSV, TEXT formats. - (enh #271): Locale specific validation messages and code enhancements. - (enh #270): Add iframe as default target for export form download. - (enh #248): Add direct download, supplement sheets, data validation features. diff --git a/src/ExportMenu.php b/src/ExportMenu.php index 3c9dda9..37ea5c1 100644 --- a/src/ExportMenu.php +++ b/src/ExportMenu.php @@ -57,6 +57,10 @@ class ExportMenu extends GridView { use TranslationTrait; + /** + * @var string UTF-8 encoding + */ + const ENCODING_UTF8 = 'utf-8'; /** * @var string HTML (Hyper Text Markup Language) export format */ @@ -373,9 +377,9 @@ class ExportMenu extends GridView public $autoWidth = true; /** - * @var string encoding for the downloaded file header. Defaults to 'utf-8'. + * @var string encoding for the downloaded file header. Defaults to [[ENCODING_UTF8]]. */ - public $encoding = 'utf-8'; + public $encoding = self::ENCODING_UTF8; /** * @var string the exported output file name. Defaults to 'grid-export'; @@ -1117,10 +1121,15 @@ public function initPhpSpreadsheetWriter($type) * @var WriterCsv $writer */ $writer = $this->_objWriter = IOFactory::createWriter($this->_objSpreadsheet, $type); - if ($this->_exportType === self::FORMAT_TEXT) { + $t = $this->_exportType; + if ($t === self::FORMAT_TEXT) { $delimiter = $this->getSetting('delimiter', "\t"); $writer->setDelimiter($delimiter); } + $needsEncoding = $t === self::FORMAT_HTML || $t === self::FORMAT_CSV || $t === self::FORMAT_TEXT; + if ($this->encoding === self::ENCODING_UTF8 && $needsEncoding) { + $writer->setUseBOM(true); + } $this->raiseEvent('onInitWriter', [$this->_objWriter, $this]); }