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 magic_quotes_gpc的一点认识与分析
Aug 18 PHP
PHP 第二节 数据类型之数组
Apr 28 PHP
浅析PHP 按位与或 (^ 、&amp;)
Jun 21 PHP
php合并js请求的例子
Nov 01 PHP
php中rename函数用法分析
Nov 15 PHP
PHP生成压缩文件实例
Feb 07 PHP
Codeigniter校验ip地址的方法
Mar 21 PHP
PHP实现递归复制整个文件夹的类实例
Aug 03 PHP
使用Thinkphp框架开发移动端接口
Aug 05 PHP
PHP最常用的正则表达式
Feb 13 PHP
php静态成员方法和静态的成员属性的使用方法
Oct 26 PHP
PHP基于SPL实现的迭代器模式示例
Apr 22 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源代码
2006/10/09 PHP
玩转虚拟域名◎+ .
2006/10/09 PHP
透析PHP的配置文件php.ini
2006/10/09 PHP
2个自定义的PHP in_array 函数,解决大量数据判断in_array的效率问题
2014/04/08 PHP
PHP使用imagick扩展实现合并图像的方法
2017/04/25 PHP
php安装扩展mysqli的实现步骤及报错解决办法
2017/09/23 PHP
php封装db类连接sqlite3数据库的方法实例
2017/12/19 PHP
php 调用百度sms来发送短信的实现示例
2018/11/02 PHP
jQuery 处理网页内容的实现代码
2010/02/15 Javascript
分享33个jQuery与CSS3实现的绚丽鼠标悬停效果
2014/12/15 Javascript
原生Js实现简易烟花爆炸效果的方法
2015/03/20 Javascript
jQuery实现的右下角广告窗体跟随效果示例
2016/09/16 Javascript
angularjs+bootstrap实现自定义分页的实例代码
2017/06/19 Javascript
vue源码学习之Object.defineProperty 对数组监听
2018/05/30 Javascript
webpack dll打包重复问题优化的解决
2018/10/10 Javascript
微信小程序module.exports模块化操作实例浅析
2018/12/20 Javascript
VsCode里的Vue模板的实现
2020/08/12 Javascript
vue 使用 sortable 实现 el-table 拖拽排序功能
2020/12/26 Vue.js
Tornado Web服务器多进程启动的2个方法
2014/08/04 Python
使用Django Form解决表单数据无法动态刷新的两种方法
2017/07/14 Python
Python对象中__del__方法起作用的条件详解
2018/11/01 Python
python seaborn heatmap可视化相关性矩阵实例
2020/06/03 Python
基于matplotlib中ion()和ioff()的使用详解
2020/06/16 Python
pandas之分组groupby()的使用整理与总结
2020/06/18 Python
Tech21美国/加拿大:英国NO.1防摔保护壳品牌
2018/01/20 全球购物
支票、地址标签、包装纸和慰问卡:Current Catalog
2018/01/30 全球购物
销售总监工作职责
2013/11/21 职场文书
小学教师事迹材料
2014/01/13 职场文书
建设单位项目负责人任命书
2014/06/06 职场文书
留学推荐信(中英文版)
2015/03/26 职场文书
复试通知单模板
2015/04/24 职场文书
贫民窟的百万富翁观后感
2015/06/09 职场文书
焦裕禄纪念馆观后感
2015/06/09 职场文书
一年之计:2019年下半年的计划
2019/05/07 职场文书
POST提交数据常见的四种方式
2022/01/18 HTML / CSS
Python采集爬取京东商品信息和评论并存入MySQL
2022/04/12 Python