PHPExcel - adding template in multiple sheets - php

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

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

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

Hardcoding headers to excel file using PHP while dynamically querying SQL

I have this PHPExcel code:
/** Error reporting */
error_reporting(E_ALL);
date_default_timezone_set('Europe/London');
/** Include PHPExcel */
require_once '../Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
//$objPHPExcel->setActiveSheetIndex(0)
// ->setCellValue('A1', 'Hello')
// ->setCellValue('A2', 'world!')
$row = 2;
while($row_data = mysql_fetch_assoc($result)) {
$col = 1;
foreach($row_data as $key=>$value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$col++;
}
$row++;
}
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Results');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Output.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
Basically I am dynamically querying the database and placing it into an excel file.
I just want to hardcode in a few headers. So for A1 I'd like to hardcode in "Hello" and A2: "World!" while querying the database and putting the data starting in Column B
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('A2', 'world!');
$col = 1;
while($row_data = mysql_fetch_assoc($result)) {
$row = 1;
foreach($row_data as $value) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
$row++;
}
$col++;
}
Note that Excel5 has a limit of 256 columns, so if you have more than 255 data records the additional columns will de dropped from the saved workbook if you're saving to that format.

Categories