Create, read, and write Excel files from PHP

GemBox.Spreadsheet is a .NET library for processing Excel files but since it's also a COM-accessible library, you can use it in PHP.

System Requirements

To use GemBox.Spreadsheet in PHP, you'll need to:

  1. Download and install GemBox.Spreadsheet Setup.
  2. Expose GemBox.Spreadsheet to COM Interop with Regasm.exe tool:
    :: Add GemBox.Spreadsheet to COM registry for x86 (32-bit) applications.
    C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe [path to installed GemBox.Spreadsheet.dll]
    :: Add GemBox.Spreadsheet to COM registry for x64 (64-bit) applications.
    C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe [path to installed GemBox.Spreadsheet.dll]

Working with Excel files in PHP

The following example shows how you can create a new Excel file from PHP and write some spreadsheet data into its cells. Also, the example shows how you can read an existing Excel file and update its cells.

  // Create ComHelper object.
  $comHelper = new Com("GemBox.Spreadsheet.ComHelper", null, CP_UTF8);
  // If using the Professional version, put your serial key below.

  *** Create Excel ***

  // Create new ExcelFile object.
  $workbook = new Com("GemBox.Spreadsheet.ExcelFile", null, CP_UTF8);
  // Add new ExcelWorksheet object.
  $worksheet = $workbook->Worksheets->Add("Sheet1");

  // Set width and format of column "A".
  $columnA = $comHelper->GetColumn($worksheet, 0);
  $columnA->Width = 20 * 256;
  $columnA->Style->Font->Weight = 700;

  // Set values of cells "A1", "A2", "A3" and "A4".
  $columnA->Cells->Item(0)->Value = "John Doe";
  $columnA->Cells->Item(1)->Value = "Bob Garvey";
  $columnA->Cells->Item(2)->Value = "Ben Stilwell";
  $columnA->Cells->Item(3)->Value = "Peter Pan";
  // Set values of cells "B1", "B2", "B3" and "B4".
  $columnB = $comHelper->GetColumn($worksheet, 1);
  $columnB->Cells->Item(0)->Value = 1000;
  $columnB->Cells->Item(1)->Value = 2000;
  $columnB->Cells->Item(2)->Value = 3000;
  $columnB->Cells->Item(3)->Value = 4000;

  // Create new Excel file.
  $workbook->Save(getcwd() . "\\New.xlsx");

  *** Read Excel ***

  // Read existing Excel file.
  $book = $comHelper->Load(getcwd() . "\\New.xlsx");
  // Get first Excel sheet.
  $sheet = $book->Worksheets->Item(0);
  // Get first Excel row.
  $row1 = $comHelper->GetRow(sheet, 0);

  // Display values of cells "A1" and "B1".
  echo "Cell A1:" . $row1->Cells->Item(0)->Value;
  echo "<br>";
  echo "Cell B1:" . $row1->Cells->Item(1)->Value;

  *** Update Excel ***

  // Update values of cells "A1" and "B1".
  $row1->Cells->Item(0)->Value = "Jane Doe";
  $row1->Cells->Item(1)->Value = 2000;

  // Write the updated Excel file.
  $book.Save(getcwd() . "\\Updated.xlsx");
Reading and writing spreadsheet data from new and existing Excel files in PHP
Screenshot of a newly created and then updated Excel file

Wrapper Library

Not all members of GemBox.Spreadsheet are accessible because of the COM limitations like unsupported static and overload methods. That is why you can use the ComHelper class which provides alternatives for some members that cannot be called with COM Interop.

However, if you need to use many GemBox.Spreadsheet members from PHP, a recommended approach is to create a .NET wrapper library instead. Your wrapper library should do all the work within and exposes a minimal set of classes and methods to the unmanaged code.

This will enable you to take advantage of GemBox.Spreadsheet's full capabilities, avoid any COM limitations, and improve performance by reducing the number of COM Callable Wrappers created at runtime.

See also

Next steps

GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Download Buy