PHP implementation of the operation of the Excel class, Phpexcel class detailed _php tutorial

Source: Internet
Author: User
Tags php excel

PHP implementation of the operation of the Excel class detailed, Phpexcel class


The example in this article describes the operation of the PHP implementation of the Excel class. Share to everyone for your reference, 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 ("Do not Connect");    }//Class Entry public static function getinstance () {if (null = = self:: $instance) {self:: $instance = new Excel ();  } return Self:: $instance;  }//Set file address Public function Setfile ($filename) {return $this->filename= $filename;  }//Open 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= $thisMaxcols ();      $this->maxrows= $this->maxrows ();    $this->getcells (); }}//Get all values of the table and write into the array private function Getcells () {for ($i =1; $i < $this->maxcols; $i + +) {for ($j =2; $j < $th is->maxrows; $j + +) {$this->cells[$this->worksheet->cells (1, $i)->value][]= (string) $this->work      Sheet->cells ($j, $i)->value;  }} return $this->cells;  }//Returns the table Content array public Function Getalldata () {return $this->cells;  }//Returns the contents of the Public Function cell ($row, $col) {return $this->worksheet->cells ($row, $col)->value; }//Get table field an array group Public function GetFields () {for ($i =1; $i < $this->maxcols; $i + +) {$this->fields[]= $thi    S->worksheet->cells (1, $i)->value;  } return $this->fields;     }//Modify to make cell contents 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 one row of data public function Editonerow ($row, $arr) {if ($this->workbook==null | | $this->worksheet==null | | $r    ow>=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 ();    }}}//Get total number of columns Private function Maxcols () {$i = 1;        while (true) {if (0== $this->worksheet->cells (1, $i)) {return $i;      Break    } $i + +;    }}//Get Total Row number Private function MaxRows () {$i = 1;        while (true) {if (0== $this->worksheet->cells ($i, 1)) {return $i;      Break    } $i + +;      }}//Read make row data public function Getonerow ($row =2) {if ($row >=2) {for ($i =1; $i < $this->maxcols; $i + +) {$arr []= $this-&Gt;worksheet->cells ($row, $i)->value;    } return $arr;    }}//Close 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://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. = "Home connection address:". $this->link_url. "
"; $message. = "E-mail:". $this->link_email. "
"; $message. = "Business Contact QQ:". $this->link_qq. "
"; $message. = "Business phone QQ:" $this->link_tel. "
"; $message. = "Contacts:" $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 ();? >

Read more about PHP operations in Excel related content readers can view this site: "PHP operation Office Document tips summary (including word,excel,access,ppt)"

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • Introduction to PHP Excel Class Phpexcel usage method
    • thinkphp exporting CSV files and using tables to output Excel
    • How PHP uses Phpexcel to import and export data
    • PHP Import Export Excel code
    • PHP Export Chinese content Excel file class instance
    • Generate an Excel file in PHP to a specified directory
    • Summary of some common methods in Phpexcel
    • How to import Excel files into MySQL database by PHP
    • Workaround for PHP to generate Excel column names more than 26 columns greater than Z
    • Ways to export Excel using Phpexcel in Yii
    • thinkphp How to import Excel files based on Phpexcel

http://www.bkjia.com/PHPjc/1092842.html www.bkjia.com true http://www.bkjia.com/PHPjc/1092842.html techarticle PHP Implementation of the operation of the Excel class, Phpexcel class detailed examples of this article describes the PHP implementation of the operation of the Excel class. Share to everybody for reference, concrete as follows: Phpclass excel{static $inst ...

  • 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.