Joey
Joey

Reputation: 1

File cannot be downloaded error when exporting to xlsx using PHPExcel

Good Morning.

I need help regarding exporting data to XLSX format.

I always get this message box error message:

<IP Address> - download.phpをダウンロードできません。

Which says that the download.php cannot be downloaded.

On the other hand, when I try to change this to XLS format, I was able to download the file correctly.

here is my simple code:

index.php

<script>
function ExportDataTemplate()
{
    window.location = "download.php";
}

</script>

<input type="button" class="MyButton" value="Data With Template" style="width: 150px;" onClick="ExportDataTemplate();"/>

download.php

<?php
include 'Classes/PHPExcel.php';
include 'Classes/PHPExcel/IOFactory.php';

error_reporting(E_ALL ^E_NOTICE);

ini_set('memory_limit', -1);
set_time_limit (0) ;

$objReader = PHPExcel_IOFactory::createReader("Excel2007"); 
$objPHPExcel = $objReader->load("Templates/tl_export_xlsx.xlsx");

$objWorksheet = $objPHPExcel->getActiveSheet();
$objPHPExcel->getDefaultStyle()->getFont()->setName('MS Pゴシック')->setSize(10);

$worksheet = $objPHPExcel->setActiveSheetIndex(0);

$newFilename = "TL_Export_". date("YmdHi").".xlsx";

$styleArray = array(
  'borders' => array(
    'allborders' => array(
      'style' => PHPExcel_Style_Border::BORDER_THIN
    )
  )
);

$objPHPExcel->getActiveSheet()->getStyle('A1:C1')->applyFromArray($styleArray);

$worksheet->setCellValueByColumnAndRow(0, 2, "1");
$worksheet->setCellValueByColumnAndRow(1, 2, "Department");
$worksheet->setCellValueByColumnAndRow(2, 2, Team Leader");

unset($styleArray);
ob_clean();

header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=\"".$newFilename."\"");
header("Cache-Control: max-age=0");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); 
$objWriter->save('php://output');

?>

Thank you in advance.

By the way, I am using Linux Cent OS, PHPExcel v1.7.6 and PHP 5.3.3

Upvotes: 0

Views: 1492

Answers (1)

Grzegorz Adam Kowalski
Grzegorz Adam Kowalski

Reputation: 5565

You have simple typo in your code. This:

$worksheet->setCellValueByColumnAndRow(2, 2, Team Leader");

Should be this:

$worksheet->setCellValueByColumnAndRow(2, 2, "Team Leader");

After that, everything seems to work properly.

EDIT:

Because you have IE 8, you should try resolving known compatiblity issues:

  1. Try setting Content-Type to application/vnd.ms-excel
  2. Try setting filename in Content-Disposition without quotes or with single quotes

Also, if you're using javascript (eg. window.open) to download XLSX file in IE 8, then the file may not be downloaded as XLSX is considered "not displayable" in browser.

Upvotes: 1

Related Questions