Support Center

How to use GemBox.Spreadsheet in PHP

GemBox.Spreadsheet is a library for the .NET framework that enables developers to manipulate spreadsheet files simply and efficiently. It enables reading, writing, converting and printing of various spreadsheet file formats (XLSX, XLS, ODS, CSV, HTML, PDF and XPS).

GemBox.Spreadsheet can also be used from the PHP framework. PHP framework provides a DOTNET class with which we can create an instance of a .NET class. After creating a DOTNET object we can use .NET class members (methods, properties, etc.) from our PHP code.

This article will show the steps for using the GemBox.Spreadsheet library from PHP code.

 

System Requirements:

  1. PHP Framework, version 4.1 or above.
  2. .NET Framework, version 3.0 or above.
  3. GemBox.Spreadsheet for .NET Framework 3.0 - 4.6.

 

Installation Guide:

  1. Download and install the latest version of GemBox.Spreadsheet from this page.
  2. Open the Command Prompt (CMD) as Administrator (see here how to do this).
  3. Register GemBox.Spreadsheet to COM by running the following command in CMD:

    "<path to RegAsm.exe>" "<path to GemBox.Spreadsheet.dll>"

    E.g:

    "C:\Windows\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe" "C:\Program Files (x86)\GemBox Software\GemBox.Spreadsheet 3.9\Bin\NET3X4X\GemBox.Spreadsheet.dll"

  4. Enable usage of DOTNET in php.ini:

    [COM_DOT_NET]
    extension=php_com_dotnet.dll

Now we can start using GemBox.Spreadsheet through PHP code.

 

Sample 1: Create an Excel file in PHP

The following sample demonstrates how we can create a new Excel file:

<?php
  // GemBox.Spreadsheet's full name.
  // NOTE: If you're using a newer version you'll need to adjust the Version's number below.
  $gbs_full_name = "GemBox.Spreadsheet, Version=39.3.30.1163, Culture=neutral, PublicKeyToken=b1b72c69714d4847";
  
  // Create ComHelper object.
  $com_helper = new DOTNET($gbs_full_name, "GemBox.Spreadsheet.ComHelper");
  // Set license.
  // NOTE: If you're using a Professional version you'll need to put your serial key below.
  $com_helper->ComSetLicense("FREE-LIMITED-KEY");
  
  // Create new excel file.
  $workbook = new DOTNET($gbs_full_name, "GemBox.Spreadsheet.ExcelFile");
  // Create new sheet.
  $worksheet = $workbook->Worksheets->Add("Hello World Sheet");
  // Get A1 cell.
  $cell = $worksheet->Cells->Item(0);
  // Set A1 cell's value.
  $cell->Value = "Hello World";
  
  // Save "HelloWorld.xlsx" file.
  $file_name = "HelloWorld.xlsx";
  $file_path = getcwd() . "\\" . $file_name;
  $workbook->Save($file_path);
  
  // Download "HelloWorld.xlsx" file.
  $type = filetype($file_path);
  header("Content-type: $type");
  header("Content-Disposition: attachment;filename=$file_name");
  readfile($file_path);
?>

The resulting HelloWorld.xlsx file:

 

Sample 2: Read and Write Excel file in PHP

The following sample demonstrates how we can load an existing Excel file, update it with some new data, and save it as a PDF file:

<?php
  // GemBox.Spreadsheet's full name.
  // NOTE: If you're using a newer version you'll need to adjust the Version's number below.
  $gbs_full_name = "GemBox.Spreadsheet, Version=39.3.30.1163, Culture=neutral, PublicKeyToken=b1b72c69714d4847";
  
  // Create ComHelper object.
  $com_helper = new DOTNET($gbs_full_name, "GemBox.Spreadsheet.ComHelper");
  // Set license.
  // NOTE: If you're using a Professional version you'll need to put your serial key below.
  $com_helper->ComSetLicense("FREE-LIMITED-KEY");
    
  // Load excel file.
  $workbook = $com_helper->Load(getcwd() . "\\Input.xlsx");
  // Get first sheet.
  $worksheet = $workbook->Worksheets->Item(0);
  
  // Create new data.
  $new_data = array("Bob Garvey"=>1000, "Ben Stilwell"=>2000, "Peter Pan"=>3000);
  // Get cells from "A6" to "B8".
  $cells_range = $worksheet->Cells->GetSubrange("A6", "B8");
  // Update cells with new data.
  $cell_index = 0;
  foreach($new_data as $name => $salery) {
    $cells_range->Item($cell_index++)->Value = $name;
    $cells_range->Item($cell_index++)->Value = $salery;
  }
  
  // Save "Output.pdf" file.
  $file_name = "Output.pdf";
  $file_path = getcwd() . "\\" . $file_name;
  $workbook->Save($file_path);
  
  // Download "Output.pdf" file.
  $type = filetype($file_path);
  header("Content-type: $type");
  header("Content-Disposition: attachment;filename=$file_name");
  readfile($file_path);
?>

Used input Input.xlsx file:

The resulting Output.pdf file:

Subscribe to this article to get an email notification when it is updated.

2 Comments

  • This requires installation of dotnet on each end user. Correct? So it is limited.
  • Hi,

    No, the .NET Framework is not required on clients (your end users).
    It is only required on server (where your PHP code is executed).

    Regards,
    Mario