Skip to content

Commit 342a04a

Browse files
committed
to prevent excel file corruption, Set non-numeric data as a string cell instead of a numeric type cell.
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?")
1 parent 164dbde commit 342a04a

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

xlsxwriter.class.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,11 @@ protected function writeCell(XLSXWriter_BuffererWriter &$file, $row_number, $col
369369
} elseif ($num_format_type=='n_datetime') {
370370
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="n"><v>'.self::convert_date_time($value).'</v></c>');
371371
} elseif ($num_format_type=='n_numeric') {
372-
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="n"><v>'.self::xmlspecialchars($value).'</v></c>');//int,float,currency
372+
if (!is_string($value) || $value=='0' || ($value[0]!='0' && ctype_digit($value)) || preg_match("/^\-?(0|[1-9][0-9]*)?(\.[0-9]+)?$/", $value)){
373+
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="n"><v>'.self::xmlspecialchars($value).'</v></c>');//int,float,currency
374+
} else { //not numeric, treat it as string
375+
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="inlineStr"><is><t>'.self::xmlspecialchars($value).'</t></is></c>');
376+
}
373377
} elseif ($num_format_type=='n_string') {
374378
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="inlineStr"><is><t>'.self::xmlspecialchars($value).'</t></is></c>');
375379
} elseif ($num_format_type=='n_auto' || 1) { //auto-detect unknown column types

0 commit comments

Comments
 (0)