Create Excel file in PHP (2024)

User GuideTutorialsPHP tutorialsTutorial 03: How to create Excel file in PHP
Previous sample
Export data to Excel in custom format in PHP
Next sample
Export data to XLSX file in PHP

EasyXLS Excel library can be used to export Excel files with PHP on Windows, Linux, Mac or other operating systems. The integration vary depending on the operating system or if .NET Framework or Java is chosen:

1. EasyXLS on Windows using .NET Framework (COM+) with PHP


2. EasyXLS on Linux, Mac, Windows using Java with PHP

EasyXLS on Windows using .NET Framework (COM+) with PHP

If you opt for the COM+ version of EasyXLS, be sure that you have EasyXLS installed and registered on your machine.

Find more about Getting Started with EasyXLS

Step 1: Download and install EasyXLS Excel Library for COM+

To download the trial version of EasyXLS Excel Library, press the below button:

If you already own a license key, you may login and download EasyXLS from your account.

Step 2: Verify if COM and .NET extension is enabled in PHP

Check PHP.ini for [COM_DOT_NET] extension.

See also

How to enable COM and .NET extension in PHP?


Step 3: Verify if EasyXLS is registered

Check if EasyXLS component is present in Component Services.

See also

How to verify if EasyXLS is registered?


Step 4: License file setup

Step required for EasyXLS v9.0 or later.

If you are using a trial, generate a trial license file from EasyXLS trials page. The trial license is valid for 30-days.

If you own a license key, you may login to the account that purchased the license and generate the license file from:
https://www.easyxls.com/my-orders

Setup the license file into your project using these guidelines.

Step 5: Run PHP code that that creates Excel file

Execute the following PHP code that creates Excel file programmatically.

View Excel file

Copy code

<?php/*===============================================================| Tutorial 03|| This tutorial shows how to create an Excel file that has | multiple sheets in PHP. The created Excel file is empty and the | next tutorial shows how to add data into sheets.* =============================================================*/header("Content-Type: text/html");echo "Tutorial 03<br>";echo "----------<br>";// Create an instance of the class that creates Excel files$workbook = new COM("EasyXLS.ExcelDocument");// Create two sheets$workbook->easy_addWorksheet_2("First tab");$workbook->easy_addWorksheet_2("Second tab");// Create Excel fileecho "Writing file: C:\Samples\Tutorial03 - create Excel file.xlsx<br>";$workbook->easy_WriteXLSXFile("C:\Samples\Tutorial03 - create Excel file.xlsx");// Confirm the creation of Excel fileif ($workbook->easy_getError() == "") echo "File successfully created.";else echo "Error encountered: " . $workbook->easy_getError();// Dispose memory$workbook->Dispose();$workbook = null;?>
Overloaded methods
For methods with same name but different parameters, only the first method overload retains the original name. Subsequent overloads are uniquely renamed by appending to the method name '_2', '_3', etc (method, method_2, method_3), an integer that corresponds to the order of declaration that can be found in EasyXLS.h, a file that comes with EasyXLS installation.

EasyXLS on Linux, Mac, Windows using Java with PHP

If you opt for the Java version of EasyXLS, a similar code as above requires PHP/Java Bridge between PHP and Java.

Find more about Getting Started with EasyXLS

Step 1: Download EasyXLS Excel Library for Java

To download the trial version of EasyXLS Excel Library, press the below button:

If you already own a license key, you may login and download EasyXLS from your account.

Step 2: License file setup

Step required for EasyXLS v9.0 or later.

If you are using a trial, generate a trial license file from EasyXLS trials page. The trial license is valid for 30-days.

If you own a license key, you may login to the account that purchased the license and generate the license file from:
https://www.easyxls.com/my-orders

Setup the license file into your project using these guidelines.

Step 3: Install PHP/Java Bridge

Download PHP/Java Bridge and follow the PHP/Java Bridge installation guide for directions.

Step 4: Setup EasyXLS library in Tomcat

Copy EasyXLS.jar into Tomcat installation path, lib folder.

Step 5: Run PHP code that that creates Excel file

Execute a code as below PHP code that creates Excel file programmatically.

View Excel file

Copy code

<?php require_once("http://localhost:8080/JavaBridge/java/Java.inc");/*==============================================================| Tutorial 03|| This tutorial shows how to create an Excel file that has | multiple sheets in PHP. The created Excel file is empty and the | next tutorial shows how to add data into sheets.===============================================================*/header("Content-Type: text/html");echo "Tutorial 03<br>";echo "----------<br>";// Create an instance of the class that creates Excel files$workbook = new java("EasyXLS.ExcelDocument"); // Create two sheets$workbook->easy_addWorksheet("First tab");$workbook->easy_addWorksheet("Second tab");// Create Excel fileecho "Writing file: C:\Samples\Tutorial03 - create Excel file.xlsx<br>";$workbook->easy_WriteXLSXFile("C:\Samples\Tutorial03 - create Excel file.xlsx");// Confirm the creation of Excel fileif ($workbook->easy_getError() == "") echo "File successfully created.";else echo "Error encountered: " . $workbook->easy_getError();// Dispose memory$workbook->Dispose();?>

Related sections

How to create Excel file?


How to set sheet name, sheet color, sheet zoom, hide sheet, protect sheet or select sheet?

See also

How to create new Excel sheet?


How to create XLSX file?


How to create XLSM file?


How to create XLSB file?


How to create XLS file?


How to add data to Excel file?


How to create Excel file from list?

Related methods

ExcelDocument.easy_WriteXLSXFile
ExcelDocument.easy_WriteXLSBFile
ExcelDocument.easy_WriteXLSFile
ExcelWorksheet.easy_insertList
ExcelDocument.easy_WriteXLSXFile_FromList
ExcelDocument.easy_WriteXLSBFile_FromList
ExcelDocument.easy_WriteXLSFile_FromList

