From 164dbded55171ad15257e7ce0d06e51131cb43e3 Mon Sep 17 00:00:00 2001 From: hnx8 Date: Mon, 2 Jul 2018 16:59:54 +0900 Subject: [PATCH 1/3] numeric format '#','?' accepts, '$' fixes. --- xlsxwriter.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xlsxwriter.class.php b/xlsxwriter.class.php index a987735f8..81bc7589d 100644 --- a/xlsxwriter.class.php +++ b/xlsxwriter.class.php @@ -778,9 +778,9 @@ private static function determineNumberFormatType($num_format) if (preg_match("/[Y]{2,4}/i", $num_format)) return 'n_date'; if (preg_match("/[D]{1,2}/i", $num_format)) return 'n_date'; if (preg_match("/[M]{1,2}/i", $num_format)) return 'n_date'; - if (preg_match("/$/", $num_format)) return 'n_numeric'; - if (preg_match("/%/", $num_format)) return 'n_numeric'; - if (preg_match("/0/", $num_format)) return 'n_numeric'; + if (preg_match('/\$/', $num_format)) return 'n_numeric'; + if (preg_match('/%/', $num_format)) return 'n_numeric'; + if (preg_match('/[0#?]/', $num_format)) return 'n_numeric'; return 'n_auto'; } //------------------------------------------------------------------ From 342a04a0318b9b8c825490ab249fa35ca3b14eed Mon Sep 17 00:00:00 2001 From: hnx8 Date: Mon, 2 Jul 2018 17:22:08 +0900 Subject: [PATCH 2/3] to prevent excel file corruption, Set non-numeric data as a string cell instead of a numeric type cell. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Note: if not, bad numeric type cell causes "(filename.xlsx)の一部の内容に問題が見つかりました。可能な限り内容を回復しますか?" in the Japanese version of Excel. (English version perhaps "Excel found unreadable contents in (filename.xlsx). Do you want to recover the contents of this workbook?") --- xlsxwriter.class.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/xlsxwriter.class.php b/xlsxwriter.class.php index 81bc7589d..cc8c15112 100644 --- a/xlsxwriter.class.php +++ b/xlsxwriter.class.php @@ -369,7 +369,11 @@ protected function writeCell(XLSXWriter_BuffererWriter &$file, $row_number, $col } elseif ($num_format_type=='n_datetime') { $file->write(''.self::convert_date_time($value).''); } elseif ($num_format_type=='n_numeric') { - $file->write(''.self::xmlspecialchars($value).'');//int,float,currency + if (!is_string($value) || $value=='0' || ($value[0]!='0' && ctype_digit($value)) || preg_match("/^\-?(0|[1-9][0-9]*)?(\.[0-9]+)?$/", $value)){ + $file->write(''.self::xmlspecialchars($value).'');//int,float,currency + } else { //not numeric, treat it as string + $file->write(''.self::xmlspecialchars($value).''); + } } elseif ($num_format_type=='n_string') { $file->write(''.self::xmlspecialchars($value).''); } elseif ($num_format_type=='n_auto' || 1) { //auto-detect unknown column types From df877250d4c206aa295bd85fdf689e2f997263b8 Mon Sep 17 00:00:00 2001 From: hnx8 Date: Tue, 3 Jul 2018 18:04:50 +0900 Subject: [PATCH 3/3] to avoid cell value becoming '1900/01/01', Set non-date data as a string cell instead of a date type cell. --- xlsxwriter.class.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/xlsxwriter.class.php b/xlsxwriter.class.php index cc8c15112..76868cf00 100644 --- a/xlsxwriter.class.php +++ b/xlsxwriter.class.php @@ -365,9 +365,19 @@ protected function writeCell(XLSXWriter_BuffererWriter &$file, $row_number, $col } elseif (is_string($value) && $value{0}=='='){ $file->write(''.self::xmlspecialchars($value).''); } elseif ($num_format_type=='n_date') { - $file->write(''.intval(self::convert_date_time($value)).''); + $dateValue = self::convert_date_time($value); + if ($dateValue > 0 || preg_match("/(\d+):(\d{2}):(\d{2})/", $value)) { + $file->write(''.intval($dateValue).''); + } else { //not date, treat it as string + $file->write(''.self::xmlspecialchars($value).''); + } } elseif ($num_format_type=='n_datetime') { - $file->write(''.self::convert_date_time($value).''); + $dateValue = self::convert_date_time($value); + if ($dateValue > 0 || preg_match("/(\d+):(\d{2}):(\d{2})/", $value)) { + $file->write(''.$dateValue.''); + } else { //not datetime, treat it as string + $file->write(''.self::xmlspecialchars($value).''); + } } elseif ($num_format_type=='n_numeric') { if (!is_string($value) || $value=='0' || ($value[0]!='0' && ctype_digit($value)) || preg_match("/^\-?(0|[1-9][0-9]*)?(\.[0-9]+)?$/", $value)){ $file->write(''.self::xmlspecialchars($value).'');//int,float,currency