Creating Excel file using PhpSpreadsheet with default data using Laravel 5.5 - php

I am trying to download excel file with pre-populated data using PhpSpreadsheet in laravel. But I am got empty file downloaded.Here`s my code.
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('B1','testing');
$writer = new Xlsx($spreadsheet);
$filename = 'tessa';
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'. $filename .'.Xlsx"');
header('Cache-Control: max-age=0');
$writer->save($filename);
I tried dd($sheet) Here how it looks,it contains my data;
The file downloaded but empty file.Can any one tell me how to solve the issue?

You don't really need to set those headers. But if you must, edit filename to:
$filename = 'tessa.xlsx';

You need to change
$writer->save($filename);
to:
$writer->save('php://output');

Related

How to modify existing macro enabled excel file in laravel?

I am using Php spreadsheet api for modify existing macro enabled excel file. but after download same file with update first sheet data, remaining macro file layout missing and corrupted.
Please suggest if i am wrong ?
Code:
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader->setRealFormat(true);
$spreadsheet = $reader->load($inputFileName);
$position_at_row1 = $position_at_row+1;
$count_new = $position_at_row1 +24;
$sheet = $spreadsheet->getSheetByName('Tabelle1');
$sheet = $spreadsheet->getSheet(0);
$sheet->getCell('A1')->setValue(10);
$sheet->getCell('A2')->setValue(10);
$writer = new Xlsx($spreadsheet);
header("Content-Type: application/vnd.ms-excel");
header('Content-Disposition: attachment; filename="' . urlencode($original_file_name) . '"');
$writer->save('php://output');

PHP spreadsheet not working when i use objects

im trying to use phpspreadsheet to save some data from my db to an excel file. The problem is: when i create my sql object, my excel document just the format is not valid. When i delete the object, file opens normally. if i comment the line $psql = new PSql(); it may works fine.
require_once("funcoes/Psql.php");
require 'vendor/autoload.php';
$psql = new PSql();
$dados = $psql->getUltimoRegistro($_SESSION["contrato"]);
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$spreadsheet->getActiveSheet()
->setCellValue('A1', "Contrato")
->setCellValue('B1', "Código")
->setCellValue('C1', "Estatstica")
->setCellValue('D1', "Infração");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="result.xlsx"');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');

PhpSpreadsheet corrupted xlsx file

I have issue when trying to download xlsx file. It works just fine when i test it locally, but when i upload it to production server, file gets corrupted.
this is the file output:
this is code im using:
<?php
require 'conn.php';
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', '#');
$sheet->setCellValue('B1', 'First');
$sheet->setCellValue('C1', 'Last');
$sheet->setCellValue('D1', 'Handle');
header('Content-Type: application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="text.xlsx"');
header('Cache-Control: max-age=0');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
die;
i tried changing header to, it did not help:
header('Content-type: application/vnd.ms-excel');
I think that will be ok if you add cleaning of the output buffer before you call createWriter static method.
ob_end_clean();
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
I had the same symptom and it was because the webserver continued to output things after doing
$writer->save('php://output');
In my case it was feasible to just do a die() after it and the problem's gone.
Maybe it can save somebody a little time.
I had the same problem and ob_end_clean();solved my problem also. The last lines are below.
ob_end_clean();
$writer=new Xlsx($spreadsheet);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="'.urlencode($file_name).'"');
$writer->save('php://output');

Error : the file format or file extension is not valid. verify that the file has not been corrupted

i want to use PHPSpreadsheet library in my project to export data to excel the file exported but when i try to open the file this error display: excel cannot open the file because the file format or file extension is not valid. verify that the file has not been corrupted
Note: i use MVC in my project
so the code in controller as the following:
protected function Excel($view, $variables = [])
{
require_once PATH_LIBRARY_FOLDER.'PhpSpreadsheet\vendor\autoload.php';
ob_start();
// Note: make new Spreadsheet object
$spreadsheet = new Spreadsheet();
// Note: get current active sheet (frist sheet)
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
extract($variables);
include($this->viewPath.$view.'.php');
// Note: set the header to define it is excel file
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
// Note: set the header to define excel file name
header("Content-Disposition: attachment;filename=\"filename.xlsx\"");
header("Cache-Control: max-age=0");
// Note: create IOFactory object
$writer = IOFactory::createWriter($spreadsheet, 'xlsx');
ob_get_clean();
$writer->save('php:://output');
exit();
}
when i open the file as a text i found this error:
Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Writer\Exception: Could not open php:://output for writing. in C:\xampp\htdocs\GL_App\Library\PhpSpreadsheet\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx.php:218
I have the same problem,My solution here . Just put code below :
ob_end_clean();
Code:
ob_end_clean();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'. $filename );
header('Cache-Control: max-age=0');
$writer->save('php://output'); // download file
Change this line of code
$writer->save('php:://output');
to
$writer->save('php://output');
It should work. Also, how do you pass data to excel sheet? It's possible that you have corrupt data.
Please, insert this in your code after autoload.php
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

PHPExcel is not generating correct xls file

I am using PHPExcel in my project to generate a xls file of data coming from the database. I have downloaded the library of PHPExcel and using it in my PHP class as give below :
Class name : A.class.php
Path : inside a folder named "inc";
PHPExcel Lib folder : inside "inc" and relative to A.class.php
class A{
// code stuff
public function phpexcelgenerate()
{
require_once 'PHPExcel_1.7.9_doc/Classes/PHPExcel.php';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="sam.xlsx"');
header('Cache-Control: max-age=0');
$objPHPExcel = new PHPExcel;
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', "12");
$writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
// This line will force the file to download
$writer->save();
}
// code stuff
}
This code is generating a xls file but that file is fully empty. I am thinking that I have included the files of PHPExcel in a wrong way in PHP classes. Could anyone tell me where I am doing wrong OR an example how can I do the same ?
You need to pass a filename to the save() method. Then read the contents of that files and echo to the browser.
$writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$writer->save("excel.xls");
readfile("excel.xls");
first of all you need to read the file:
// Read the file
$objReader = PHPExcel_IOFactory::createReader(Excel5);
$objPHPExcel = $objReader->load($fileName);
now get the data from db:
//get data
$details = $this->main_model->get_details()->row();
then assign the data to correct cells:
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $i);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $rec->eType);
at the end write the new file:
header('Content-Type: application/vnd.ms-excel'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
//force user to download the Excel file without writing it to server's HD
$objWriter->save('php://output');

Categories