Phpexcel generates corrupted file . xslx

Asked

Viewed 2,356 times

0

I want to generate a file .xlsx, with records that were not imported from another table .xlsx, but I can generate the file .xls without problems, already when I try to generate .xlsx, Excel warns that the file is corrupted.

Follows the code:

$php_excel->setActiveSheetIndex(0)
      ->setCellValue('A' . $linha_php_excel, $row[CPF])
      ->setCellValue('B' . $linha_php_excel, $row[NOME])
      ->setCellValue('C' . $linha_php_excel, $row[NR_CONTRATO])
      ->setCellValue('D' . $linha_php_excel, $row[DATA_CONTRATO])
      ->setCellValue('E' . $linha_php_excel, $row[PRODUTO])
      ->setCellValue('F' . $linha_php_excel, $row[OBS_CONTRATO])
      ->setCellValue('G' . $linha_php_excel, $row[PARCELA])
      ->setCellValue('H' . $linha_php_excel, $row[VALOR])
      ->setCellValue('I' . $linha_php_excel, $row[VALOR])
      ->setCellValue('J' . $linha_php_excel, $row[OBS_PARCELA])
      ->setCellValue('K' . $linha_php_excel, 'Registro já existe');
$php_excel->getActiveSheet()->getStyle('K' . $linha_php_excel)
      ->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
$linha_php_excel++;

Header:

$objWriter = PHPExcel_IOFactory::createWriter($php_excel, 'Excel2007');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="teste.xlsx"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
  • Internally the files . xls and xlsx are different. Only the fact that you change the extension of a valid . xls file to . xlsx already prevents Excel from opening it. xlsx among other things, "zip" the contents. The library that is using supports this file format?

  • Supports, I use Phpexcel class.

1 answer

3


I usually mount header then mount $objWriter and still use ob_end_clean(); to clear any corruption error and after that I export the excel file.

Would look like this...

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

$objWriter = PHPExcel_IOFactory::createWriter($php_excel, 'Excel2007');
ob_end_clean();
$objWriter->save('php://output');
  • That way it worked, thanks for the help, @thiagofred, you know some way, I can make the download happen on another page, like recording in a $_SESSION at $php_excel, and on another page, download the table?

  • I’ve never done this and I don’t know if it works, but try to create a $_SESSION with $objWriter and download it on another page. Maybe it works.

  • It didn’t work, I’ll look it up.

  • but on the page you searched for the $_Session data["$objWriter"] you did include the Phpexcel function to make $objWriter->save('php://output'); when prompted?

  • Yes, again gave the warning that the file is corrupted, when I use the $_SESSION, I think the only way to do is to record each $Row in $_SESSION, and on the page I want to download, mount the . xlsx.

  • It can be... or save $php_excel to the session, then mount the header and export it to another page.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.