Link Search Menu Expand Document

Create Spreadsheet - PHP

Spreadsheet SDK sample in PHP demonstrating ‘Create Spreadsheet’


// this PHP script creates XLS file using Spreadsheet SDK and streams it into the browser for user to download


to use COM objects in PHP need to enable it by adding the following line into php.ini:


If you are usiung EasyPHP DevServer then to edit php.ini just right-click on the tray icon and select "Configuration"->"PHP"


// create spreadsheet object
$document = new COM("Bytescout.Spreadsheet.Spreadsheet");

// Add new worksheet
$worksheet = $document->Workbook->Worksheets->Add("123HelloWorld");

// get cell value
$cell = $worksheet->Cell(0,0); // you can also use worksheet.Cell_2("A1") as well. Cell_2() is because all overloads are accessible via _Index postfix, like .Cell_2(), .Cell_3() etc

// set cell value
$cell->Value = "Hello, World!";

// generate and get xls document as an array of bytes
$XLSDocumentArray = $document->GetAsBytesArrayXLS();

// now set headers to properly tell what kind of content we stream into browser

// set file name to save into
header("Content-Disposition: attachment; filename=\"HelloWorld.xls\"");

// now write the array with content into the output 
foreach ($XLSDocumentArray as $byte) echo chr($byte);

catch(Exception $err) // catch exception if any
header("Content-Type: text/plain"); // switch to plain text output type
echo($err->getMessage()); // write error message to the output

You may use the SDK from PHP on Windows using DOTNET() class in PHP

1) Install the DLL into the GAC (global assembly cache)

 a) Open "c:\Windows\assembly" in Windows Explorer and drag-and-drop the dll into this folder
 b) install via command line:

"%programfiles%\Microsoft SDKs\Windows\v6.0\bin\gacutil.exe" -i "Bytescout.Spreadsheet.dll"

NOTE: you may need to change %programfiles%\Microsoft SDKs\Windows\v6.0\bin\ into 
%programfiles%\Microsoft SDKs\Windows\v6.0A\bin\
or into
%programfiles%\Microsoft SDKs\Windows\v7.0A\bin\

If you are still getting "File not found" then run the search for gacutil.exe in "Program Files" folder to find it.

2) Now run Windows Explorer and open "C:\WINDOWS\assembly" to view installed .NET assemblies in the GAC to verify if the dll was installed into the GAC
3) Find the installed and right-click on it and select "Properties"
4) In the Properties window double-click on "Assembly Name" and copy it to your PHP code (see below)
5) In the Properties window double-click on "Public Key Token" and copy it to your PHP code (see below)
6) In the Properties window double-click on "Version" value and copy it to your PHP code (see below)

from (4), (5) and (6) you should compose the following string:

$dotnetobj = new DOTNET("ASSEMBLYNAME, Version=VERSIONVALUE, Culture=neutral, PublicKeyToken=PUBLICKEYVALUE", 

ASSEMBLYNAME = the assembly name  (like Bytescout.Spreadsheet)
VERSIONVALUE = version value you got on step (5)
PUBLICKEYVALUE = public key value you got on step (4)
CLASSNAME = class name from the dll you want to create (for example, "Spreadsheet")

Example for Spreadsheet SDK version

$dotnetobj = new DOTNET("Bytescout.Spreadsheet, Version=, Culture=neutral, PublicKeyToken=f7dd1bd9d40a50eb", 

Then you may use $dotnetobj and call its methods like this:
$dotnetobj->SomeMethod() (see the documentation of the SDK for available classes and methods and properties)

Download Source Code (.zip)

Return to the previous page Explore Spreadsheet SDK