delphi word技术融合

 

新一篇: delphi动态创建的TMSComm串口控件不起作用!

<script></script>

procedure TExprmResultDlg.PreviewExprmReport;
var
    Template, NewTemplate,DocumentType,OleVisible:oleVariant;
    strTitle_Prior,strText_Prior,strTitle_Last,strText_Last:String;
begin
    Try
        Wordapplication1.Connect;
    Except
        Application.MessageBox('您尚未安装 Word应用程序,请先安装Word2000,然后再运行本程序!',
                                '系统提示',mb_Ok +MB_ICONWARNING);
        Abort;
    End;

    Template := EmptyParam;  //表示不使用模板
    NewTemplate := False;  //表示新建新建文档的类型为文档

    Doc.ConnectTo(WordApplication1.Documents.Add(Template, NewTemplate,DocumentType,OleVisible));

    //关闭拼音查找和语法查找,以便提高程序运行的效率
    WordApplication1.Options.CheckSpellingAsYouType := False;
    WordApplication1.Options.CheckGrammarAsYouType := False;

    //添加页眉
    AddRepPageHeader(18,Edit_RepHeader.Text,alCenter);

    //添加页脚  页码设置
    AddRepPageFooder(1,alCenter);

    m_wordSesson:= WordApplication1.Selection;
    WordFont1.ConnectTo(m_wordSesson.Font);
    WordFont1.Name := '宋体';

    //输出报告信息
    AddParagraphTitle('模块信息');//添加段落标题

    strTitle_Prior:='厂站名称:';
    strText_Prior:=Edit_FactoryName.Text;

    AddParagraphContentSingle(strTitle_Prior,strText_Prior);

    strTitle_Prior:='测试人:';
    strText_Prior:=Edit_TestPeoson.Text;
    strTitle_Last:='测试时间:';
    strText_Last:=FormatDateTime('yyyy"年"mm"月"dd"日"',now);

    AddParagraphContentDouble(strTitle_Prior,strText_Prior,strTitle_Last,strText_Last);

    //输出报告内容
    AddParagraphTitle('试验结果');//添加段落标题
    WriteReportInformation;


    AddParagraphTitle('试验结论');//添加段落标题
    m_wordSesson.TypeParagraph;
    m_wordSesson.TypeParagraph;
    m_wordSesson.TypeParagraph;

    Wordapplication1.Disconnect;

end;

 


procedure TExprmResultDlg.AddRepPageHeader(const nFontSize:integer;const strText:String;Const PageFoodAlign:TPageFoodAlign);//添加页眉
var
    alTextAlign:OleVariant;
begin
    Case PageFoodAlign of
        alLeft:alTextAlign:=wdAlignParagraphLeft;
        alCenter:alTextAlign:=wdAlignParagraphCenter;
        alRight:alTextAlign:=wdAlignParagraphRight;
        alJustify:alTextAlign:=wdAlignParagraphJustify;
    end;


    WordApplication1.ActiveWindow.ActivePane.View.SeekView :=wdSeekCurrentPageHeader;
    WordApplication1.Selection.ParagraphFormat.Alignment := alTextAlign;

    WordApplication1.Selection.Font.Size:=18;
    WordApplication1.Selection.Font.Color:=ClBlack;
    WordApplication1.Selection.InsertAfter(strText);
   
end;

procedure TExprmResultDlg.AddRepPageFooder(const nFistPageNum:integer;Const PageFoodAlign:TPageFoodAlign);//添加页脚  页码设置
var
    mmm,nPageNum,aaa,alTextAlign:OleVariant;
begin
    Case PageFoodAlign of
        alLeft:alTextAlign:=wdAlignParagraphLeft;
        alCenter:alTextAlign:=wdAlignParagraphCenter;
        alRight:alTextAlign:=wdAlignParagraphRight;
        alJustify:alTextAlign:=wdAlignParagraphJustify;
    end;

    mmm:=wdLine;
    nPageNum:=nFistPageNum;
    aaa:=wdFieldPage;
    WordApplication1.ActiveWindow.ActivePane.View.SeekView :=wdSeekCurrentPageFooter;
    WordApplication1.Selection.Move(mmm,nPageNum);
    WordApplication1.Selection.ParagraphFormat.Alignment := alTextAlign;

    WordApplication1.Selection.InsertAfter('第');
    mmm:=wdCharacter;
    WordApplication1.Selection.Move(mmm,nPageNum);
    WordApplication1.Selection.Fields.Add(WordApplication1.Selection.Range,aaa,mmm,nPageNum);

    aaa:=wdFieldNumPages;
    WordApplication1.Selection.InsertAfter('页/共');
    WordApplication1.Selection.Move(mmm,nPageNum);
    WordApplication1.Selection.Fields.Add(WordApplication1.Selection.Range,aaa,mmm,nPageNum);

    WordApplication1.Selection.InsertAfter('页');
    WordApplication1.ActiveWindow.ActivePane.View.SeekView := wdSeekMainDocument;
end;

 

procedure TExprmResultDlg.PastePicToWord(Const imageName:TImage);
var
    AFormat:word;
    AData :cardinal;
    APalette : hpalette;
begin
    imageName.Picture.SaveToClipboardFormat(AFormat,AData,APalette);
    Clipboard.SetAsHandle(AFormat,AData);
    doc.Sentences.Last.Paste;
end;


