Download spreadsheet using the Phpexcel class

Asked

Viewed 2,050 times

-3

I found this phpexcel class, I can generate xls files searching Mysql data. It works really well. But I need to take a spreadsheet that is stored as a template in the database and only a few blank fields.

I want to take this spreadsheet, upload it, fill in the cells and then download it.

  • Is there a code? Something’s wrong?

  • I was starting from scratch, but I was able to walk around a little bit taking out some macros that existed in the spreadsheet. 
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);

$objPHPExcel->setActiveSheetIndex(0) ->setCellValue('C3', $numRegister);

  • To download you can use this code header('Content-Type: application/vnd.ms-excel;');
header('Content-Disposition: attachment;filename=plan.xls');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: max-age=0');
 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');

  • Thank you! I’ll test it here.

  • Taking advantage of space, it worked perfectly. Now I wanted to get a file in stored in a blob field in mysql, I would like to grab it, temporarily store, edit and download it on the machine, has how to do?

  • I believe that reply can help.

  • Thanks, I’ll take a look at the documentation of this class here.

Show 2 more comments

2 answers

1

To generate XLSX use

Define in $xlsName the name of the XLSX with the extension. Example: $xlsName = 'test.xlsx';

$objPHPExcel = new PHPExcel();

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$xlsName.'"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');

For XLS use

Define in $xlsName the name of the XLS with the extension. Example: $xlsName = 'test.xls';

$objPHPExcel = new PHPExcel();

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$xlsName.'"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
  • The code is no longer in the codeplex, it’s in the github

  • Version earlier than PHP 5.3.3 I recommend generating XLS, because some of the XLXS classes have functions not existing in PHP prior to 5.4.0. The recommended version of PHP to stop generating XLSX is 5.4.0

1

To force the download of an excel sheet it is necessary to define the header, stating how the content should be treated by the browser and invoke the method save() of some Writer

//definição do cabeçalho
header('Content-Type: application/vnd.ms-excel;');
header('Content-Disposition: attachment;filename=plan.xls');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: max-age=0');

//cria uma planilha no formato do excel 2003
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');

The latest versions, documentations, tutorials etc can be found at: Phpexcel

Browser other questions tagged

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