PHP spreadsheet not working when i use objects - php

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');

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');

Create Excel from HTML using PHPExcel

I am using HTML code in a variable and I do not know how can I pass to generate Excel using PHPExcel.
I am able to generate Excel with load HTML file but if I want use HTML in a variable then I am confused. Please give me an idea on how to implement this.
My sample code is below.
<?php
require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';
$objPHPExcel = new PHPExcel();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="name_of_file.xls"');
header('Cache-Control: max-age=0');
$html ='<table><tr><td><font color="blue">Hello</font></td><td><font style="color: blue;">Rest</font></td></tr><tr><td><font style="color: green;">Next</font></td><td><font style="color: Yellow;">Back</font></td></tr></table>';
$wizard = new PHPExcel_Helper_HTML;
$richText = $wizard->buildTextRun($html);
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', $richText);
$objPHPExcel->getActiveSheet()->setTitle('Sheet 1');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output', 'w');
?>
When I am using it in variable then its showing all in column vise but I want that all should come new row vise.

Creating Excel file using PhpSpreadsheet with default data using Laravel 5.5

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');

spreadsheet xlsx to pdf blank page

I am using the PHPSpreadsheet library to convert a generated XLSX file into a PDF. The XLSX conversion and download is working fine, but when I try to convert this XLSX into PDF I am getting empty PDF file, when I try to Opening it, it says the adobe reader/Web reader says "Could not open this file"
<?php
require_once(APPPATH .'third_party/phpspreadsheet/vendor/autoload.php');
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Writer\Csv;
use PhpOffice\PhpSpreadsheet\Exception;
use \PhpOffice\PhpSpreadsheet\Writer\Pdf\Dompdf
$spreadsheet = new Spreadsheet();
$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->setActiveSheetIndex(0)->setCellValue('A1', 'Date')
->setCellValue('B1', 'XXXXX')
->setCellValue('C1', 'YYYY')
->setCellValue('D1', 'ZZZZZ')
->setCellValue('E1', 'XXXXX')
->setCellValue('F1', 'YYYY')
->setCellValue('G1', 'ZZZZ')
->setCellValue('H1', 'ZZZZZ');
// code to fill in the data
$spreadsheet->getActiveSheet()->fromArray($data["result_set"],null,'A2');
$writer = new Xlsx($spreadsheet);
IOFactory::registerWriter("PDF", Dompdf::class);
$pdfwriter = IOFactory::createWriter($spreadsheet, 'PDF');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="01simple.pdf"');
header('Cache-Control: max-age=0');
$writer->save('php://output');

PHPExcel - adding template in multiple sheets

I'm trying to add template in multiple sheets with phpexcel:
$sheet = $objPHPExcel->getActiveSheet();
//Start adding next sheets
$i=0;
while ($i < 10) {
$objPHPExcel = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objPHPExcel->load('template.xlsx'); // Empty Sheet
$objWorkSheet = $objPHPExcel->createSheet($i); //Setting index when creating
//Write cells
$objWorkSheet->setCellValue('A1', 'Hello'.$i)
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');
// Rename sheet
$objWorkSheet->setTitle("$i");
$i++;
}
Unfortunately this doesn't work. I only get two sheets, sheet with template and sheet with "9" title
So this is the result (sheet titles[image]):
Sheet1 9
You can use PHPSpreadhsheet. This is what the currently deprecated PHPExcel library has been continued as.
Include the library in your .php file in the following way -
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
?>
You can set title by adding-
$spreadsheet->setActiveSheetIndex(0)->setCellValue('A1','Hello');
$spreadsheet->getActiveSheet()->setTitle('new');
$writer = new Xlsx($spreadsheet);
$filename = 'XXX';
header('Content-Disposition: attachment;filename="'. $filename .'.xls"');
header('Cache-Control: max-age=0');
$writer->save('php://output');

Categories