php实现的操作excel类详解


Posted in PHP onJanuary 15, 2016

本文实例讲述了php实现的操作excel类。分享给大家供大家参考,具体如下:

<?php
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;
  //构造函数
  private function Excel()
  {
    $this->excel = new COM("Excel.Application") or die("Did Not Connect");
  }
  //类入口
  public static function getInstance()
  {
    if(null == self::$instance)
    {
      self::$instance = new Excel();
    }
    return self::$instance;
  }
  //设置文件地址
  public function setFile($filename)
  {
    return $this->filename=$filename;
  }
  //打开文件
  public function Open()
  {
    $this->workbook=$this->excel->WorkBooks->Open($this->filename);
  }
  //设置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();
    }
  }
  //取得表所有值并写进数组
  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;
  }
  //返回表格内容数组
  public function getAllData()
  {
    return $this->cells;
  }
  //返回制定单元格内容
  public function Cell($row,$col)
  {
    return $this->worksheet->Cells($row,$col)->Value;
  }
  //取得表格字段名数组
  public function getFields()
  {
    for($i=1;$i<$this->maxcols;$i++)
    {
      $this->fields[]=$this->worksheet->Cells(1,$i)->value;
    }
    return $this->fields;
  }
  //修改制定单元格内容
  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();
    }
  }
  //修改一行数据
  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();
      }
    }
  }
  //取得总列数
  private function maxCols()
  {
    $i=1;
    while(true)
    {
      if(0==$this->worksheet->Cells(1,$i))
      {
        return $i;
        break;
      }
      $i++;
    }
  }
  //取得总行数
  private function maxRows()
  {
    $i=1;
    while(true)
    {
      if(0==$this->worksheet->Cells($i,1))
      {
        return $i;
        break;
      }
      $i++;
    }
  }
  //读取制定行数据
  public function getOneRow($row=2)
  {
    if($row>=2)
    {
      for($i=1;$i<$this->maxcols;$i++)
      {
        $arr[]=$this->worksheet->Cells($row,$i)->Value;
      }
      return $arr;
    }
  }
  //关闭对象
  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="你好";
   //$message.="首页连接地址为:".$this->link_url."<br>";
   //$message.="电子邮箱为:".$this->link_email."<br>";
   //$message.="商务联系QQ:".$this->link_qq."<br>";
   //$message.="商务电话QQ:".$this->link_tel."<br>";
   //$message.="联系人:".$this->link_people."<br>";
    $smtp->debug=false;
    foreach($str['email'] as $key=>$value){
      $smtpemailto=$value;
      @$smtp->sendmail($smtpemailto,$smtpuseremail,$mailsubject,$message,$mailtype);
      exit;
    }
    //exit;
$excel->Close();
?>

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
解决控件遮挡问题:关于有窗口元素和无窗口元素
Jan 28 PHP
完美解决dedecms中的[html][/html]和[code][/code]问题
Mar 20 PHP
在Windows系统上安装PHP运行环境文字教程
Jul 19 PHP
PHP面向对象学习笔记之二 生成对象的设计模式
Oct 06 PHP
记录PHP错误日志 display_errors与log_errors的区别
Oct 09 PHP
深入PHP异步执行的详解
Jun 03 PHP
解析PHP中DIRECTORY_SEPARATOR,PATH_SEPARATOR两个常量的作用
Jun 21 PHP
php读取本地文件常用函数(fopen与file_get_contents)
Sep 09 PHP
php中数字、字符与对象判断函数用法实例
Nov 26 PHP
php比较相似字符串的方法
Jun 05 PHP
PHP实现获取文件后缀名的几种常用方法
Aug 08 PHP
php+mysql+jquery实现日历签到功能
Feb 27 PHP
php实现的xml操作类
Jan 15 #PHP
PHP基于单例模式实现的数据库操作基类
Jan 15 #PHP
Linux安装配置php环境的方法
Jan 14 #PHP
PHP实现QQ登录实例代码
Jan 14 #PHP
PHP实现图片不变型裁剪及图片按比例裁剪的方法
Jan 14 #PHP
详解HTTP Cookie状态管理机制
Jan 14 #PHP
在php中设置session用memcache来存储的方法总结
Jan 14 #PHP
You might like
php中的静态变量的基本用法
2014/03/20 PHP
php截取字符串函数substr,iconv_substr,mb_substr示例以及优劣分析
2014/06/10 PHP
Laravel重写用户登录简单示例
2016/10/08 PHP
PHP实现抽奖功能实例代码
2020/06/30 PHP
JavaScript中的闭包原理分析
2010/03/08 Javascript
DWZ table的原生分页浅谈
2013/03/01 Javascript
jQuery之排序组件的深入解析
2013/06/19 Javascript
JS中把字符转成ASCII值的函数示例代码
2013/11/21 Javascript
对 jQuery 中 data 方法的误解分析
2014/06/18 Javascript
JQuery操作textarea,input,select,checkbox方法
2015/09/02 Javascript
微信小程序 SocketIO 实例讲解
2016/10/13 Javascript
JavaScript仿微博发布信息案例
2016/11/16 Javascript
AngularJs表单校验功能实例代码
2017/02/09 Javascript
js 单引号替换成双引号,双引号替换成单引号的实现方法
2017/02/16 Javascript
10个经典的网页鼠标特效代码
2018/01/09 Javascript
Vue页面骨架屏的实现方法
2018/05/22 Javascript
ES6 系列之 Generator 的自动执行的方法示例
2018/10/19 Javascript
解决layer.open弹出框不能获取input框的值为空的问题
2019/09/10 Javascript
Vue.js下拉菜单组件使用方法详解
2019/10/19 Javascript
[00:59]DOTA2荣耀之路1:Doom is back!weapon X!
2018/05/22 DOTA
[00:49]完美世界DOTA2联赛10月28日开团时刻:随便打
2020/10/29 DOTA
python列表操作使用示例分享
2014/02/21 Python
python解析xml文件操作实例
2014/10/05 Python
Python实现根据IP地址和子网掩码算出网段的方法
2015/07/30 Python
Python实现希尔排序算法的原理与用法实例分析
2017/11/23 Python
对python:threading.Thread类的使用方法详解
2019/01/31 Python
Tensorflow不支持AVX2指令集的解决方法
2020/02/03 Python
python时间与Unix时间戳相互转换方法详解
2020/02/13 Python
Python实现播放和录制声音的功能
2020/08/12 Python
python实现经典排序算法的示例代码
2021/02/07 Python
移动端rem布局的两种实现方法
2018/01/03 HTML / CSS
HTML5 3D旋转相册的实现示例
2019/12/03 HTML / CSS
环保倡议书500字
2014/05/15 职场文书
群众路线教师自我剖析材料
2014/09/29 职场文书
python基础学习之递归函数知识总结
2021/05/26 Python
vue使用Google Recaptcha验证的实现示例
2021/08/23 Vue.js