How to Write Data to an Excel File Using PHP

A detailed guide on how to write data to an Excel file using PHP with the help of the PHPExcel library or the more modern PhpSpreadsheet library.

We will use the PhpSpreadsheet library, a popular and powerful tool for manipulating Excel files in PHP. This guide will show you how to install and use the library to create and write data to an Excel file.

Step 1: Install the PhpSpreadsheet library

First, install the PhpSpreadsheet library using Composer:

composer require phpoffice/phpspreadsheet

Step 2: Write data to an Excel file

Here is a simple example of how to write data to an Excel file using PHP:

<?php
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

// Create a new Excel file
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

// Write data to cells
$sheet->setCellValue('A1', 'Name');
$sheet->setCellValue('B1', 'Age');
$sheet->setCellValue('A2', 'John Doe');
$sheet->setCellValue('B2', '25');
$sheet->setCellValue('A3', 'Jane Smith');
$sheet->setCellValue('B3', '30');

// Save the Excel file
$writer = new Xlsx($spreadsheet);
$writer->save('list.xlsx');

echo "Data has been written to the Excel file successfully!";
?>

Detailed explanation

  1. use PhpOffice\PhpSpreadsheet\Spreadsheet;: Uses the Spreadsheet class from the PhpSpreadsheet library.
  2. $spreadsheet = new Spreadsheet();: Creates a new Spreadsheet object.
  3. $sheet->setCellValue('A1', 'Name');: Writes data to cell A1 with the value "Name".
  4. $writer = new Xlsx($spreadsheet);: Creates a Writer object to save the Excel file in .xlsx format.
  5. $writer->save('list.xlsx');: Saves the Excel file as "list.xlsx" in the current directory.

System Requirements

  • PHP version 7.3 or later
  • Composer to manage PHP libraries
  • PhpSpreadsheet library

How to install libraries to run the above PHP code

  1. Install Composer if you haven't: https://getcomposer.org/download/
  2. Use the command composer require phpoffice/phpspreadsheet to install the PhpSpreadsheet library.
  3. Make sure your PHP file includes require 'vendor/autoload.php'; to automatically load the libraries installed via Composer.

Tips

  • Always validate your data before writing to the Excel file to avoid potential issues.
  • Ensure the target directory has write permissions to save the Excel file successfully.


Related

How to convert a Markdown string to HTML using PHP

A guide on how to convert a Markdown string to HTML in PHP using the `Parsedown` library, enabling you to display Markdown content effectively on your website.
Creating a Simple Chat Application Using Socket.IO in PHP

A step-by-step guide to creating a simple chat application using Socket.IO in PHP, helping you understand how to utilize WebSocket for real-time communication.
Update data in MySQL using PHP

A guide on how to update data in MySQL using PHP. This code uses the UPDATE SQL statement to change information in a MySQL database efficiently.
How to use strtok() function in PHP

This article explains how to use the `strtok` function in PHP to split a string into smaller parts based on delimiters. The `strtok` function is a useful tool for string manipulation in PHP projects.
Check if a given String is Binary String or Not using PHP

A guide on how to check if a given string is a binary string (containing only `0` and `1` characters) in PHP. This article uses basic PHP string functions.
Guide to reading Excel files in PHP

A comprehensive guide on how to read content from Excel files (.xlsx, .xls) using PHP, utilizing the PHPExcel library, including installation and implementation steps.
Example of Singleton Pattern in PHP

A guide on the Singleton Pattern in PHP with a detailed example, explaining how to implement and use this design pattern in object-oriented programming.
How to check for a substring in a string in PHP

A guide on how to use the `strpos` function in PHP to check if a substring exists within a larger string. This is a simple and efficient way to handle string operations in PHP.
Create image thumbnail with PHP

A guide on how to create an image thumbnail with PHP using the GD library. This PHP code helps resize an original image to a custom thumbnail size.
Update multiple columns in MySQL using PHP

A guide on how to update multiple columns in MySQL using PHP. This PHP code uses the UPDATE statement to change multiple column values in a MySQL database.

main.add_cart_success