A
A
Alexis_D2018-04-30 10:19:36
PHP
Alexis_D, 2018-04-30 10:19:36

How to correctly specify the format for Excel to read correctly?

Good day. I made an export from the database to Excel using PHPExcel. When opening the downloaded file, excel gives the error "The file '...' cannot be opened because the format or extension of this file is invalid. Please make sure that the file is not damaged and its name extension matches its format"
Here is the code for processing and writing to the excel format

$phpExcel = new PHPExcel();

          $result = mysqli_query($connect, "SELECT * FROM `test_excel`");
          $row = 4;
          while ($data = mysqli_fetch_object($result)) {
            $phpExcel->getActiveSheet()
                ->setCellValue('A'.$row , $data->id)
                ->setCellValue('B'.$row , $data->name)
                ->setCellValue('C'.$row , $data->nameScore)
                ->setCellValue('D'.$row , $data->noSpaceName);
                $row++;
          }

          header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
          header('Content-Disposition:attachment;filename="test.xlsx"');
          header('Cache-Control:max-age=0');

          $file = PHPExcel_IOFactory::createWriter($phpExcel, 'Excel2007');
          $file->save('php://output');

I have excel 2013 if I change the line
$file = PHPExcel_IOFactory::createWriter($phpExcel, 'Excel2007');
to $file = PHPExcel_IOFactory::createWriter($phpExcel, 'Excel2013');
then outputs "cannot open file '...' because the format or extension of this file is invalid."

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Den Korolev, 2018-04-30
@Kodenkos

1) PHPExel deprecated
2) Where did you get Excel2013 from? possible formats Exel2007 and Exel5
3) Well, in the case, change either the extension to xls or the formn to Excel5, it should work.

A
Alexander Taratin, 2018-04-30
@Taraflex

For simple formatting it is better to use
opensource.box.com/spout/getting-started/#writer
output example to browser is commented out

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question