有会的不吝赐教了。

解决方案 »

  1.   

    begin
      try
        Template := EmptyParam;
        NewTemplate := false;
        ItemIndex := 1;
        try
          Wordapplication.Connect;
        except
          MessageDlg('你没有安装WORD软件!', mtError, [mbOk], 0);
          Abort;
        end;
        autoformat:=true;
        Wordapplication.Visible := True;
        WordApplication.Caption := '自动生成WORD文件表格';
        Template := EmptyParam;
        NewTemplate := False;
        if ChkBoxNewDoc.Checked then
        begin
          WordApplication.Documents.Add(Template, NewTemplate);
          WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
        end;//建立文档
        WordApplication.Options.CheckSpellingAsYouType := False;
        WordApplication.Options.CheckGrammarAsYouType := False;
        pagew:=WordDocument.PageSetup.PageWidth-worddocument.PageSetup.Get_rightMargin-worddocument.PageSetup.Get_leftMargin;
        //pagew是页面设置表格的总宽度
            with WordDocument do
            begin
    tables.Add(WordDocument.Words.Last,self.DBGrid.DataSource.DataSet.RecordCount+1,self.DBGrid.DataSource.DataSet.FieldCount);
    //插入表格
            gridw:=0;
            for rowi:=0 to  dbgrid.Columns.Count-1 do //建立单元格宽度
                    begin
                    WordFont.ConnectTo(WordDocument.Sentences.Get_Last.Font);
                    WordFont.Bold := 1;
                    tables.Item(1).Cell(1,rowi+1).Range.Set_Text(DBGrid.Columns[rowi].Title.Caption);
                    gridw:=gridw+dbgrid.Columns[rowi].Width;
                    end;
            for rowi:=0 to  dbgrid.Columns.Count-1 do
                begin
                grideveryw:=((dbgrid.Columns[rowi].Width-2) / gridw)*(pagew);
                tables.Item(1).Columns.item(rowi+1).Set_Width(grideveryw);
                end;
            WordFont.ConnectTo(WordDocument.Sentences.Get_Last.Font);
            WordFont.Bold := 0;
            DBGrid.DataSource.DataSet.first;
            for coli:=0 to  self.DBGrid.DataSource.DataSet.RecordCount-1 do
                begin
                for rowi:=0 to  dbgrid.Columns.Count-1 do
                    begin
                    try
                        case DBGrid.Columns[rowi].Field.DataType of
                        ftGraphic://如果是图
                            begin
                            DBImgFishImg.CopyToClipboard;
                            tables.Item(1).Cell(coli+2,rowi+1).Range.Paste;
                            end;
                        ftMemo:
                            begin
                            tables.Item(1).Cell(coli+2,rowi+1).Range.Set_Text('简要介绍(略)');
                            end;
                        else
                            tables.Item(1).Cell(coli+2,rowi+1).Range.Set_Text(DBGrid.DataSource.DataSet.Fields[rowi].AsString);
                        end;
                    except
                    end;
                    end;
                DBGrid.DataSource.DataSet.Next;
                end;  
            end;
        WordDocument.Range.PageSetup.Set_BottomMargin(100);
        WordDocument.Range.InsertAfter('表格填写结束!');
        worddocument.PageSetup.Set_Gutter(30);
        worddocument.PageSetup.Set_GutterOnTop(true);
        coli:=1;
        with WordDocument do
            begin
                If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
                    ActiveWindow.Panes.Item(2).Close;          //.Panes[2].Close;        ActiveWindow.ActivePane.View.SeekView:=wdSeekCurrentPageHeader;//显示页眉
            Application.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;   //中间对齐
            //wdAlignParagraphRight:右边对齐;
            //wdAlignParagraphJustify:两边对齐;
            //wdAlignParagraphLeft:左边对齐;
            //wdAlignParagraphCenter:中间对齐;
            Application.Selection.TypeText('dcsdcs初次开发的软件,设置页眉!酷吧!!!');
            ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageFooter;//显示页脚
            Application.Selection.ParagraphFormat.Alignment := wdAlignParagraphLeft;
            Application.Selection.TypeText('dcsdcs初次开发的软件,设置页脚!酷吧!!!');
            ActiveWindow.ActivePane.View.SeekView:=wdSeekMainDocument;
            end;
        BtnCloseWord.Enabled := True;
        BtnPrint.Enabled := True;
        BtnPreview.Enabled := True;
      except
        on E: Exception do
        begin
          ShowMessage(E.Message);
          WordApplication.Disconnect;
        end;
      end;
    end;
      

  2.   

    使用OleContainer:
    OleContainer1.OleObject.Sections.Item(wdHeaderFooterPrimary).
    Headers.Item(wdHeaderFooterPrimary).Range.Text:='sdfsdf';