Created
August 11, 2017 03:30
-
-
Save ahgood/562090d22b6eed262ae43e878c292968 to your computer and use it in GitHub Desktop.
Create multiple worksheet by PhpSpreadsheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Install: composer require phpoffice/phpspreadsheet:dev-develop | |
Github: https://github.com/PHPOffice/PhpSpreadsheet/ | |
Document: https://phpspreadsheet.readthedocs.io/ | |
*/ | |
require 'vendor/autoload.php'; | |
use PhpOffice\PhpSpreadsheet\IOFactory; | |
use PhpOffice\PhpSpreadsheet\Spreadsheet; | |
// Create new Spreadsheet object | |
$spreadsheet = new Spreadsheet(); | |
// Set document properties | |
$spreadsheet->getProperties()->setCreator('PhpOffice') | |
->setLastModifiedBy('PhpOffice') | |
->setTitle('Office 2007 XLSX Test Document') | |
->setSubject('Office 2007 XLSX Test Document') | |
->setDescription('PhpOffice') | |
->setKeywords('PhpOffice') | |
->setCategory('PhpOffice'); | |
// Add some data | |
$spreadsheet->setActiveSheetIndex(0) | |
->setCellValue('A1', 'Hello'); | |
// Rename worksheet | |
$spreadsheet->getActiveSheet()->setTitle('URL Added'); | |
$spreadsheet->createSheet(); | |
// Add some data | |
$spreadsheet->setActiveSheetIndex(1) | |
->setCellValue('A1', 'world!'); | |
// Rename worksheet | |
$spreadsheet->getActiveSheet()->setTitle('URL Removed'); | |
// Set active sheet index to the first sheet, so Excel opens this as the first sheet | |
$spreadsheet->setActiveSheetIndex(0); | |
// Redirect output to a client’s web browser (Xlsx) | |
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); | |
header('Content-Disposition: attachment;filename="01simple.xlsx"'); | |
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@asha15 I think you need to read your file first and then use method above to update to update it, then save.