Skip to content
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

Reading Xlsx With No or Empty Styles #2272

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
22 changes: 16 additions & 6 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,8 @@ public function load(string $pFilename, int $flags = 0): Spreadsheet
// Initialisations
$excel = new Spreadsheet();
$excel->removeSheetByIndex(0);
if (!$this->readDataOnly) {
$excel->removeCellStyleXfByIndex(0); // remove the default style
$excel->removeCellXfByIndex(0); // remove the default style
}
$addingFirstCellStyleXf = true;
$addingFirstCellXf = true;
$unparsedLoadedData = [];

$this->zip = $zip = new ZipArchive();
Expand Down Expand Up @@ -534,8 +532,12 @@ public function load(string $pFilename, int $flags = 0): Spreadsheet
. "$xmlNamespaceBase/styles"
. "']";
$xpath = self::getArrayItem(self::xpathNoFalse($relsWorkbook, $relType));
// I think Nonamespace is okay because I'm using xpath.
$xmlStyles = $this->loadZipNonamespace("$dir/$xpath[Target]", $mainNS);
if ($xpath === null) {
$xmlStyles = self::testSimpleXml(null);
} else {
// I think Nonamespace is okay because I'm using xpath.
$xmlStyles = $this->loadZipNonamespace("$dir/$xpath[Target]", $mainNS);
}
$xmlStyles->registerXPathNamespace('smm', Namespaces::MAIN);
$fills = self::xpathNoFalse($xmlStyles, 'smm:fills/smm:fill');
$fonts = self::xpathNoFalse($xmlStyles, 'smm:fonts/smm:font');
Expand Down Expand Up @@ -593,6 +595,10 @@ public function load(string $pFilename, int $flags = 0): Spreadsheet
// add style to cellXf collection
$objStyle = new Style();
self::readStyle($objStyle, $style);
if ($addingFirstCellXf) {
$excel->removeCellXfByIndex(0); // remove the default style
$addingFirstCellXf = false;
}
$excel->addCellXf($objStyle);
}

Expand Down Expand Up @@ -624,6 +630,10 @@ public function load(string $pFilename, int $flags = 0): Spreadsheet
// add style to cellStyleXf collection
$objStyle = new Style();
self::readStyle($objStyle, $cellStyle);
if ($addingFirstCellStyleXf) {
$excel->removeCellStyleXfByIndex(0); // remove the default style
$addingFirstCellStyleXf = false;
}
$excel->addCellStyleXf($objStyle);
}
}
Expand Down
68 changes: 64 additions & 4 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/XlsxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
use PhpOffice\PhpSpreadsheet\Document\Properties;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Style\Conditional;
Expand Down Expand Up @@ -37,6 +37,7 @@ public function testLoadXlsxRowColumnAttributes(): void
}

self::assertFalse($worksheet->getColumnDimension('E')->getVisible());
$spreadsheet->disconnectWorksheets();
}

public function testLoadXlsxWithStyles(): void
Expand All @@ -61,6 +62,54 @@ public function testLoadXlsxWithStyles(): void
);
}
}
$spreadsheet->disconnectWorksheets();
}

/**
* Test load Xlsx file without styles.xml.
*/
public function testLoadXlsxWithoutStyles(): void
{
$filename = 'tests/data/Reader/XLSX/issue.2246a.xlsx';
$reader = new Xlsx();
$spreadsheet = $reader->load($filename);

$tempFilename = File::temporaryFilename();
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($tempFilename);

$reader = new Xlsx();
$reloadedSpreadsheet = $reader->load($tempFilename);
unlink($tempFilename);

$reloadedWorksheet = $reloadedSpreadsheet->getActiveSheet();

self::assertEquals('TipoDato', $reloadedWorksheet->getCell('A1')->getValue());
$spreadsheet->disconnectWorksheets();
$reloadedSpreadsheet->disconnectWorksheets();
}

/**
* Test load Xlsx file with empty styles.xml.
*/
public function testLoadXlsxWithEmptyStyles(): void
{
$filename = 'tests/data/Reader/XLSX/issue.2246b.xlsx';
$reader = new Xlsx();
$spreadsheet = $reader->load($filename);

$tempFilename = File::temporaryFilename();
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($tempFilename);

$reader = new Xlsx();
$reloadedSpreadsheet = $reader->load($tempFilename);
unlink($tempFilename);

$reloadedWorksheet = $reloadedSpreadsheet->getActiveSheet();
self::assertEquals('TipoDato', $reloadedWorksheet->getCell('A1')->getValue());
$spreadsheet->disconnectWorksheets();
$reloadedSpreadsheet->disconnectWorksheets();
}

