PhpSpreadsheet
PhpSpreadsheet copied to clipboard
.xlsx file opened with error "We found a problem with some content"
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 is the expected behavior?
The file is opened correctly without notification "We found a problem with some content in 'file name'. "
What is the current behavior?
The file is opened with notification "We found a problem with some content in 'file name'. "
What are the steps to reproduce?
public function printReport()
{
$data = $this->allDokumenDipinjam();
if ($data) {
$data = $data->values();
$spreadsheet = IOFactory::load('format/format-laporan.xlsx');
$worksheet = $spreadsheet->getActiveSheet();
foreach ($data as $index => $debitur) {
$dokumen = $debitur->dokumen->values();
$dokumenCount = count($dokumen);
$worksheet->mergeCells("A" . 5 + $index . ":A" . 5 + $dokumenCount - 1);
}
}
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('tes.xlsx');
return response()->download('tes.xlsx')->deleteFileAfterSend(true);
}
What features do you think are causing the issue
- [ ] Reader
- [X] Writer
- [X] Styles
- [ ] Data Validations
- [ ] Formula Calculations
- [ ] Charts
- [ ] AutoFilter
- [ ] Form Elements
Does an issue affect all spreadsheet file formats? If not, which formats are affected?
I only tried it in .xlsx format.
Which versions of PhpSpreadsheet and PHP are affected?
PHP: 8.2.4 PhpSpreadsheet: 2.0
Unable to debug without file (format-laporan.xlsx) and data (allDokumenDipinjam()).
I can confirm this bug and add the following preliminary information:
- it does not originate in release 2.0, but somewhere in release 1.29.0. The same file works in versions <= 1.28.0, but not in 1.29.0 or 2.0.0
- it does not look like Zipstream 3 is to blame, I downgraded to 2.4 and still had it generate a faulty file.
- It's not limited to the writer. If you take an xlsx template and load it, then immediately save it, for example as xls, you will still end up with a faulty file.
- it only appears in Excel. LibreOffice opens the files without a problem, as before.
I've been doing various diffs to try and find the bug. I have a hunch it has something to do with styles, but will need to diff deeper to find it.
I have the same problem. I tried to downgrade phpoffice/phpspreadsheet => 1.28.0 and Zipstream => 2.4 but it Is the same. What can i do?
Unable to help without a file that demonstrates the problem.
I tried to generate my file with a template and without it.
$objPHPExcel = new Spreadsheet();
//$objReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
//$objPHPExcel = \PhpOffice\PhpSpreadsheet\IOFactory::load("excel/Templates/usuariosCursos.xlsx");
// Set document properties
$objPHPExcel->getProperties()->setCreator("Sistema Versión 3.0")
->setLastModifiedBy("Sistema Versión 3.0")
->setTitle("Lista de Usuarios")
->setSubject("Lista de Usuarios")
->setDescription("Lista de Usuarios")
->setKeywords("lista usuarios")
->setCategory("Lista");
foreach ($newUsers as $i => $user) {
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A' . ($i + 2), $user['entry_ID'])
->setCellValue('B' . ($i + 2), $user['user_ID'])
->setCellValue('C' . ($i + 2), $user['firstNames'])
->setCellValue('D' . ($i + 2), $user['lastNames'])
->setCellValue('E' . ($i + 2), $user['phone'])
->setCellValue('F' . ($i + 2), $user['email'])
->setCellValue('G' . ($i + 2), $user['province'])
->setCellValue('H' . ($i + 2), $user['country'])
->setCellValue('I' . ($i + 2), $user['occupation'])
->setCellValue('J' . ($i + 2), $user['sector_0'])
->setCellValue('K' . ($i + 2), $user['sector_1'])
->setCellValue('L' . ($i + 2), $user['sector_2'])
->setCellValue('M' . ($i + 2), $user['anuncios'])
->setCellValue('N' . ($i + 2), $user['seller']);
}
foreach (range('A', 'I') as $columnID) {
$objPHPExcel->getActiveSheet()->getColumnDimension($columnID)
->setAutoSize(true);
}
$link = 'lista_usuarios_' . gmdate('d-M-Y_H-i-s') . '.xlsx';
$objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objPHPExcel, "Xlsx");
ob_end_clean();
$objWriter->save($link);
Before I used php 7.3 with PHPExcel and I didn't issues but now I updated my php version to 8.3 with PhpSpreadsheet but when I try to open my file, excel give me the error "We found a problem with some content". LibreOffice can open the file without problem.
Your code creates an error-free spreadsheet for me. I did need to comment out the ob_end_clean
call because it doesn't match up with an earlier ob_start. Of course, if that is not the source of your problem, I do not know how you populated the $newUsers
array. For my purposes, I used the following code:
$newUsers = [
['entry_ID' => 'entry1', 'user_ID' => 'userid1', 'firstNames' => 'first1', 'lastNames' => 'last1', 'phone' => 'phone1', 'email' => 'email1', 'province' => 'province1', 'country' => 'country1', 'occupation' => 'occ1', 'sector_0' => 'sec0_1', 'sector_1' => 'sec1_1', 'sector_2' => 'sec2_1', 'anuncios' => 'anuncios1', 'seller' => 'seller1'],
['entry_ID' => 'entry2', 'user_ID' => 'userid2', 'firstNames' => 'first2', 'lastNames' => 'last2', 'phone' => 'phone2', 'email' => 'email2', 'province' => 'province2', 'country' => 'country2', 'occupation' => 'occ2', 'sector_0' => 'sec0_2', 'sector_1' => 'sec1_2', 'sector_2' => 'sec2_2', 'anuncios' => 'anuncios2', 'seller' => 'seller2'],
];
To try to narrow down this problem, can you add that to your code just before the foreach
loop and see what you wind up with.
I added the array before the foreach loop, and it's the same, but I'm checking that this error only happens in production enviroment, local works fine. I don't know if I have to change some config with php directives or something else.
Just guessing, but I would be interested to know if there is any difference between the settings for error_reporting
and display_errors
in your 2 environments.
They had same display_errors = On and differents error_reporting. I changed in production error_reporting = E_ALL. The site gave me an error about ob_clean on my download code, I removed this line and now it's working fine. Thank you very much for your help!! :)
Maybe, for Laravel: https://github.com/PHPOffice/PhpSpreadsheet/issues/217#issuecomment-451972678