profile
viewpoint

Ask questionsWriting to a Template xlsx file with a Pivot Table

This is:

- [X] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

Load existing template with a pivot table on the first worksheet, input raw data on the second sheet, and have the pivot table update with the new data.

What is the current behavior?

Data is being inputted on the second sheet but the pivot table on the first sheet is not longer a pivot table, and is now text.

What are the steps to reproduce?

  1. create excel (xlsx) template with pivot table on first page generated from raw data on second.
  2. delete the raw data and refresh the pivot table
  3. load new data into the raw data worksheet with PhpSpreadsheet
  4. download and view xlsx file

Please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue without relying on an external Excel file or a web server: This is very simplified version of the code I'm trying to run. I cannot provide the issue without providing an external excel file, so I'm trusting that you can do that on your own.

<?php
//receive payload as a json post parameter
$payload = isset($_POST['PAYLOAD']) ? json_decode($_POST['PAYLOAD'], true) : null;

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

require_once __DIR__ . './vendor/phpoffice/phpspreadsheet/src/Bootstrap.php';

// Create new Spreadsheet object
$spreadsheet = IOFactory::load('./templates/PivotTemplate.xlsx');

$spreadsheet->getProperties()->setCreator('Ori Nachassi')
        ->setTitle('Pivot Demo')
        ->setSubject('Office 2007 XLSX');


// ============================ START Raw Data SHEET =============================

$spreadsheet->setActiveSheetIndex(1);
$spreadsheet->getActiveSheet()->setTitle('Raw Data');

if ($payload != null) {
	$rowCount = '2';
	foreach ($payload as $row) {

               $spreadsheet->setActiveSheetIndex(1)
		  ->setCellValue('A'.$rowCount, utf8_encode($row[0]));
		
		$spreadsheet->setActiveSheetIndex(1)
		  ->setCellValue('B'.$rowCount, utf8_encode($row[1]));

                $spreadsheet->setActiveSheetIndex(1)
		  ->setCellValue('C'.$rowCount, utf8_encode($row[2]));

                $rowCount++;
	}		
} 	

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$spreadsheet->setActiveSheetIndex(1);
$filename = 'Pivot Table ('.date("d-m-y").').xlsx';
// Redirect output to a client’s web browser (Xlsx)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0

$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
exit;

?>

Which versions of PhpSpreadsheet and PHP are affected?

Php Version 7.1.8 PHPSpreadsheet Version 1.0.0

Thank you for this great tool, your help is greatly appreciated!

PHPOffice/PhpSpreadsheet

Answer questions molerat619

Almost 1 year later - still a bug.

useful!
source:https://uonfu.com/
Github User Rank List