public function testLoadXlsxAutofilter(): void
Expand All @@ -78,6 +127,7 @@ public function testLoadXlsxAutofilter(): void
AutoFilter\Column::AUTOFILTER_FILTERTYPE_FILTER,
$autofilter->getColumn('A')->getFilterType()
);
$spreadsheet->disconnectWorksheets();
}

public function testLoadXlsxPageSetup(): void
Expand All @@ -97,6 +147,7 @@ public function testLoadXlsxPageSetup(): void

self::assertEquals(PageSetup::PAPERSIZE_A4, $worksheet->getPageSetup()->getPaperSize());
self::assertEquals(['A10', 'A20', 'A30', 'A40', 'A50'], array_keys($worksheet->getBreaks()));
$spreadsheet->disconnectWorksheets();
}

public function testLoadXlsxConditionalFormatting(): void
Expand All @@ -116,6 +167,7 @@ public function testLoadXlsxConditionalFormatting(): void
self::assertEquals(Conditional::OPERATOR_BETWEEN, $conditionalRule->getOperatorType());
self::assertEquals(['200', '400'], $conditionalRule->getConditions());
self::assertInstanceOf(Style::class, $conditionalRule->getStyle());
$spreadsheet->disconnectWorksheets();
}

public function testLoadXlsxDataValidation(): void
Expand All @@ -127,6 +179,7 @@ public function testLoadXlsxDataValidation(): void
$worksheet = $spreadsheet->getActiveSheet();

self::assertTrue($worksheet->getCell('B3')->hasDataValidation());
$spreadsheet->disconnectWorksheets();
}

/*
Expand All @@ -152,6 +205,7 @@ public function testLoadXlsxDataValidationOfAnotherSheet(): void
self::assertTrue($validationCell->hasDataValidation());
self::assertSame(DataValidation::TYPE_LIST, $validationCell->getDataValidation()->getType());
self::assertSame('Feuil2!$A$3:$A$5', $validationCell->getDataValidation()->getFormula1());
$spreadsheet->disconnectWorksheets();
}

/**
Expand All @@ -163,7 +217,8 @@ public function testLoadXlsxWithoutCellReference(): void
{
$filename = 'tests/data/Reader/XLSX/without_cell_reference.xlsx';
$reader = new Xlsx();
$reader->load($filename);
$spreadsheet = $reader->load($filename);
$spreadsheet->disconnectWorksheets();
}

/**
Expand All @@ -174,12 +229,14 @@ public function testLoadWithReadFilter(): void
$filename = 'tests/data/Reader/XLSX/without_cell_reference.xlsx';
$reader = new Xlsx();
$reader->setReadFilter(new OddColumnReadFilter());
$data = $reader->load($filename)->getActiveSheet()->toArray();
$spreadsheet = $reader->load($filename);
$data = $spreadsheet->getActiveSheet()->toArray();
$ref = [1.0, null, 3.0, null, 5.0, null, 7.0, null, 9.0, null];

for ($i = 0; $i < 10; ++$i) {
self::assertEquals($ref, \array_slice($data[$i], 0, 10, true));
}
$spreadsheet->disconnectWorksheets();
}

/**
Expand All @@ -191,7 +248,8 @@ public function testLoadXlsxWithDoubleAttrDrawing(): void
{
$filename = 'tests/data/Reader/XLSX/double_attr_drawing.xlsx';
$reader = new Xlsx();
$reader->load($filename);
$spreadsheet = $reader->load($filename);
$spreadsheet->disconnectWorksheets();
}

/**
Expand All @@ -206,10 +264,12 @@ public function testLoadSaveWithEmptyDrawings(): void
$resultFilename = File::temporaryFilename();
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($excel);
$writer->save($resultFilename);
$excel->disconnectWorksheets();
$excel = $reader->load($resultFilename);
unlink($resultFilename);
// Fake assert. The only thing we need is to ensure the file is loaded without exception
self::assertNotNull($excel);
$excel->disconnectWorksheets();
}

/**
Expand Down
Binary file added tests/data/Reader/XLSX/issue.2246a.xlsx
Binary file not shown.
Binary file added tests/data/Reader/XLSX/issue.2246b.xlsx
Binary file not shown.