procedure TExprmResultDlg.AddParagraphTitle(const strTitle:String);//添加段落标题
begin
    //加回车键,形成段落:
    ParaFmt.SpaceBefore := 30;
    ParaFmt.SpaceAfter := 30;
    //m_wordSesson.TypeParagraph;
    WordFont1.Size:=14;
    WordFont1.Bold:=1;
    m_wordSesson.TypeText(strTitle + #13);

    m_tableNum:=m_tableNum + 1;

end;

procedure TExprmResultDlg.AddParagraphContentSingle(Const strTitle,strText:String);
var
    i:integer;
    TableBehavior,AutoFitbehavior:oleVariant;
begin
    TableBehavior:=FALSE;
    AutoFitbehavior:=FALSE;
    Doc.Tables.Add(WordApplication1.Selection.Range, 1 , 4, TableBehavior,AutoFitbehavior);//创建表格

    m_tableRow:=m_tableRow + 1;

    for i:=1 to 4 do
    begin
        Doc.Tables.Item(m_tableNum).Cell(m_tableRow,i).Range.Font.Size := 12;
        Doc.Tables.Item(m_tableNum).Cell(m_tableRow,i).Range.Font.Bold:=0;

        //去掉表格边框
        Doc.Tables.Item(m_tableNum).Cell(m_tableRow,i).Range.Borders.item(wdBorderLeft).LineStyle := wdLineStyleNone;
        Doc.Tables.Item(m_tableNum).Cell(m_tableRow,i).Range.Borders.item(wdBorderRight).LineStyle := wdLineStyleNone;
        Doc.Tables.Item(m_tableNum).Cell(m_tableRow,i).Range.Borders.item(wdBorderTop).LineStyle := wdLineStyleNone;
        Doc.Tables.Item(m_tableNum).Cell(m_tableRow,i).Range.Borders.item(wdBorderBottom).LineStyle := wdLineStyleNone;
    end;


    Doc.Tables.Item(m_tableNum).Cell(m_tableRow,2).Merge(Doc.Tables.Item(m_tableNum).Cell(m_tableRow,3));
    Doc.Tables.Item(m_tableNum).Cell(m_tableRow,2).Merge(Doc.Tables.Item(m_tableNum).Cell(m_tableRow,3));

    Doc.Tables.Item(m_tableNum).Cell(m_tableRow,1).Range.ParagraphFormat.Alignment:=wdAlignParagraphRight;
    Doc.Tables.Item(m_tableNum).Cell(m_tableRow,2).Range.ParagraphFormat.Alignment:=wdAlignParagraphLeft;


    Doc.Tables.Item(m_tableNum).Cell(m_tableRow,1).Range.Text:=strTitle;
    Doc.Tables.Item(m_tableNum).Cell(m_tableRow,2).Range.Text:=strText;


    WordApplication1.Selection.SetRange(100 * m_tableRow,100 * m_tableRow);//设定段落范围
end;


procedure TExprmResultDlg.AddParagraphContentDouble(Const strTitle_Prior,strText_Prior,strTitle_Last,strText_Last:String);
var
    i:integer;
    TableBehavior,AutoFitbehavior:oleVariant;
begin
    TableBehavior:=FALSE;
    AutoFitbehavior:=FALSE;
    Doc.Tables.Add(WordApplication1.Selection.Range, 1 , 4, TableBehavior,AutoFitbehavior);//创建表格
    m_tableRow:=m_tableRow + 1;

    for i:=1 to 4 do
    begin
        Doc.Tables.Item(m_tableNum).Cell(m_tableRow,i).Range.Font.Size := 12;
        Doc.Tables.Item(m_tableNum).Cell(m_tableRow,i).Range.Font.Bold:=0;

        //去掉表格边框
        Doc.Tables.Item(m_tableNum).Cell(m_tableRow,i).Range.Borders.item(wdBorderLeft).LineStyle := wdLineStyleNone;
        Doc.Tables.Item(m_tableNum).Cell(m_tableRow,i).Range.Borders.item(wdBorderRight).LineStyle := wdLineStyleNone;
        Doc.Tables.Item(m_tableNum).Cell(m_tableRow,i).Range.Borders.item(wdBorderTop).LineStyle := wdLineStyleNone;
        Doc.Tables.Item(m_tableNum).Cell(m_tableRow,i).Range.Borders.item(wdBorderBottom).LineStyle := wdLineStyleNone;       
    end;


    Doc.Tables.Item(m_tableNum).Cell(m_tableRow,1).Range.ParagraphFormat.Alignment:=wdAlignParagraphRight;
    Doc.Tables.Item(m_tableNum).Cell(m_tableRow,2).Range.ParagraphFormat.Alignment:=wdAlignParagraphLeft;

    Doc.Tables.Item(m_tableNum).Cell(m_tableRow,3).Range.ParagraphFormat.Alignment:=wdAlignParagraphRight;
    Doc.Tables.Item(m_tableNum).Cell(m_tableRow,4).Range.ParagraphFormat.Alignment:=wdAlignParagraphLeft;
                                                            
    Doc.Tables.Item(m_tableNum).Cell(m_tableRow,1).Range.Text:=strTitle_Prior;
    Doc.Tables.Item(m_tableNum).Cell(m_tableRow,2).Range.Text:=strText_Prior;
    Doc.Tables.Item(m_tableNum).Cell(m_tableRow,3).Range.Text:=strTitle_Last;
    Doc.Tables.Item(m_tableNum).Cell(m_tableRow,4).Range.Text:=strText_Last;

    WordApplication1.Selection.SetRange(100 * m_tableRow,100 * m_tableRow);//设定段落范围
end;

你可能感兴趣的:(Delphi)