如题.....本人作了几个固定的WORD报表格式,现须替换其中的字段信息生成报表,但具体的代码不知如何下手...

解决方案 »

  1.   

    引用OLE对象
    可以查下OFFICE的VBA帮助,里面有很详细的说明和例子
      

  2.   

    //验证提交内容
    if edbbmc.Text='' then
    begin
    MessageDlg('请填写报表名称!', mtInformation,[mbOk], 0);
    exit;
    end;
     //启动word
    waPrint.Connect;
    waPrint.Visible:=false;
    //打开空白模板
    docindex:=0;
    itemindex:=1;
    template:=extractFileDir(Application.Exename)+'\template\Dyreport.dot';
    newtemplate:=false;
    documenttype:=0;
    visble:=true;
    unit_:=1;
    count:=1;
    waPrint.Documents.Add(template,newtemplate,documenttype,visble);
    wdPrint.ConnectTo(waPrint.Documents.Item(itemindex));
    Unit_:=wdCharacter;
    Count:=4;
    Extend:=wdExtend;
    //写入报表标题
    waprint.Selection.MoveRight(unit_,count,extend);
    waprint.Selection.TypeText(edbbmc.Text);
    Unit_:=wdLine;
    Count:=1;
    waPrint.Selection.MoveDown(Unit_,Count,Extend);
    DefaultTableBehavior:=wdWord9TableBehavior;
    waprint.ActiveDocument.Tables.Add(waprint.Selection.Range,2,lbdes.Count,DefaultTableBehavior,AutoFitBehavior);
    waprint.Selection.Columns.PreferredWidthType:=wdPreferredWidthAuto;
    //读取数据库的内容并写入表格内报表数据
    grid:=wdPrint.Tables.Item(1);
    for i:=1 to lbdes.Items.Count do
    grid.Cell(1,i).Range.Text:=lbdes.Items[i-1];
    with dmform.AllQuery do
     begin
     last;
     First;
     if recordcount>1 then
     begin
     Unit_:=wdLine;
     Count:=2;
     waprint.Selection.MoveDown(unit_,count,extend);
     Unit_:=wdCharacter;
     Count:=7;
     waprint.Selection.MoveRight(unit_,count,extend);
     numrows:=recordcount-1;
     waprint.Selection.InsertRowsBelow(numrows);
     end;
     for i := 2 to recordcount+1 do
     begin
     if not Eof then
     for j:=1 to lbdes.Items.Count do
     begin
    grid.Cell(i,j).Range.Text:=FieldByName(transfield(lbdes.Items[j-1])).AsString;
     end;
     Next;
     end;
     first;
    end;
    waPrint.Visible:=true;
      

  3.   

    olecell赋值即可,不过2000,2003不一样,比较麻烦!