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);
Host PHP Websites with Ease [Starts at $10 Credit]
- Free Staging
- Free backup
- PHP 8.0
- Unlimited Websites
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.
Shahzeb Ahmed
Shahzeb is a Digital Marketer with a Software Engineering background, works as a Community Manager — PHP Community at Cloudways. He is growth ambitious and aims to learn & share information about PHP & Laravel Development through practice and experimentation. He loves to travel and explore new ideas whenever he finds time. Get in touch with him at [email protected]