Php-implemented excel operations and phpexcel operations. Php-implemented Operation excel class details, phpexcel class detailed examples of this article describes the php-implemented Operation excel class. For your reference, see phpclassExcel {static $ inst php operations excel and phpexcel.
This example describes the excel class implemented by php. We will share this with you for your reference. The details are as follows:
<? Phpclass 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);} // sets Sheet public function setSheet ($ num = 1) {if ($ num> 0) {$ this-> sheetnum = $ num; $ this-> work Sheet = $ 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 ;}// return the table content array public fun Ction getAllData () {return $ this-> cells;} // return the public function Cell ($ row, $ col) that specifies the Cell content) {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 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 public function getOneRow ($ row = 2) of the row data) {if ($ row >=2) {for ($ I = 1; $ I <$ this-> maxcols; $ I ++) {$ arr [] = $ this-> worksheet-> Cells ($ row, $ I)-> Value;} return $ arr ;}} // Close the 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 ()-> Value; $ excel-> WorkBooks-> Close (); */$ excel = Excel: getInstance (); $ excel-> setFile ("D: // kaka.xls"); $ excel-> Open (); $ excel-> setSheet (); for ($ I = 1; $ I <16; $ I ++) {$ arr [] = $ I ;} // $ excel-> editOneRow (2, $ arr); // print_r ($ excel-> getAllData (); $ str = $ excel-> getAllData (); include_once ('mail. class. php '); $ smtpserver = "smtp.yeah.net"; $ smtpserverport = 25; $ smtpuseremail = "yanqihu58@yeah.net"; $ smtpemailto = "yanqihu@139.com"; $ smtpuser = "yanqihu58 "; $ smtppwd = "123456789"; $ mailtype = "HTML"; $ smtp = new smtp ($ smtpserver, $ smtpserverport, true, $ smtpuser, $ smtppwd ); $ message = "Hello"; // $ message. = "homepage connection address :". $ this-> link_url."
"; // $ Message. =" email: ". $ this-> link_email ."
"; // $ Message. =" business contact QQ: ". $ this-> link_qq ."
"; // $ Message. =" business phone QQ: ". $ this-> link_tel ."
"; // $ Message. =" Contact: ". $ this-> link_people ."
"; $ Smtp-> debug = false; foreach ($ str ['email '] as $ key => $ value) {$ smtpemailto = $ value; @ $ smtp-> sendmail ($ smtpemailto, $ smtpuseremail, $ mailsubject, $ message, $ mailtype); exit ;}// exit; $ excel-> Close ();?>