A
A
Angelika2020-08-11 19:51:10
PHP
Angelika, 2020-08-11 19:51:10

Why does an error occur when opening a file created with PhpSpreadsheet?

My code for creating excel file:

header('Content-Type: application/json');

$spreadsheet = new PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

//формирую массив с данными

$sheet->fromArray($arrayData, NULL, 'A1');

$writer = new PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
ob_start();
$writer->save('php://output');
$xlsData = ob_get_contents();
ob_end_clean();

echo json_encode('data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,'.base64_encode($xlsData));


Downloading to a computer, the file does not open and gives an error:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
error051840_01.xml Errors found in the file "C:\Users\apisi\Downloads\ Report (6).xlsx"A file-level check has been performed and the file has been repaired. Some fragments of the document may have been restored or deleted.

The file can be opened only if you put a check in the properties:
5f32cc1819b6e754559123.png

I rummaged through the Internet, basically everything rests on the headers or put die at the end of the script;
None of this helps.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Configural, 2022-01-27
@Configural

It happened to me too, it came out after updating Phpspreadsheet, and not in all files, but only where there were fields with lists of valid values ​​(they pulled these values ​​from the second sheet). Project on Laravel 5.4. I decided to downgrade the package to version 1.14 (in which it was guaranteed to work). In composer.json, forced version (without prefixes like ^ or ~)
"require": {
...
"phpoffice/phpspreadsheet": "1.14",
...
},
and ran composer update.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question