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 相关文章推荐
十天学会php之第十天
Oct 09 PHP
PHP 模拟登陆MSN并获得用户信息
May 16 PHP
php实现水仙花数的4个示例分享
Apr 08 PHP
9段PHP实用功能的代码推荐
Oct 14 PHP
推荐10个提供免费PHP脚本下载的网站
Dec 31 PHP
php单例模式实现方法分析
Mar 14 PHP
PHP查询并删除数据库多列重复数据的方法(利用数组函数实现)
Feb 23 PHP
PHP实现打包下载文件的方法示例
Oct 07 PHP
PHP date()格式MySQL中插入datetime方法
Jan 29 PHP
java解析json方法总结
May 16 PHP
在Laravel5中正确设置文件权限的方法
May 22 PHP
如何在PHP中读写文件
Sep 07 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数组(array)输出的三种形式详解
2013/06/05 PHP
ThinkPHP调用common/common.php函数提示错误function undefined的解决方法
2014/08/25 PHP
基于递归实现的php树形菜单代码
2014/11/19 PHP
PHP实现抓取HTTPS内容
2014/12/01 PHP
php实现读取内存顺序号
2015/03/29 PHP
jquery 最简单的属性菜单
2009/10/08 Javascript
Google (Local) Search API的简单使用介绍
2013/11/28 Javascript
解释&amp;&amp;和||在javascript中的另类用法
2014/07/28 Javascript
jQuery浏览器CSS3特写兼容实例
2015/01/19 Javascript
基于JavaScript实现仿京东图片轮播效果
2015/11/06 Javascript
js控制文本框只能输入中文、英文、数字与指定特殊符号的实现代码
2016/09/09 Javascript
JavaScript学习笔记整理_setTimeout的应用
2016/09/19 Javascript
完美的js div拖拽实例代码
2016/09/24 Javascript
bootstrap导航、选项卡实现代码
2016/12/28 Javascript
vue.js国际化 vue-i18n插件的使用详解
2017/07/07 Javascript
javaScript日期工具类DateUtils详解
2017/12/08 Javascript
微信小程序实现长按删除图片的示例
2018/05/18 Javascript
浅谈node中的cluster集群
2018/06/02 Javascript
React如何解决fetch跨域请求时session失效问题
2018/11/02 Javascript
一份超级详细的Vue-cli3.0使用教程【推荐】
2018/11/15 Javascript
30分钟快速实现小程序语音识别功能
2018/11/27 Javascript
layui扩展上传组件模拟进度条的方法
2019/09/23 Javascript
浅谈vue中get请求解决传输数据是数组格式的问题
2020/08/03 Javascript
Vue 打包的静态文件不能直接运行的原因及解决办法
2020/11/19 Vue.js
python中的yield使用方法
2014/02/11 Python
Python快速从注释生成文档的方法
2016/12/26 Python
python实现字符串连接的三种方法及其效率、适用场景详解
2017/01/13 Python
疯狂上涨的Python 开发者应从2.x还是3.x着手?
2017/11/16 Python
Python字符串的一些操作方法总结
2019/06/10 Python
opencv python 图片读取与显示图片窗口未响应问题的解决
2020/04/24 Python
matplotlib交互式数据光标mpldatacursor的实现
2021/02/03 Python
瑞典灯具和照明网上商店:Lamp24.se
2018/03/17 全球购物
一些关于MySql加速和优化的面试题
2014/01/30 面试题
PHP使用Redis队列执行定时任务实例讲解
2021/03/24 PHP
安全伴我行演讲稿
2014/09/04 职场文书
商品陈列协议书
2014/09/29 职场文书