PHP Classes

Excel: Manipulate Excel files using the COM interface

Recommend this page to a friend!
  Info   View files Example   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum (6)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 62%Total: 34,467 All time: 14 This week: 123Down
Version License PHP version Categories
samounexcel 1.0.0Unspecified (ask ...3Tools, Files and Folders


Implements the basic functionalities of Excel in PHP4 using the COM interface.

Picture of alain samoun
Name: alain samoun <contact>
Classes: 3 packages by
Country: ???
Age: ???
All time rank: 8
Week rank: 530 Down


/* Test for Excel.php: A class for use with PHP4 scripts only*/

 * This is a test file for Excel class to create,load,read,write,save and use some of the
 * internal functionalities of workbooks and sheets.
 * Tested with Windows 98 - MS Office 2000
 * Apache 1.3.9 PHP4.02 Running as CGI
 * (c) Alain M. Samoun 09/2000.
 * Gnu GPL code (see for more information ).
 /* Set up files and directories for the test. Change as you wish.*/
 # Put the include file in your php include directory
require ("excel.php");
# Put the test.xls file in the C:\My Documents\ directory
$workbook = "test.xls";
$pathin = "C:\\My Documents\\";
$sheet = "test";
$pathout = "c:\\My Documents\\"; #The directory to save your files

 /* Instantiate Excel and open the test file. */

 #Instantiate Excel
$E = new Excel;

#Open the workbook
"Test starting..<br>";

/* Read the content of a cell or range of cells */

  # Read the A1 cell
Print "The content of cell A1 is: ". $E->readrange($sheet,"A1")."<br>";
# Read the content of cells A1,A2,A3,A4
$content = $E->readrange($sheet,"A1:A4");
        if (
$count = sizeof($content);
            for (
$num=0; $num<$count; $num++)
"Got Cell A$num2 Content = ". $content[$num] . "<br>";
/* Write to a cell or range of cells */
  # Change the content of cell A1
$E->writerange($sheet,"A1","New content");
# Read the new content
Print "The content of cell A1 now is: ". $E->readrange($sheet,"A1")."<br>";
# Write an array of numbers to the cells B1,C1,D1,E1
$array = array("1","2","3","4");
# Read the new content for the range:
$content = $E->readrange($sheet,"B1:E1");
       if (
$count = sizeof($content);
        for (
$num=0; $num<$count; $num++)
$letter= chr(66+$num);
"Set cell $letter"."1 Content= ". $content[$num] . "<br>";
/* Write a formula in cell B3 */
# Read the B3 content
Print "The content of cell B3=SUM(B1..D1) is: ". $E->readrange($sheet,"B3")."<br>";

/* Execute a macro */
 #The following will run the macro Macro2 that will put a string on cell D6
 #content of cell D6 before macro
Print"Cell D6 before macro:". $E->readrange($sheet,"D6")." <br>";
#Read the macro result:
$result = $E->readrange($sheet,"D6");
"Cell D6 content after running Macro2 = $result <br>";
/* Read the result of a build-in excel function */

 #Example: PMT financial function
$arrayparam = array("0.08/12","10","10000");
$result = $E->runfunction("PMT",$arrayparam);
"<br>Financial function PMT result using the \"run function \" method: ".sprintf("%.2f",$result);

/* Convert all files in a directory */
/* (Uncoment to run this part)
 #File to convert from extension "xls" to extension "csv"
     if ($E->XLTranslate($pathin,$pathout,"xls","csv",0))
         print "<br>All 'xls' files in $pathin have been converted to 'csv files in $pathout.<br>";
 /* Save the workbook */

 #Save the current workbook as a quattro pro file:
"<br> $workbook has been saved as a WQ1 file. <br>";

/* Close workbook and exit excel */
     unset (
/* Start a new instance of excel with a new workbook */
$E = new Excel;
$E->XL("",$pathin,"sheet1"); #Note the empty name for workbook
Print "<br>Starting a new workbook<br>";
# Write something in cell A1
$E->writerange("sheet1","A1","Cell A1 in the new workbook");
# Read the new content
Print "The content of cell A1 in the new workbook is: ". $E->readrange("sheet1","A1")."<br>";
# Save the new workbook as an xls file
# And close it
         unset (

"<br> Test finished!";

  Files folder image Files  
File Role Description
Accessible without login Plain text file About_testing_this_class.txt Doc. Text file
Plain text file excel.php Class Excel class for PHP4
Accessible without login Plain text file excelclass.txt Doc. Text file
Accessible without login Plain text file test.xls Data Excel test file
Accessible without login Plain text file testclass.php Example Test the methods of Excel class.

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:14
This week:123Down
User Ratings User Comments (4)
 All time
Thanks a lot
11 years ago (Leon Nguyen)
11 years ago (Fernando Castro Saenz)
Thanks! Great!
15 years ago (Benjamin Langlotz)
15 years ago (Joe M)