Is it possible to write in an existing spreadSheet with PhpSpreadSheet?

Alexandre Corvino picture Alexandre Corvino · May 30, 2018 · Viewed 7.6k times · Source

I'm currently using PhpSpreadSheet Library and I wanna write into an existing spreadSheet. Is that possible?

If yes, how? I didn't see any possibility in the documentation.

Thanks!

Answer

Evil_skunk picture Evil_skunk · May 30, 2018
  • Load the existing Spreadsheet-File
  • Change something
  • Write it again to Filesystem

Code:

<?php

//load spreadsheet
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load("yourspreadsheet.xlsx");

//change it
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'New Value');

//write it again to Filesystem with the same name (=replace)
$writer = new Xlsx($spreadsheet);
$writer->save('yourspreadsheet.xlsx');