Creating a spreadsheet - PhpSpreadsheet Documentation (2024)

The Spreadsheet class

The Spreadsheet class is the core of PhpSpreadsheet. It containsreferences to the contained worksheets, document security settings anddocument meta data.

To simplify the PhpSpreadsheet concept: the Spreadsheet classrepresents your workbook.

Typically, you will create a workbook in one of two ways, either byloading it from a spreadsheet file, or creating it manually. A thirdoption, though less commonly used, is cloning an existing workbook thathas been created using one of the previous two methods.

Loading a Workbook from a file

Details of the different spreadsheet formats supported, and the optionsavailable to read them into a Spreadsheet object are described fully inthe Reading Files document.

$inputFileName = './sampleData/example1.xls';/** Load $inputFileName to a Spreadsheet object **/$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);

Creating a new workbook

If you want to create a new workbook, rather than load one from file,then you simply need to instantiate it as a new Spreadsheet object.

/** Create a new Spreadsheet Object **/$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

A new workbook will always be created with a single worksheet.

Clearing a Workbook from memory

The PhpSpreadsheet object contains cyclic references (e.g. the workbookis linked to the worksheets, and the worksheets are linked to theirparent workbook) which cause problems when PHP tries to clear theobjects from memory when they are unset(), or at the end of a functionwhen they are in local scope. The result of this is "memory leaks",which can easily use a large amount of PHP's limited memory.

This can only be resolved manually: if you need to unset a workbook,then you also need to "break" these cyclic references before doing so.PhpSpreadsheet provides the disconnectWorksheets() method for thispurpose.

$spreadsheet->disconnectWorksheets();unset($spreadsheet);
Creating a spreadsheet - PhpSpreadsheet Documentation (2024)
Top Articles
Latest Posts
Article information

Author: Greg O'Connell

Last Updated:

Views: 5793

Rating: 4.1 / 5 (62 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Greg O'Connell

Birthday: 1992-01-10

Address: Suite 517 2436 Jefferey Pass, Shanitaside, UT 27519

Phone: +2614651609714

Job: Education Developer

Hobby: Cooking, Gambling, Pottery, Shooting, Baseball, Singing, Snowboarding

Introduction: My name is Greg O'Connell, I am a delightful, colorful, talented, kind, lively, modern, tender person who loves writing and wants to share my knowledge and understanding with you.