From bef2b4647c18ed661e77eb136f6902b8b2e5c643 Mon Sep 17 00:00:00 2001 From: Chris Graham Date: Mon, 30 Mar 2015 22:07:37 +0100 Subject: [PATCH] Fix charset and number format issues --- src/Spout/Writer/Internal/XLSX/Worksheet.php | 2 +- src/Spout/Writer/XLS.php | 22 +++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Spout/Writer/Internal/XLSX/Worksheet.php b/src/Spout/Writer/Internal/XLSX/Worksheet.php index d49f065..7721114 100644 --- a/src/Spout/Writer/Internal/XLSX/Worksheet.php +++ b/src/Spout/Writer/Internal/XLSX/Worksheet.php @@ -149,7 +149,7 @@ EOD; if (empty($cellValue)) { $data .= '/>' . PHP_EOL; } else { - if (trim($cellValue, '0123456789.') == '' /*similar to is_numeric without having PHPs regular quirkiness*/) { + if (trim($cellValue, '-0123456789.') == '' /*similar to is_numeric without having PHPs regular quirkiness*/) { $data .= '>' . $cellValue . '' . PHP_EOL; } else { if ($this->shouldUseInlineStrings) { diff --git a/src/Spout/Writer/XLS.php b/src/Spout/Writer/XLS.php index cf91d2c..2278783 100644 --- a/src/Spout/Writer/XLS.php +++ b/src/Spout/Writer/XLS.php @@ -115,6 +115,26 @@ class XLS extends AbstractWriter $value = substr($value, 0, 255); } + // We are doing simple Western European encoding (utf-8 storage in BIFF-5 is very complex) + if (function_exists('utf8_decode')) { + $test = @utf8_decode($value); + if (is_string($test)) { + $value = $test; + } + } + elseif (function_exists('iconv')) { + $test = @iconv('utf-8', 'iso-8859-1' . '//TRANSLIT', $value); + if (is_string($test)) { + $value = $test; + } + } + elseif (function_exists('mb_convert_encoding')) { + $test = @mb_convert_encoding($value, 'iso-8859-1', 'utf-8'); + if (is_string($test)) { + $value = $test; + } + } + $length = strlen($value); $wasWriteSuccessful = true; $wasWriteSuccessful = $wasWriteSuccessful && fwrite($this->filePointer, pack("ssssss", 0x204, 8 + $length, $this->row, $this->col, 0x0, $length)); @@ -144,7 +164,7 @@ class XLS extends AbstractWriter $this->home(); foreach ($dataRow as $cell) { - if (trim($cell, '0123456789.') == '' /*similar to is_numeric without having PHPs regular quirkiness*/) { + if ($cell != '' && trim($cell, '-0123456789.') == '' /*similar to is_numeric without having PHPs regular quirkiness*/) { $wasWriteSuccessful = $wasWriteSuccessful && $this->number($cell); } else {