Files
oleibman 69b65d4e2a Xlsx Writer Eliminate xml:space From Non-Text Nodes
Fix #4542. PhpSpreadsheet has been writing attribute `xml:space="preserve"` to the `table` tag when writing a Table. According to the issue, Excel 2016 is treating the resulting file as corrupt. I do not have access to a version of Excel 2016 to confirm. This seems to be a bug with that release. Nevertheless, the OOXML spec, with over 100 references to `xml:space` does not indicate that it is a permitted attribute for `table`. It should only be specified for text nodes. This PR eliminates the undocumented, and unneeded, usage.

Investigating further, PhpSpreadsheet also writes this attribute for `workbook`, `styleSheet`, and `worksheet` tags. It is again undocumented and unneeded in those cases. Although all Excel releases, including 2016, apparently tolerate such usage, this PR also eliminates those.

Finally, there is one case where PhpSpreadsheet omits this tag when it is needed. When writing a cell whose data type is an inline string, and the string contains leading or trailing whitespace, the text tag needs to specify `xml:space`, and is now changed to do so.
2025-07-26 00:08:33 -07:00

110 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Table;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx as XlsxWriter;
use PHPUnit\Framework\TestCase;
class Issue4542Test extends TestCase
{
public function testXmlSpace(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$string = ' Ye&ar ';
$trimString = trim($string);
$sheet->getCell('A1')->setValue($string);
$sheet->getCell('A2')->setValueExplicit($string, DataType::TYPE_INLINE);
$sheet->getCell('B1')->setValue($trimString);
$sheet->getCell('B2')->setValueExplicit($trimString, DataType::TYPE_INLINE);
$writer = new XlsxWriter($spreadsheet);
$writer->createStyleDictionaries();
$writerStyle = new XlsxWriter\Style($writer);
$data = $writerStyle->writeStyles($spreadsheet);
self::assertStringContainsString(
'<styleSheet',
$data
);
self::assertStringNotContainsString(
'xml:space',
$data
);
$writerWorkbook = new XlsxWriter\Workbook($writer);
$data = $writerWorkbook->writeWorkbook($spreadsheet);
self::assertStringContainsString(
'<workbook',
$data
);
self::assertStringNotContainsString(
'xml:space',
$data
);
$stringTable = $writer->createStringTable();
$writerStringTable = new XlsxWriter\StringTable($writer);
$data = $writerStringTable->writeStringTable($stringTable);
self::assertStringContainsString(
'<si><t xml:space="preserve"> Ye&amp;ar </t></si>',
$data
);
self::assertStringContainsString(
'<si><t>Ye&amp;ar</t></si>',
$data
);
$writerWorksheet = new XlsxWriter\Worksheet($writer);
$data = $writerWorksheet->writeWorksheet($sheet, []);
self::assertStringContainsString(
'<c r="A2" t="inlineStr"><is><t xml:space="preserve"> Ye&amp;ar </t></is></c>',
$data
);
self::assertStringContainsString(
'<c r="B2" t="inlineStr"><is><t>Ye&amp;ar</t></is></c>',
$data
);
$spreadsheet->disconnectWorksheets();
}
public function testTable(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->fromArray(
[
['MyCol', 'Colonne2', 'Colonne3'],
[10, 20],
[2],
[3],
[4],
],
null,
'B1',
true
);
$table = new Table('B1:D5', 'Tableau1');
$sheet->addTable($table);
$writer = new XlsxWriter($spreadsheet);
$writerTable = new XlsxWriter\Table($writer);
$data = $writerTable->writeTable($table, 1);
self::assertStringContainsString(
'<table ',
$data
);
self::assertStringNotContainsString(
'xml:space',
$data
);
$spreadsheet->disconnectWorksheets();
}
}