Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Xls Reader changing grey background to black in Excel template [Issue #2147](Changing grey background to black in Excel template) [PR #2156](https://github.com/PHPOffice/PhpSpreadsheet/pull/2156)
- Column width and Row height styles in the Html Reader when the value includes a unit of measure. [Issue #2145](https://github.com/PHPOffice/PhpSpreadsheet/issues/2145).
- Data Validation flags not set correctly when reading XLSX files. [Issue #2224](https://github.com/PHPOffice/PhpSpreadsheet/issues/2224) [PR #2225](https://github.com/PHPOffice/PhpSpreadsheet/pull/2225)
- Reading XLSX files without styles.xml throws an exception. [Issue #2246](https://github.com/PHPOffice/PhpSpreadsheet/issues/2246)

## 1.18.0 - 2021-05-31

Expand Down
26 changes: 19 additions & 7 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,9 @@ 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 +533,14 @@ 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 +598,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,10 +633,13 @@ 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);
}
}

$styleReader = new Styles($xmlStyles);
$styleReader->setStyleBaseData(self::$theme, $styles, $cellStyles);
$dxfs = $styleReader->dxfs($this->readDataOnly);
Expand Down
45 changes: 44 additions & 1 deletion 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 @@ -63,6 +63,49 @@ public function testLoadXlsxWithStyles(): void
}
}

/**
* 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());
}

/**
* 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());
}

public function testLoadXlsxAutofilter(): void
{
$filename = 'tests/data/Reader/XLSX/autofilterTest.xlsx';
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.