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 相关文章推荐
从MySQL数据库表中取出随机数据的代码
Sep 05 PHP
PHP 采集程序 常用函数
Dec 18 PHP
探讨如何在PHP开启gzip页面压缩实例
Jun 09 PHP
php缓冲 output_buffering的使用详解
Jun 13 PHP
PHP根据传入参数合并多个JS和CSS文件的简单实现
Jun 13 PHP
php之readdir函数用法实例
Nov 13 PHP
迪菲-赫尔曼密钥交换(Diffie?Hellman)算法原理和PHP实现版
May 12 PHP
PHP使用strtotime获取上个月、下个月、本月的日期
Dec 30 PHP
thinkPHP中钩子的两种配置调用方法详解
Nov 11 PHP
浅谈PHP的反射机制
Dec 15 PHP
Thinkphp 5.0实现微信企业付款到零钱
Sep 30 PHP
YII2框架中ActiveDataProvider与GridView的配合使用操作示例
Mar 18 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中func_get_args(),func_get_arg(),func_num_args()的区别
2013/09/30 PHP
js和php邮箱地址验证的实现方法
2014/01/09 PHP
Codeigniter整合Tank Auth权限类库详解
2014/06/12 PHP
学习php中的正则表达式
2014/08/17 PHP
PHP实现恶意DDOS攻击避免带宽占用问题方法
2015/05/27 PHP
php设计模式之备忘模式分析【星际争霸游戏案例】
2020/03/24 PHP
json简单介绍
2008/06/10 Javascript
js 图片缩放(按比例)控制代码
2009/05/27 Javascript
JQuery的html(data)方法与&amp;lt;script&amp;gt;脚本块的解决方法
2010/03/09 Javascript
JS模拟自动点击的简单实例
2013/08/08 Javascript
js脚本获取webform服务器控件的方法
2014/05/16 Javascript
基于jquery实现简单的分页控件
2016/03/17 Javascript
JS组件系列之Bootstrap table表格组件神器【二、父子表和行列调序】
2016/05/10 Javascript
wap手机端解决返回上一页的js实例
2016/12/08 Javascript
jQuery validate插件功能与用法详解
2016/12/15 Javascript
利用jQuery解析获取JSON数据
2017/04/08 jQuery
微信小程序onLaunch异步,首页onLoad先执行?
2018/09/20 Javascript
vue项目前端错误收集之sentry教程详解
2019/05/27 Javascript
vue.js 2.0实现简单分页效果
2019/07/29 Javascript
vue quill editor 使用富文本添加上传音频功能
2020/01/14 Javascript
ES6中的Javascript解构的实现
2020/10/30 Javascript
一则python3的简单爬虫代码
2014/05/26 Python
Python中的Descriptor描述符学习教程
2016/06/02 Python
Django框架用户注销功能实现方法分析
2019/05/28 Python
Django Aggregation聚合使用方法解析
2019/08/01 Python
解决pytorch GPU 计算过程中出现内存耗尽的问题
2019/08/19 Python
Django 自动生成api接口文档教程
2019/11/19 Python
Python tkinter和exe打包的方法
2020/02/05 Python
意大利网上书店:LaFeltrinelli
2020/06/12 全球购物
酷瑞网络科技面试题
2012/03/30 面试题
社团文化节邀请函
2014/01/10 职场文书
仲裁协议书
2014/09/26 职场文书
2014年宣传部个人工作总结
2014/12/06 职场文书
教师正风肃纪心得体会
2016/01/15 职场文书
高考升学宴主持词
2019/06/21 职场文书
Redis实现订单自动过期功能的示例代码
2021/05/08 Redis