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

Updated on June 14, 2021

2 Min Read
create-or-edit-excel-sheets-in-php

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.

You might also like: How To Host PHP On DigitalOcean

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.

Code:

//Including PHPExcel library and creation of its object

require('PHPExcel.php');

$phpExcel = new PHPExcel;

// Setting font to Arial Black

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

// Setting font size to 14

$phpExcel->getDefaultStyle()->getFont()->setSize(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

$sheet->getStyle('A1:D1')->getFont()->setBold(true)->setSize(14);

// Insert product data

// Autosize the columns

$sheet->getColumnDimension('A')->setAutoSize(true);

$sheet->getColumnDimension('B')->setAutoSize(true);

$sheet->getColumnDimension('C')->setAutoSize(true);

// Save the spreadsheet

$writer->save('products.xlsx');

Stop Wasting Time on Servers

Cloudways handle server management for you so you can focus on creating great apps and keeping your clients happy.

Alternate Approach:

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

header('Content-Type: application/vnd.ms-excel');

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

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

$writer->save('php://output');

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

require('PHPExcel.php');

// 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

$writer->save('products.xlsx');

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()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);

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

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

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

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

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

Conclusion

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

Customer Review at

“Cloudways hosting has one of the best customer service and hosting speed”

Sanjit C [Website Developer]

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 [email protected]

×

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

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!