Skip to content

Commit

Permalink
to avoid cell value becoming '1900/01/01', Set non-date data as a str…
Browse files Browse the repository at this point in the history
…ing cell instead of a date type cell.
  • Loading branch information
hnx8 committed Jul 3, 2018
1 parent 342a04a commit df87725
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions xlsxwriter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,19 @@ protected function writeCell(XLSXWriter_BuffererWriter &$file, $row_number, $col
} elseif (is_string($value) && $value{0}=='='){
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="s"><f>'.self::xmlspecialchars($value).'</f></c>');
} elseif ($num_format_type=='n_date') {
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="n"><v>'.intval(self::convert_date_time($value)).'</v></c>');
$dateValue = self::convert_date_time($value);
if ($dateValue > 0 || preg_match("/(\d+):(\d{2}):(\d{2})/", $value)) {
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="n"><v>'.intval($dateValue).'</v></c>');
} else { //not date, treat it as string
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="inlineStr"><is><t>'.self::xmlspecialchars($value).'</t></is></c>');
}
} elseif ($num_format_type=='n_datetime') {
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="n"><v>'.self::convert_date_time($value).'</v></c>');
$dateValue = self::convert_date_time($value);
if ($dateValue > 0 || preg_match("/(\d+):(\d{2}):(\d{2})/", $value)) {
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="n"><v>'.$dateValue.'</v></c>');
} else { //not datetime, treat it as string
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="inlineStr"><is><t>'.self::xmlspecialchars($value).'</t></is></c>');
}
} 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('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="n"><v>'.self::xmlspecialchars($value).'</v></c>');//int,float,currency
Expand Down

0 comments on commit df87725

Please sign in to comment.