具体讲就是如何实现表格的调整,合并,撤分等功能

解决方案 »

  1.   

    具体对表格的操作可以通过录制宏获得!{打开Word文档,初始化信息}
    function TcommonFunc.CreateTestPaper(wordFile: String):OLEVariant;
    var
      MSWord: OLEVariant;
      i:Integer;
    begin
    //打开Word;
      try
        MSWord := GetActiveOleObject('Word.Application');
      except
        try
          MSWord := CreateOleObject('Word.Application');
        except
          Exit;
        end;
      end;
      MSWord.Visible := True;
      //----新建word----//
      //MSWord.Documents.Add(NewTemplate:=true);  //----打开word----//
      MSWord.documents.open(FileName:=wordFile);
      //写入标题;
      with adotest_mess do
      begin
      sql.Clear;
      sql.add('select * from test_message where id='+inttostr(pmainformtp.test_id));
      open;
      MSWord.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
      MSWord.Selection.Font.Size:=22;
      MSWord.Selection.Font.Bold:=wdToggle;
      MSWord.Selection.TypeText(Text:=FieldByName('test_name').asstring);
      MSWord.Selection.TypeParagraph;
      MSWord.Selection.Font.Bold:=wdToggle;  MSWord.Selection.ParagraphFormat.Alignment:= wdAlignParagraphCenter;
      MSWord.Selection.Font.Size:=11;
      MSWord.Selection.TypeParagraph;
      //创建表格
      MSWord.ActiveDocument.Tables.Add(Range:=MSWord.Selection.Range,
                                             NumRows:=2,
                                             NumColumns:=13,
                                             DefaultTableBehavior:=wdWord9TableBehavior,
                                             AutoFitBehavior:=wdAutoFitContent);
      MSWord.Selection.Tables.item(1).Select;
      MSWord.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
      //向表格写入数据
      MSWord.Selection.TypeText(Text:='题号');
      MSWord.Selection.MoveDown(Unit:=wdLine, Count:=1);
      MSWord.Selection.TypeText(Text:='分数');
      MSWord.Selection.MoveUp(Unit:=wdLine, Count:=1);
      for i:=1 to 10 do
      begin
        MSWord.Selection.MoveRight(Unit:=wdCharacter, Count:=1);
        MSWord.Selection.TypeText(Text:=self.numToCnStrNum(i));
      end;
      MSWord.Selection.MoveRight(Unit:=wdCharacter, Count:=1);
      MSWord.Selection.TypeText(Text:='总分');
      MSWord.Selection.MoveRight(Unit:=wdCharacter, Count:=1);
      MSWord.Selection.TypeText(Text:='附加题');
      MSWord.Selection.MoveDown(Unit:=wdLine, Count:=1);
      MSWord.Selection.MoveDown(Unit:=wdLine, Count:=1);
      result:=MSWord
      end;
    end;