Delphi操作Word中的表

作者在 2006-12-24 04:05:00 发布以下内容

Procedure GetWordCellStr;
var

    WordApp: TWordApplication;
    WordDoc: TWordDocument;
    DocInx,FileName,CfCversions,IsReadOnly,AddToRctFiles,PswDocument,
    PswTemplate,Revert,WPswDocument,WPswTemplate,FormatWord: OleVariant;
    i,iRow,iCol: Integer;
    myCell: Cell;
    myRow: Row;
begin
  Memo.Lines.Clear ;

    // ===== 创建对象 =====
    if not Assigned(WordApp) then                         
    begin
      WordApp:= TWordApplication.Create(nil);
      WordApp.Visible := false;
    end;
    if not Assigned(WordDoc) then
      WordDoc:= TWordDocument.Create(nil);
  try
    DocInx:=1;
    FileName := ′d:\test.doc′;
    IsReadOnly:=true;
    CfCversions := EmptyParam;
    AddToRctFiles:= EmptyParam;
    PswDocument:= EmptyParam;
    PswTemplate:= EmptyParam;
    Revert:= EmptyParam;
    WPswDocument:= EmptyParam;
    WPswTemplate:= EmptyParam;
    FormatWord:= EmptyParam;    
    // ===== 打开文件 =====                        
    WordApp.Documents.open(FileName,CfCversions,IsReadOnly,AddToRctFiles,
       PswDocument,PswTemplate,Revert,WPswDocument,WPswTemplate,FormatWord);
    // ===== 关联文件 =====
    WordDoc.ConnectTo(WordApp.Documents.Item(DocInx));

    //方法(1)==> 规则表
    For i := 1 To WordDoc.Tables.Count do      //第 i 个表
    begin                                      //第 iRow 行
      For iRow := 1 To WordDoc.Tables.Item(i).Rows.Count do     
      begin                                            //第 iCol列
        For icol := 1 To WordDoc.Tables.Item(i).Columns.Count do
        begin
          myCell:=WordDoc.Tables.Item(i).Cell(iRow,icol);
          memo1.Lines.add(myCell.Range.Text);
        end;
      end;
    end;

   
    //方法(2)==> 不规则表:只有横向合并时
    For i := 1 To WordDoc.Tables.Count do   &n

软件开发技术贴 | 阅读 5482 次
文章评论,共0条
游客请输入验证码