Previous sample
Export data to Excel in custom format in PHP
Next sample
Export data to XLSX file in PHP
Create Excel file in PHP (2024)

FAQs

How to create an Excel file using PHP? ›

Create an Excel File in PHP

Access Cells collection of the desired worksheet using $worksheets->get(index)->getCells() method. Insert value into the desired cell using $cells->get(“A1”)->putValue(“Hello world!”) method. Save the Excel workbook using $workbook->save(“output. xlsx”, cells\SaveFormat::XLSX) method.

Why doesn't PHPExcel allow to write more than 5000 rows? ›

Almost certainly this is a timeout or a memory issue. The only PHPExcel limit for worksheets size is 65,536 rows and 256 (IV) columns (when using the Excel5 Writer); or 1,048,576 rows and 16,384 (XFD) columns (when using the Excel2007 Writer). $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

What is the best PHP library for Excel? ›

1. PhpSpreadsheet. This is one of the most starred libraries on GitHub with more than 11k stars on GitHub. As the name suggests, this is a library written entirely in PHP that provides a number of classes that let you read and write a variety of spreadsheet file formats, including Excel and LibreOffice Calc.

How to set data in Excel in PHP? ›

To insert data into an Excel sheet using PHP, you can utilize a library like PHPExcel or PhpSpreadsheet (the successor to PHPExcel). These libraries provide a range of functionalities to create, manipulate, and write data to Excel files.

How to create a file in PHP? ›

PHP Create File - fopen()

The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files. If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).

How to create empty Excel file in PHP? ›

xlsx<br>"; $workbook->easy_WriteXLSXFile("C:\Samples\Tutorial03 - create Excel file. xlsx"); // Confirm the creation of Excel file if ($workbook->easy_getError() == "") echo "File successfully created."; else echo "Error encountered: " . $workbook->easy_getError(); // Dispose memory $workbook->Dispose(); ?>

Can Excel handle 3 million rows? ›

A common myth I hear very frequently is that you can't work with more than 1 million records in Excel. Actually, the right myth should be that you can't use more than 1,048,576 rows, since this is the number of rows on each sheet; but even this one is false.

How to read and write an Excel file in PHP? ›

Download the Code and Examples​
  1. Best PHP Libraries to Parse and Write Excel Files.
  2. Install Box/Spout library.
  3. Excel (XLSX) File Example.
  4. Read an Excel File (XLSX)
  5. Write an Excel File (XLSX)
  6. Read a Large Excel File Efficiently while Using Low Memory.
Mar 11, 2022

How many rows can be written in CSV? ›

In the case of CSV, one cell is a value that is separated by delimiters. Here you will encounter a limit of 1,048,576 rows. After you reach this limit you will be warned that you're not seeing all the data. Similar to Excel, in Numbers, you'll see a warning if your file exceeds 1,000,000 lines.

Can PHP interact with Excel? ›

The CData ODBC driver for Excel enables you to create PHP applications with connectivity to Excel data. Leverage the native support for ODBC in PHP. Drop the CData ODBC Driver for Excel into your LAMP or WAMP stack to build Excel-connected Web applications.

Can I use Excel sheet as database in PHP? ›

Read the Excel file using the PHPExcel library. The PHPExcel library is a PHP library for reading and writing Excel files, and it can be used to read the data from your Excel file into PHP. Iterate through the rows of the Excel file and insert the data into the database table.

Can we read Excel file in PHP? ›

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 .

What is the PHP extension for Excel? ›

PHPExcel is a library written in pure PHP and providing a set of classes that allow you to write to and read from different spreadsheet file formats, like Excel (BIFF) . xls, Excel 2007 (OfficeOpenXML) . xlsx, CSV, Libre/OpenOffice Calc .

How to generate Excel reports in PHP with MySQL? ›

Export To Excel In PHP With MySQL
  1. Make Table in My SQL for storing the user. ...
  2. Now, insert some temporary data to this table. ...
  3. Now, first we make one simple PHP page that will display the above table data in tabular format with one “Export link”. ...
  4. Now, we have to fetch the SQL table value for displaying table data rows.
Jul 19, 2016

Can PHP read an XLSX file? ›

XLSXReader is a small PHP class for reading data from Microsoft Excel XLSX (OpenXML) files. There are some much more comprehensive Excel libraries for PHP, but I created this because I was looking for a library that made it as easy as possible to: Open an Excel file. Get a list of the sheets (names and sheetIds)

Can you use PHP in Excel? ›

PhpSpreadsheet is a library written in pure PHP and offers a set of classes that allow you to read and write various spreadsheet file formats such as Excel and LibreOffice Calc. In this tutorial, we are going learn how to read and write the xlsx file. You can integrate it with your database if you need.

How to create a csv file in PHP? ›

The fputcsv() function is used to format a line as CSV (comma separated values) file and writes it to an open file. The file which has to be read and the fields are sent as parameters to the fputcsv() function and it returns the length of the written string on success or FALSE on failure. fclose( $fp );

Top Articles
Latest Posts
Article information

Author: Zonia Mosciski DO

Last Updated:

Views: 6237

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Zonia Mosciski DO

Birthday: 1996-05-16

Address: Suite 228 919 Deana Ford, Lake Meridithberg, NE 60017-4257

Phone: +2613987384138

Job: Chief Retail Officer

Hobby: Tai chi, Dowsing, Poi, Letterboxing, Watching movies, Video gaming, Singing

Introduction: My name is Zonia Mosciski DO, I am a enchanting, joyous, lovely, successful, hilarious, tender, outstanding person who loves writing and wants to share my knowledge and understanding with you.