Skip to content

Commit

Permalink
to prevent excel file corruption, Set non-numeric data as a string ce…
Browse files Browse the repository at this point in the history
…ll 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?")
  • Loading branch information
hnx8 committed Jul 2, 2018
1 parent 164dbde commit 342a04a
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion xlsxwriter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,11 @@ protected function writeCell(XLSXWriter_BuffererWriter &$file, $row_number, $col
} 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>');
} elseif ($num_format_type=='n_numeric') {
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="n"><v>'.self::xmlspecialchars($value).'</v></c>');//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('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="n"><v>'.self::xmlspecialchars($value).'</v></c>');//int,float,currency
} else { //not numeric, 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_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_auto' || 1) { //auto-detect unknown column types
Expand Down

0 comments on commit 342a04a

Please sign in to comment.