-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Error open file Xlsx when modify value #4128
Comments
Agree that the file you uploaded is messed up. However, using your code, I am unable to see a problem with 2.2.0, 2.2.1, or master - the workbook opens just fine and is as expected (empty except for cell D1 which has its expected value). I am using Excel 365 rather than 2016, but I doubt that's the problem; similarly English rather than French, and even more doubtful of that as the problem. |
I have the same issue with Excel 2016 but I also cannot reproduce it with Excel Home and Studen 2021. |
@oleibman Opening the uploaded example files with my Excel 2021 shows the following error message and I am not able to open the file. A self created XLSX file with the example code dos not show any problem when opening with Excel 2021. At work I am using Excel 2016 and getting the error message like "We found a problem in the content of XXX, but we can try to recover as much content as possible." with my generated xlsx files. I will try to prepare some more example code to reproduce the problem. |
I already noted that the uploaded file is not suitable for any version of Excel. But, since my attempt to reproduce the problem with the supplied code generated a file which appears to usable with any version of Excel (I don't have 2016 but I have access to earlier and later versions), I don't know how to proceed for the moment. Your description of your problem (usable in Excel 2021 but not in 2016) seems to be a different case; I will look forward to your example code. |
Rather oddly, LibreOffice Calc is able to open the uploaded file even though Excel can't. I'm not sure how, and that, of course, does not solve the mystery of how the file was created in that particular manner. |
And now the mystery seems explainable. If I change the file extension from $filename = 'issue.4128.orig.xlsx';
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($filename);
var_dump($reader::class);
$spreadsheet = $reader->load($filename);
var_dump($spreadsheet->getActiveSheet()->getCell('D1')->getValue()); Result:
@Fabacks is it possible that you used the |
I used the following example code and generated the following Excel files (PHP 8.3.10 Windows)
Using PhpSpreadsheet 2.1.0 - No errors when opening the file with Excel 2016: Using PhpSpreadsheet 2.2.1 - Errors when opening the file with Excel 2016: The repair log does not contain any useful content:
|
Thank you for the sample files. There is a minor difference between the 2 files. While I research, can you add: $spreadsheet->getActiveSheet()->setSelectedCells('D1'); Is the resulting workbook now usable in Excel 2016? |
Setting the selected cell seems to solve to problem.
No matter if the selected cell is another cell. |
Fix PHPOffice#4128. PR PHPOffice#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.
@oleibman Yes, testing PR #4132 with Excel 2016 was successful. No need anymore to explicitly set Info for testing: It is important to always use an new xlsx filname because Excel memorizes the last selected cell from previous opening for the same filename. So you would not get the error message again when opening a (new generated) file with the same filename in which you explicitly had set the selected cell. Many thanks for your support! |
Hello, Version 2.2.2 fixes the problem. |
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:
What are the steps to reproduce?
Execute the code
Please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue without relying on an external Excel file or a web server:
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
What features do you think are causing the issue
Which versions of PhpSpreadsheet and PHP are affected?
Phpspreasheet >=2.2
PHP >= 8.1
Excel 2016
Environment Windows
Langue French
The text was updated successfully, but these errors were encountered: