请问各位高手:我现在有一个应用需要读取Excel文档中的内容,在网上查询后知道用COM函数,但网上的介绍都比较简要,请问哪位有关于PHP的COM一类函数的中文介绍资料,同时在Apach 2.2.2 + PHP5的模式下能否直接使用COM进行Excel文件的操作,是否需要安装相关的类库之类的东西。谢谢各位了。

解决方案 »

  1.   


    <?PHP 
    $filename   =   "c:/spreadhseet/test.xls "; 
    $sheet1   =   1; 
    $sheet2   =   "sheet2 "; 
    $excel_app   =   new   COM( "Excel.application ")   or   Die   ( "Did   not   connect "); 
    print   "Application   name:   {$excel_app-> Application-> value}\n "   ;   
    print   "Loaded   version:   {$excel_app-> Application-> version}\n "; 
    $Workbook   =   $excel_app-> Workbooks-> Open( "$filename ")   or   Die( "Did   not   open   $filename   $Workbook "); 
    $Worksheet   =   $Workbook-> Worksheets($sheet1); 
    $Worksheet-> activate; 
    $excel_cell   =   $Worksheet-> Range( "C4 "); 
    $excel_cell-> activate; 
    $excel_result   =   $excel_cell-> value; 
    print   "$excel_result\n "; 
    $Worksheet   =   $Workbook-> Worksheets($sheet2); 
    $Worksheet-> activate; 
    $excel_cell   =   $Worksheet-> Range( "C4 "); 
    $excel_cell-> activate; 
    $excel_result   =   $excel_cell-> value; 
    print   "$excel_result\n "; 
    #To   close   all   instances   of   excel: 
    $Workbook-> Close; 
    unset($Worksheet); 
    unset($Workbook); 
    $excel_app-> Workbooks-> Close(); 
    $excel_app-> Quit(); 
    unset($excel_app); 
    ?> http://cn.php.net/manual/en/class.com.php
    用com组件 或 phpExcel 这是一个写好的php类
      

  2.   

    可以用ODBC来读excel 自动化接口访问excel的有点不太好找
      

  3.   

    phpexcel类
     public function adduserimport() { 
         if(strtolower(substr($_FILES['userfile']['name'],-4))=='.xls') 
         { 
           if(move_uploaded_file($_FILES['userfile']['tmp_name'],P("webprojectpath")."temp/tempuser.xls"))  
           { 
                if(file_exists(P("webprojectpath")."temp/tempuser.xls")) 
                { 
                    $m=M("r.user"); 
                    date_default_timezone_set('Asia/Shanghai'); 
                    import('@plugin.Classes.PHPExcel.IOFactory'); 
                    //加载xls文件
                    $objPHPExcel =PHPExcel_IOFactory::load(P("webprojectpath")."temp/tempuser.xls"); 
                    $objWorksheet = $objPHPExcel->getActiveSheet(); 
                    //取得行数和列数
                    $highestRow = $objWorksheet->getHighestRow(); // e.g. 10 
                    $highestColumn = $objWorksheet->getHighestColumn(); // e.g 'F'                 $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // e.g. 5 
                     //取得第一列
                    $row=1; 
                    $user=array(); 
                    for ($col = 0; $col < $highestColumnIndex; ++$col) { 
                        $user[$col]=$objWorksheet->getCellByColumnAndRow($col, $row)->getValue(); 
                      } 
                    $tmkey=array(); //保存列的位置,预防列位置前后变了,这样不会影响导入。
                   //映射字段 
                    if(is_array($user)) 
                    { 
                      foreach($user as $key=>$value) 
                      { 
                        $value=str_replace(" ","",trim($value)); //消除中间空格
                        switch($value) 
                        { 
                          case '姓名': 
                                 $tmkey['realname']=$key; 
                              break; 
                          case '职务': 
                                 $tmkey['job']=$key; 
                              break; 
                          case '性别': 
                                 $tmkey['sex']=$key; 
                              break; 
                          case '用户名': 
                                 $tmkey['username']=$key; 
                              break; 
                          case '密码': 
                                 $tmkey['password']=$key; 
                              break; 
                          case 'email': 
                                 $tmkey['email']=$key; 
                              break; 
                          case '电子邮件': 
                                 $tmkey['email']=$key; 
                              break;                         
                        } 
                      } 
                      if(empty($tmkey)) 
                      { //没有定义那么按顺序导入
                        $tmkey['realname']=0; 
                        $tmkey['job']=1; 
                        $tmkey['sex']=2; 
                        $tmkey['username']=3; 
                        $tmkey['password']=4; 
                      } 
                    } 
                    //取得了要插入字段key 
                    $projectid=$_SESSION['cid']!=''?$_SESSION['cid']:intval($_POST['projectid']); 
                    $utable=''; 
                    $utable.='<table width="720" border="0" align="center" cellpadding="2" cellspacing="1" class="forumline">' . "\n"; 
                    $utable.='<tr>' . "\n"; //输出第一行内容,就是表头
                    for ($col = 0; $col <=$highestColumnIndex; ++$col) { 
                        $utable.='<td class="row1">'. $user[$col] . '</td>' . "\n"; 
                        } 
                    $utable.='</tr>' . "\n"; //开始循环主体数据从第二行开始因为第一行做了表头类型
                    for ($row = 2; $row <= $highestRow; ++$row) { 
                      $utable.='<tr>' . "\n"; 
                      $user=array(); //取得一列数据,然后插件数据库
                      for ($col = 0; $col <=$highestColumnIndex; ++$col) { 
                        $user[$col]=$objWorksheet->getCellByColumnAndRow($col, $row)->getValue(); 
                      } 
                                                     
                            $m->clearEdit(); 
                            $username=trim($user[$tmkey['username']]); //$tmkey意思是原来存那个字段列的位置
                            if($username!='') 
                            { 
                                $u=$m->where("username='".$username."'")->limit(1)->fetch()->getData(); 
                                 //判断下是不是有重复用户名
                                if(isset($u['username'])&&$u['username']==$username) 
                                { 
                                    $username=$username.rand(1000,9999); 
                                    $user[$tmkey['username']]=$username; 
                                } 
                                $m->clearEdit(); 
                                $m->username=$username; 
                                $m->password=md5(trim($user[$tmkey['password']])); //取得密码字段
                                $m->realname=trim($user[$tmkey['realname']]); //取得真实姓名
                                $m->setprojectid($projectid); 
                                if(isset($tmkey['email'])) 
                                { //看看是否有email字段
                                  $m->email=trim($user[$tmkey['email']]); 
                                } 
                                $m->job=trim($user[$tmkey['job']]); 
                                $m->sex=trim($user[$tmkey['sex']])=='男'?1:0; 
                                $m->save(); //插入数据
                            } 
                       for ($col = 0; $col <=$highestColumnIndex; ++$col) { 
                        $utable.='<td>'. $user[$col] . '</td>' . "\n"; 
                        } 
                      $utable.='</tr>' . "\n"; 
                    } 
                    $utable.='</table>' . "\n";                 
                    unlink(P("webprojectpath")."temp/tempuser.xls"); 
             }else{ 
               redirect(url_for("rbac/userimport"),"读取用户名单出错!",3); 
             } 
           }else{ 
               redirect(url_for("rbac/userimport"),"用户名单文件出错!",3); 
             } 
         }else{ 
               redirect(url_for("rbac/userimport"),"提交用户名单文件有误!",3); 
             } 
         $this->utable=$utable; 
     } 
    还有一个是输出的。
    这个是我框架通用权限系统一个导入程序http://code.google.com/p/queryphp/downloads/list
      

  4.   

    读取的话可以用phpexcelreader   
      

  5.   

    请问各位1楼的代码中下面两行是什么含义:
    $sheet1   =   1; 
    $sheet2   =   "sheet2 "; 这一行应该是工作表中的表名,但第一行$sheet1 = 1不明白是什么意思?
    请知道告诉一下。谢谢。
      

  6.   

    请问有没有知道这两行含义的,烦请告诉一下,谢谢:
    $sheet1 = 1;  
    $sheet2 = "sheet2 "; 这一行应该是工作表中的表名,但第一行$sheet1 = 1不明白是什么意思?
      

  7.   

    有谁知道这一行是什么意思吗?
    $Worksheet-> activate;
      

  8.   

    这个应该是选中当前激活的sheet吧