Skip to content

Commit df87725

Browse files
committed
to avoid cell value becoming '1900/01/01', Set non-date data as a string cell instead of a date type cell.
1 parent 342a04a commit df87725

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Diff for: xlsxwriter.class.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,19 @@ protected function writeCell(XLSXWriter_BuffererWriter &$file, $row_number, $col
365365
} elseif (is_string($value) && $value{0}=='='){
366366
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="s"><f>'.self::xmlspecialchars($value).'</f></c>');
367367
} elseif ($num_format_type=='n_date') {
368-
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="n"><v>'.intval(self::convert_date_time($value)).'</v></c>');
368+
$dateValue = self::convert_date_time($value);
369+
if ($dateValue > 0 || preg_match("/(\d+):(\d{2}):(\d{2})/", $value)) {
370+
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="n"><v>'.intval($dateValue).'</v></c>');
371+
} else { //not date, treat it as string
372+
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="inlineStr"><is><t>'.self::xmlspecialchars($value).'</t></is></c>');
373+
}
369374
} elseif ($num_format_type=='n_datetime') {
370-
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="n"><v>'.self::convert_date_time($value).'</v></c>');
375+
$dateValue = self::convert_date_time($value);
376+
if ($dateValue > 0 || preg_match("/(\d+):(\d{2}):(\d{2})/", $value)) {
377+
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="n"><v>'.$dateValue.'</v></c>');
378+
} else { //not datetime, treat it as string
379+
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="inlineStr"><is><t>'.self::xmlspecialchars($value).'</t></is></c>');
380+
}
371381
} elseif ($num_format_type=='n_numeric') {
372382
if (!is_string($value) || $value=='0' || ($value[0]!='0' && ctype_digit($value)) || preg_match("/^\-?(0|[1-9][0-9]*)?(\.[0-9]+)?$/", $value)){
373383
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="n"><v>'.self::xmlspecialchars($value).'</v></c>');//int,float,currency

0 commit comments

Comments
 (0)