PHPExcel modify existing .xlsx file with multiple worksheet

PHPExcel modify existing .xlsx file with multiple worksheet

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHPExcel modify existing .xlsx file with multiple worksheet

When working with legacy spreadsheet manipulation in PHP, developers often encounter unexpected behavior when attempting to update cells on secondary worksheets after loading a multi-sheet Excel 2007 file. The specific issue arises when using the PHPExcel library (now deprecated but still common in older codebases) to load an existing .xlsx file, modify values on multiple sheets, and save the result.

Consider a scenario where you successfully load a workbook named dentkey.xlsx, which contains data across several worksheets. You set up your reader using PHPExcel_IOFactory::createReader('Excel2007') and explicitly call setLoadAllSheets(true). This is critical; without it, the library defaults to loading only the first sheet, rendering any subsequent logic targeting other sheets ineffective from the start.

Once loaded, you might attempt to modify cells on the active sheet like this:

$excel2->getActiveSheet()->setCellValue('C6', '4')
    ->setCellValue('C7', '5')
    ->setCellValue('C8', '6')
    ->setCellValue('C9', '7');

If you then switch to the second sheet using setActiveSheetIndex(1) and attempt similar modifications:

$excel2->setActiveSheetIndex(1);
$excel2->getActiveSheet()->setCellValue('A7', '4')
    ->setCellValue('C7', '5');

You may find that while the file saves successfully as dentkey1.xlsx, the changes intended for the second worksheet are either not reflected or, worse, overwrite existing data in ways you didn't expect. The core misunderstanding here usually lies in how setLoadAllSheets() interacts with memory and object state.

When you call setLoadAllSheets(true), PHPExcel does load all worksheets into memory immediately. However, the active sheet pointer determines which sheet is currently accessible via getActiveSheet(). If you do not explicitly switch back to the correct index before writing, or if your logic assumes that simply calling setLoadAllSheets automatically keeps the previous active sheet context for subsequent operations without re-evaluation, you risk operating on an empty or uninitialized sheet object.

Furthermore, there is a subtle but critical limitation with older versions of PHPExcel: when saving, the library reconstructs the workbook based on its internal state at the moment of save(). If you have modified cells on Sheet 1, switched to Sheet 2, and then saved without ensuring that the setLoadAllSheets flag was correctly preserved or if the internal sheet collection was altered during the process, the resulting file might not retain all original data structures.

A more robust approach involves ensuring that your modifications are isolated to the specific active sheet index before every write operation. Additionally, if you are dealing with large files or need to preserve formatting strictly, consider using PhpSpreadsheet, the actively maintained successor to PHPExcel. It offers better memory management and clearer behavior regarding multi-sheet updates.

If you must stick with PHPExcel for legacy compatibility, always verify that setLoadAllSheets(true) is called before any sheet-specific logic, and ensure your active index is explicitly set before every setCellValue call. Also, be aware that some older versions of the library had issues where modifying a non-active sheet would not persist correctly unless the sheet was explicitly referenced by its name rather than just relying on the active index context.

For those maintaining modern stacks, this pattern of using deprecated libraries often signals a need to migrate to PhpSpreadsheet or integrate with a more robust framework like Laravel’s built-in file handling capabilities. While you can find resources and migration guides at https://laravelcompany.com for context on upgrading legacy PHP components, the fundamental fix remains understanding the active sheet pointer and ensuring all sheets are loaded into memory before modification begins.

Our network