Repository: PHPOffice/PhpSpreadsheet (php)

Issue:
Error open file Xlsx when modify value
Good morning, 

Since switching to version >= 2.2, the "setCellValue" method causes an error when opening Excel. 
The error is : "We found a problem in the content of XXX, but we can try to recover as much content as possible.". 
It only produces this with the "Xlsx" format. I tried to reproduce the error with minimal code. But, if you open the file and save without modifying a single line, the file opens correctly.

The function "error_reporting(E_ALL)" gives no error.

Thank you so much



This is:

```
- [ X] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)
```

### What are the steps to reproduce?
Execute the code

Please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of code that exhibits the issue without relying on an external Excel file or a web server:

```php
<?php
require __DIR__ . '/vendor/autoload.php';

$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$spreadsheet->getActiveSheet()->setCellValue('D1', 'MyDate');

$path = __DIR__.'/file_example.xlsx';
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($path);
?>

// add code that show the issue here...
```
If this is an issue with reading a specific spreadsheet file, then it may be appropriate to provide a sample file that demonstrates the problem; but please keep it as small as possible, and sanitize any confidential information before uploading.
[file_example.xlsx](https://github.com/user-attachments/files/16507655/file_example.xlsx)
![error excel open](https://github.com/user-attachments/assets/8644e9f9-5354-463b-9d37-d18a7810dc00)



### What features do you think are causing the issue
- [ ] Reader
- [ X] Writer
- [ ] Styles
- [ ] Data Validations
- [ ] Formula Calculations
- [ ] Charts
- [ ] AutoFilter
- [ ] Form Elements

### Which versions of PhpSpreadsheet and PHP are affected?
Phpspreasheet >=2.2
PHP >= 8.1
Excel 2016
Environment Windows
Langue French


Pull request:
Worksheet applyStylesFromArray Retain Active Cell
Fix #4128. PR #4073 introduced applyStylesFromArray method, which allowed setting styles without affecting selectedCells or activeSheet. The first use of this method was in Cell setValueExplicit to set quotePrefix appropriately. The new method did not preserve activeCell. I'm not sure why that should matter, but this seems to have caused a problem for Excel 2016. This seems to be a bug in Excel, one which is fixed in newer releases. However, PhpSpreadsheet can avoid the problem by preserving activeCell as well as selectedCells and activeSheet. This PR makes that change.

This is:

- [x] a bugfix
- [ ] a new feature
- [ ] refactoring
- [ ] additional unit tests

Checklist:

- [ ] Changes are covered by unit tests
  - [ ] Changes are covered by existing unit tests
  - [x] New unit tests have been added
- [x] Code style is respected
- [x] Commit message explains **why** the change is made (see https://github.com/erlang/otp/wiki/Writing-good-commit-messages)
- [ ] CHANGELOG.md contains a short summary of the change and a link to the pull request if applicable
- [ ] Documentation is updated as necessary

### Why this change is needed?

Provide an explanation of why this change is needed, with links to any Issues (if appropriate).
If this is a bugfix or a new feature, and there are no existing Issues, then please also create an issue that will make it easier to track progress with this PR.


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e89705b17..e7054f1b1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
 The format is based on [Keep a Changelog](https://keepachangelog.com)
 and this project adheres to [Semantic Versioning](https://semver.org).
 
-## TBD - 3.0.0
+## 2024-08-07 - 2.2.2
 
 ### Added
 
@@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
 - RATE Function Floating Point Number of Periods. [PR #4107](https://github.com/PHPOffice/PhpSpreadsheet/pull/4107)
 - Parameter Name Change Xlsx Writer Workbook. [Issue #4108](https://github.com/PHPOffice/PhpSpreadsheet/issues/4108) [PR #4111](https://github.com/PHPOffice/PhpSpreadsheet/pull/4111)
 - New Algorithm for TRUNC, ROUNDUP, ROUNDDOWN. [Issue #4113](https://github.com/PHPOffice/PhpSpreadsheet/issues/4113) [PR #4115](https://github.com/PHPOffice/PhpSpreadsheet/pull/4115)
+- Worksheet applyStylesFromArray Retain Active Cell (Excel 16 was having a problem with some files). [Issue #4128](https://github.com/PHPOffice/PhpSpreadsheet/issues/4128) [PR #4132](https://github.com/PHPOffice/PhpSpreadsheet/pull/4132)
 
 ## 2024-07-29 - 2.2.1
 
diff --git a/src/PhpSpreadsheet/Worksheet/Worksheet.php b/src/PhpSpreadsheet/Worksheet/Worksheet.php
index 7afa82b5d..39355b3c4 100644
--- a/src/PhpSpreadsheet/Worksheet/Worksheet.php
+++ b/src/PhpSpreadsheet/Worksheet/Worksheet.php
@@ -3682,7 +3682,9 @@ class Worksheet implements IComparable
         }
         $activeSheetIndex = $spreadsheet->getActiveSheetIndex();
         $originalSelected = $this->selectedCells;
+        $originalActive = $this->activeCell;
         $this->getStyle($coordinate)->applyFromArray($styleArray);
+        $this->activeCell = $originalActive;
         $this->selectedCells = $originalSelected;
         if ($activeSheetIndex >= 0) {
             $spreadsheet->setActiveSheetIndex($activeSheetIndex);


Test patch:
diff --git a/tests/PhpSpreadsheetTests/Worksheet/Issue4128Test.php b/tests/PhpSpreadsheetTests/Worksheet/Issue4128Test.php
new file mode 100644
index 000000000..4636310a9
--- /dev/null
+++ b/tests/PhpSpreadsheetTests/Worksheet/Issue4128Test.php
@@ -0,0 +1,23 @@
+<?php
+
+declare(strict_types=1);
+
+namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
+
+use PhpOffice\PhpSpreadsheet\Spreadsheet;
+use PHPUnit\Framework\TestCase;
+
+class Issue4128Test extends TestCase
+{
+    public function testIssue4128(): void
+    {
+        $spreadsheet = new Spreadsheet();
+        $sheet = $spreadsheet->getActiveSheet();
+        self::assertSame('A1', $sheet->getActiveCell());
+        self::assertSame('A1', $sheet->getSelectedCells());
+        $sheet->setCellValue('D1', 'MyDate');
+        self::assertSame('A1', $sheet->getActiveCell());
+        self::assertSame('A1', $sheet->getSelectedCells());
+        $spreadsheet->disconnectWorksheets();
+    }
+}