Skip to content

Update xlsxwriter.class.php to avoid malformed files in PHP < 8.0.0 #345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions xlsxwriter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,12 @@ public function writeToFile($filename)
protected function initializeSheet($sheet_name, $col_widths=array(), $auto_filter=false, $freeze_rows=false, $freeze_columns=false )
{
//if already initialized
if ($this->current_sheet==$sheet_name || isset($this->sheets[$sheet_name]))
return;
if ($this->current_sheet==$sheet_name || isset($this->sheets[$sheet_name])) return;

if (PHP_MAJOR_VERSION < 8) {
$oldlocale = setlocale(LC_NUMERIC, 0);
setlocale(LC_NUMERIC, 'C');
}

$sheet_filename = $this->tempFilename();
$sheet_xmlname = 'sheet' . (count($this->sheets) + 1).".xml";
Expand Down Expand Up @@ -188,6 +192,8 @@ protected function initializeSheet($sheet_name, $col_widths=array(), $auto_filte
$sheet->file_writer->write( '<col collapsed="false" hidden="false" max="1024" min="'.($i+1).'" style="0" customWidth="false" width="11.5"/>');
$sheet->file_writer->write( '</cols>');
$sheet->file_writer->write( '<sheetData>');

if (PHP_MAJOR_VERSION < 8) setlocale(LC_NUMERIC, $oldlocale);
}

private function addCellStyle($number_format, $cell_style_string)
Expand Down Expand Up @@ -251,8 +257,12 @@ public function writeSheetHeader($sheet_name, array $header_types, $col_options

public function writeSheetRow($sheet_name, array $row, $row_options=null)
{
if (empty($sheet_name))
return;
if (empty($sheet_name)) return;

if (PHP_MAJOR_VERSION < 8) {
$oldlocale = setlocale(LC_NUMERIC, 0);
setlocale(LC_NUMERIC, 'C');
}

$this->initializeSheet($sheet_name);
$sheet = &$this->sheets[$sheet_name];
Expand Down Expand Up @@ -286,6 +296,8 @@ public function writeSheetRow($sheet_name, array $row, $row_options=null)
$sheet->file_writer->write('</row>');
$sheet->row_count++;
$this->current_sheet = $sheet_name;

if (PHP_MAJOR_VERSION < 8) setlocale(LC_NUMERIC, $oldlocale);
}

public function countSheetRows($sheet_name = '')
Expand Down Expand Up @@ -364,6 +376,11 @@ public function writeSheet(array $data, $sheet_name='', array $header_types=arra

protected function writeCell(XLSXWriter_BuffererWriter &$file, $row_number, $column_number, $value, $num_format_type, $cell_style_idx)
{
if (PHP_MAJOR_VERSION < 8) {
$oldlocale = setlocale(LC_NUMERIC, 0);
setlocale(LC_NUMERIC, 'C');
}

$cell_name = self::xlsCell($row_number, $column_number);

if (!is_scalar($value) || $value==='') { //objects, array, empty
Expand All @@ -385,6 +402,8 @@ protected function writeCell(XLSXWriter_BuffererWriter &$file, $row_number, $col
$file->write('<c r="'.$cell_name.'" s="'.$cell_style_idx.'" t="inlineStr"><is><t>'.self::xmlspecialchars($value).'</t></is></c>');
}
}

if (PHP_MAJOR_VERSION < 8) setlocale(LC_NUMERIC, $oldlocale);
}

protected function styleFontIndexes()
Expand Down