php 操作word 的使用com组件的总结

set_time_limit(0);
//不超时
                    
error_reporting(E_ALL);
//打印所有的错误
                    
 $empty = new VARIANT();
                    
  com_load_typelib('Word.Application');   
//加载word. Application组件

$word = new COM("word.application", null, CP_UTF8) or die("请安装word");
//
上面的参数,null, CP_UTF8,一个是默认主机为null为本地主机,CP_UTF8则是字符串转码,直接把字符串转为word的编码,操作的过程中不需要转码了。

$word->Visible = true;
//
是否可见,false为不可见。

$word->Documents->Open(realpath($File),false,true);//true的意思为已只读的方式打开
//打开指定的文件,不写realpath()函数时,需要写文件的绝对路径。

$word->Selection->Typetext( $Text );
//在word文档的开始处添加文本内容。

$word->ActiveDocument->Bookmarks->Count;
//标签的数量

$word->Selection->Find->Execute($bookName,false,true,false,false,false,true,1,false,$val,2);
//$bookName为要替换的字符串,$val为替换的值,一般对文本操作,可用字符串替换的方法,字符串替换不乱码,标签替换字符串中文的话会乱码。替换字符串最大值为255,字符串的长度操作255为无法替换,并报错。

$word->ActiveDocument->Bookmarks->Exists($bookName);
//标签是否存在,不存在返回false.

$word->ActiveDocument->Bookmarks($bookName)->range->Text = 'aaaaa';
//在标签处替换字符串,适用于英文,中文会乱码。

$word->Selection->InlineShapes->AddPicture($images);    //在开头的地方插入图片     

$word->ActiveDocument->Bookmarks('addImage')->Range->InlineShapes->AddPicture($images);
//在指定标签处插入图片。

//在word中绘制表格时需要依附标签来绘制,否则绘制的表格是在word文档开头的地方。
$objBookmark = $word->ActiveDocument->Bookmarks("drawTable");
$range = $objBookmark->Range;
$Table =$word->ActiveDocument->Tables->Add($range, $rowWidth,$colsWidth,1,0);//行,列,行列自适应

//$rowWidth为行的数量,$colsWidth为列的数量

$Table->Cell($z, $j)->Range->Font->Size = 11;
//设置字体
$Table->Cell($z, $j)->Range->Text = $title;
//在指定的位置赋值内容

 $Table->Cell(2, $titleCount)->Merge($Table->Cell(3, $titleCount));//合并单元格
 $Table->Cell(4, $titleCount)->Merge($Table->Cell(5, $titleCount));//
合并单元格
$Table->Cell(6, $titleCount)->Merge($Table->Cell(7, $titleCount));//
合并单元格
$Table->Cell(1, 1)->Split($Rownum, $Columnnum); //拆分单元格

//设置字体大小
$Table->Cell($j, $z)->Range->ParagraphFormat->Alignment=1;    //0= Left, 1=Center, 2=Right 
$Table-> word-> Selection-> Font-> Name   =   $fontName;  
$Table-> word-> Selection-> Font-> Size   =   $fontSize; 
//字体大小,只支持数字
$Table-> word-> Selection-> Font-> Underline   =   true; //是否有下划线
$Table
->Cell(1,2)->Range->Bold = True;  
//是否粗体
$Table
->Cell(1,2)->Range->Font->Italic = True;   //是否斜体

$Table->Borders->OutsideLineStyle = 2;  //设置table的外边框  2为虚线  1实线  0是没有线
$Table1->Rows(1)->Height = 50;  //设置第一行的高度为50
$Table1->Columns(1)->Width = 20;

$word-->ActiveDocument->Tables->Count; //获取word文档中table的个数
$word->ActiveDocument->Tables(1)->Rows->Count;
 $word->ActiveDocument->Tables(1)->Columns->Count;

$word->ActiveDocument->Tables(1)->Rows(1)->Delete; //删除某一行
$word->ActiveDocument->Tables(1)->Columns(2)->Delete;   

$row = $word->ActiveDocument->Tables(1)->Rows(3);       
$Table = $word->ActiveDocument->Tables(1)->Rows->Add($row);
//指定位置之前插入一行

$Table->Cell($i,1)->Range->ParagraphFormat->LineSpacing = 15;   //设置文档的行间距

$Table->Cell($i,1)->VerticalAlignment=3;
$Table->Rows($i)->Height = 40;    //设置某一行的行高

$Table->Cell($i,1)->Range->InlineShapes->AddHorizontalLineStandard;//添加一条横线

$Table->Rows($i)->Range->Find->Execute('NAME0A',false,true,false,false,false,true,1,false,'NAME'.($i-1).'A',1);
//替换在table中的替换,也可不知table中替换,最后一个参数,替换的个数0不替换,1替换一个,2全部替换。

$word->Selection->Find->Execute($bookName,false,true,false,false,false,true,1,false,$val,2); //全文替换

$Table->Rows(1)->Range->Copy;
$Table->Rows(1)->Range->Paste;
  //拷贝和粘贴

$Table->Rows->Add($Table->Rows); //把整个表格复制下来

$Table2->Cell(1,1)->Borders->OutsideLineStyle =0; //设置边框是否可见,InsideLineStyle

$word->ActiveDocument->SaveAs($File, $Format);
//保存文件并设置保存的格式。

$word->Close();       
$word->Quit();

//释放资源
?7?1

 

if(@$fp=fopen($filePath, "r")){
                //输入文件标签 
                header("Content-Type: text/html;charset=gb2312");
                 Header("Content-type:   application/octet-stream"); 
                  Header("Accept-Ranges:   bytes"); 
                 Header("Accept-Length:   ".filesize($filePath)); 
                  Header("Content-Disposition:   attachment;   filename=".$templateId."_".date("YmdHis").".doc"); 
                //输出文件内容 
              while(!@feof($fp)){
                  echo fread($fp, 1024);
              }
             fclose($fp);     
          }
//文件下载

你可能感兴趣的:(PHP,职场,com,休闲)