Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

How to Create and Edit Excel Spreadsheets in PHP

April 19, 2016

3 Min Read
Reading Time: 3 minutes

PHP is highly enriched with features of a good scripting language. It provides numerous libraries for specific purposes. PHP provides a library to deal with Excel files. It is called PHP Excel library. It enables you to read and write spreadsheets in various formats including csv, xls, ods, and xlsx. You will need to ensure that you have PHP’s upgraded version not older than PHP 5.2 . Moreover, you should have installed extensions php_qd2, php_xml and php_zip.

excel sheets in php

You might also like: How To Host PHP On DigitalOcean

How To Create Spreadsheet in PHP?

Spreadsheet creation is a very common use case in PHP development. It is used to export data to an Excel spreadsheet. Below is the code for creating an excel spreadsheet using the PHP Excel library.


//Including PHPExcel library and creation of its object


$phpExcel = new PHPExcel;

// Setting font to Arial Black

$phpExcel->getDefaultStyle()->getFont()->setName('Arial Black');

// Setting font size to 14


//Setting description, creator and title

$phpExcel ->getProperties()->setTitle("Vendor list");

$phpExcel ->getProperties()->setCreator("Robert");

$phpExcel ->getProperties()->setDescription("Excel SpreadSheet in PHP");

// Creating PHPExcel spreadsheet writer object

// We will create xlsx file (Excel 2007 and above)

$writer = PHPExcel_IOFactory::createWriter($phpExcel, "Excel2007");

// When creating the writer object, the first sheet is also created

// We will get the already created sheet

$sheet = $phpExcel ->getActiveSheet();

// Setting title of the sheet

$sheet->setTitle('My product list');

// Creating spreadsheet header

$sheet ->getCell('A1')->setValue('Vendor');

$sheet ->getCell('B1')->setValue('Amount');

$sheet ->getCell('C1')->setValue('Cost');

// Making headers text bold and larger


// Insert product data

// Autosize the columns




// Save the spreadsheet


Give Your PHP Applications Optimum Web Performance

Host Your PHP Apps With Us & See The Blazing Web Performance Yourself!

Alternate Approach:

Alternatively, spreadsheet can be downloaded rather than saving it to the server. You may use the following code:

header('Content-Type: application/');

header('Content-Disposition: attachment;filename="file.xlsx"');

header('Cache-Control: max-age=0');


How To Edit an Existing Spreadsheet in PHP?

The editing process is rather similar to the spreadsheet creation process. Use the following code to do this:

// Include PHPExcel library and create its object


// Load an existing spreadsheet

$phpExcel = PHPExcel_IOFactory::load('products.xlsx');

// Get the first sheet

$sheet = $phpExcel ->getActiveSheet();

// Remove 2 rows starting from the row 2

$sheet ->removeRow(2,2);

// Insert one new row before row 2

$sheet->insertNewRowBefore(2, 1);

// Create the PHPExcel spreadsheet writer object

// We will create xlsx file (Excel 2007 and above)

$writer = PHPExcel_IOFactory::createWriter($phpExcel, "Excel2007");

// Save the spreadsheet


You might also like: How To Import And Export CSV Files Using PHP And MySQL

How To Prepare Spreadsheets for Printing?

To get a spreadsheet ready, you should set size, margins and paper orientation.


$sheet -> getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);


$sheet ->getPageMargins()->setRight(0.75);

$sheet ->getPageMargins()->setLeft(0.75);

$sheet ->getPageMargins()->setBottom(1);


The PHPExcel library is very useful for calculations and charts. It is often used to store data in Excel sheets or to import data from Excel spreadsheets via PHP website. Got any questions about what we discussed? Leave a comment and I’ll get back to you!

Apart from knowing tricks such as working on spreadsheets in PHP, any smart PHP developer knows the importance of an excellent hosting solution for the web apps he/she creates. The Cloudways Managed PHP Hosting Platform gives you ultra-fast performance thanks to its optimized hosting stack. Launch your free trial today to venture into the future of PHP hosting.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Launch PHP websites without the worry of Server Management.

Pre-Installed Optimized Stack with Git, Composer & SSH

Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!