PHP Excel operations based on COM (PHP5)

Source: Internet
Author: User
Tags php excel
PHP Excel operation class (PHP5) based on COM, reading PHP Excel operation class (PHP5) based on COM, boring to write about it, don't forget to send me a copy of friends who like expansion, haha, thank you. ps: There is no efficiency in testing. haha ~~~ Guyudj@yahoo.com.cn to copy PHP content to the clipboard PHP code :? Php/*** Excel operation class based on COM

Don't forget to send me a copy of your friends who love expansion. thank you,

Ps: There is no efficiency in testing. haha ~~~

Guyudj@yahoo.com.cn copy PHP content to clipboard
PHP code:

/**
* Excel operations based on COM (PHP5.x)
* PHPer: T.T. R
* Date: [2007-05-24]
* Ver: 1.0.0
* Blog: [url] http://www.Gx3.cn [/url] [url] http://Gx3.cn [/url]
* QQ: 252319874
*/
Class Excel
{
Static $ instance = null;
Private $ excel = null;
Private $ workbook = null;
Private $ workbookadd = null;
Private $ worksheet = null;
Private $ worksheetadd = null;
Private $ sheetnum = 1;
Private $ cells = array ();
Private $ fields = array ();
Private $ maxrows;
Private $ maxcols;
Private $ filename;

// Constructor
Private function Excel ()
{
$ This-> excel = new COM ("Excel. Application") or die ("Did Not Connect ");
}

// Class entry
Public static function getInstance ()
{
If (null = self: $ instance)
{
Self: $ instance = new Excel ();
}
Return self: $ instance;
}

// Set the file address
Public function setFile ($ filename)
{
Return $ this-> filename = $ filename;
}

// Open the file
Public function Open ()
{
$ This-> workbook = $ this-> excel-> WorkBooks-> Open ($ this-> filename );
}

// Set Sheet
Public function setSheet ($ num = 1)
{
If ($ num> 0)
{
$ This-> sheetnum = $ num;
$ This-> worksheet = $ this-> excel-> WorkSheets [$ this-> sheetnum];
$ This-> maxcols = $ this-> maxCols ();
$ This-> maxrows = $ this-> maxRows ();
$ This-> getCells ();
}
}

// Obtain all table values and write them into the array
Private function getCells ()
{
For ($ I = 1; $ I <$ this-> maxcols; $ I ++)
{
For ($ j = 2; $ j <$ this-> maxrows; $ j ++)
{
$ This-> cells [$ this-> worksheet-> Cells (1, $ I)-> value] [] = (string) $ this-> worksheet-> Cells ($ j, $ I)-> value;
}
}
Return $ this-> cells;
}

// Returns an array of table content
Public function getAllData ()
{
Return $ this-> cells;
}

// Return the specified cell content
Public function Cell ($ row, $ col)
{
Return $ this-> worksheet-> Cells ($ row, $ col)-> Value;
}

// Obtain the table field name array
Public function getFields ()
{
For ($ I = 1; $ I <$ this-> maxcols; $ I ++)
{
$ This-> fields [] = $ this-> worksheet-> Cells (1, $ I)-> value;
}
Return $ this-> fields;
}

// Modify the cell content
Public function editCell ($ row, $ col, $ value)
{
If ($ this-> workbook = null | $ this-> worksheet = null)
{
Echo "Error: Did Not Connect! ";
} Else {
$ This-> worksheet-> Cells ($ row, $ col)-> Value = $ value;
$ This-> workbook-> Save ();
}
}

// Modify a row of data
Public function editOneRow ($ row, $ arr)
{
If ($ this-> workbook = null | $ this-> worksheet = null | $ row> = 2)
{
Echo "Error: Did Not Connect! ";
} Else {
If (count ($ arr) ==$ this-> maxcols-1)
{
$ I = 1;
Foreach ($ arr as $ val)
{
$ This-> worksheet-> Cells ($ row, $ I)-> Value = $ val;
$ I ++;
}
$ This-> workbook-> Save ();
}
}
}

// Obtain the total number of columns
Private function maxCols ()
{
$ I = 1;
While (true)
{
If (0 = $ this-> worksheet-> Cells (1, $ I ))
{
Return $ I;
Break;
}
$ I ++;
}
}

// Obtain the total number of rows
Private function maxRows ()
{
$ I = 1;
While (true)
{
If (0 = $ this-> worksheet-> Cells ($ I, 1 ))
{
Return $ I;
Break;
}
$ I ++;
}
}

// Read the row data
Public function getOneRow ($ row = 2)
{
If ($ row> = 2)
{
For ($ I = 1; $ I <$ this-> maxcols; $ I ++)
{
$ Arr [] = $ this-> worksheet-> Cells ($ row, $ I)-> Value;
}
Return $ arr;
}
}

// Close the object
Public function Close ()
{
$ This-> excel-> WorkBooks-> Close ();
$ This-> excel = null;
$ This-> workbook = null;
$ This-> worksheet = null;
Self: $ instance = null;
}
};

/*
$ Excel = new COM ("Excel. Application ");
$ Workbook = $ excel-> WorkBooks-> Open ('d: \ Apache2 \ htdocs \ wwwroot \ MyExcel.xls ');
$ Worksheet = $ excel-> WorkSheets (1 );
Echo $ worksheet-> Cells (2, 6)-> Value;
$ Excel-> WorkBooks-> Close ();
*/


$ Excel = Excel: getInstance ();
$ Excel-> setFile ("D: \ Apache2 \ htdocs \ wwwroot \ MyExcel.xls ");
$ Excel-> Open ();
$ Excel-> setSheet ();
For ($ I = 1; $ I <16; $ I ++)
{
$ Arr [] = $ I;
}
// $ Excel-> editOneRow (2, $ arr );
Print_r ($ excel-> getAllData ());
$ Excel-> Close ();

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.