正做毕业设计,实现word预览试卷试卷功能,谢谢大家了

解决方案 »

  1.   

    //添加试卷头const
      Number:array[1..10] of string = ('一', '二', '三', '四', '五', '六', '七',
        '八', '九', '十');procedure SetTitle(var PaperDocument:TWordDocument; var PaperFont:TWordFont;
      const Title:string);
    begin //  '生物科学与工程学院 人体解剖生理学试卷'
      PaperDocument.Range.InsertAfter(Title + #13);
      SetFont(PaperFont, PaperDocument, 1, 0, 0, 24);
      PaperDocument.Range.InsertAfter('院系___________班级__________姓名_________学号_____得分________' + #13);
      SetFont(PaperFont, PaperDocument, 1, 0, 0, 16);
    end;
    //设置文档字体procedure SetFont(var WordFont:TWordFont; var WordDocument:TWordDocument; aBold,
      aItalic, aShadow, aSize:integer);
    begin
      WordFont.ConnectTo(WordDocument.Sentences.Get_Last.Font);
      WordFont.Name := '宋体';
      WordFont.Bold := aBold;
      WordFont.Italic := aItalic;
      WordFont.Shadow := aShadow;
      WordFont.Size := aSize;
    end;
    //创建Word文档procedure CreateWordDocument(var WordApplication:TWordApplication; var
      WordDocument:TWordDocument);
    var
      Docs, Template, NewTemplate, ItemIndex:OleVariant;
    begin
      try
        Template := EmptyParam;
        NewTemplate := True;
        ItemIndex := 1;
        try
          WordApplication.Connect;
        except
          MessageBox(Application.Handle,
            '您可能没有安装Word!建议您安装WordXP,并选择完全安装!如果还有问题请检查您的系统!',
            '错误', MB_OK + MB_ICONSTOP);
          Abort;
        end;
        WordApplication.Visible := True;
        WordApplication.Caption := '电子试卷生成系统';
        Template := EmptyParam;
        NewTemplate := False;
        Docs := WordApplication.Documents;
        Docs.Add(NewTemplate := True);
        WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
        //关闭语法检查和拼写检查
        WordApplication.Options.CheckSpellingAsYouType := False;
        WordApplication.Options.CheckGrammarAsYouType := False;
      except
        on E:Exception do
          begin
            MessageBox(Application.Handle, PChar(E.Message), '错误', MB_OK +
              MB_ICONSTOP);
            WordApplication.Disconnect;
          end;
      end;
    end;procedure InsertTK(var Query:TQuery; var PaperDocument:TWordDocument; var
      AnDocument:TWordDocument;
      const HaveAns:Boolean; const SQLStr:string; var S:Integer; var
      StrList:TStringList);
    var
      I:Integer;
    begin
      if StrList.Count > 0 then
        begin
          if HaveAns then
            begin
              Inc(s);
              PaperDocument.Range.InsertAfter(Number[S] + '. 填空题(每题__分,共__题)'
                + #13);
              AnDocument.Range.InsertAfter(Number[S] + '. 填空题(每题__分,共__题)' +
                #13);
            end
          else
            begin
              Inc(s);
              PaperDocument.Range.InsertAfter(Number[S] + '. 填空题(每题__分,共__题)'
                + #13);
            end;
          for i := 1 to StrList.Count do
            begin
              Query.Close;
              Query.SQL.Clear;
              Query.SQL.Add(SQLStr);
              Query.ParamByName('ID').AsInteger := StrToInt(StrList.Strings[i - 1]);
              Query.Open;
              if Query.RecordCount > 0 then
                begin
                  if HaveAns then
                    begin
                      PaperDocument.Range.InsertAfter(IntToStr(i) + '. ' +
                        Query.FieldByName('填空题').AsString + #13);
                      AnDocument.Range.InsertAfter(IntToStr(i) + '. ' +
                        Query.FieldByName('填空题').AsString + #13);
                      AnDocument.Range.InsertAfter('正确答案:'  +
                        Query.FieldByName('答案').AsString + #13);
                    end
                  else
                    begin
                      PaperDocument.Range.InsertAfter(IntToStr(i) + '. ' +
                        Query.FieldByName('填空题').AsString + #13);
                    end;
                end;
            end;
        end;
    end;这个是我写的电子试卷的代码,你可以参考一下
      

  2.   

    何必要用word预览
    用richEdit控件即可