先上图:
delphi实现代码如下:
procedure TForm1.ToolButton12Click(Sender: TObject);var myTable: Table;oleCount,oleUnit: Olevariant; begin //以下打开word新的文档tryWordApplication1.Connect;WordApplication1.Caption := '打印故障记录';WordApplication1.Visible := true;exceptMessageDlg('也许word没有安装,请重试', mtError, [mbOk], 0);end; WordDocument1.ConnectTo(WordApplication1.Documents.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam));WordDocument1.Activate;//设置页面大小和左右间距 //600磅=21cm,850磅=29.7cm,是标准A4纸张大小,每磅0.035cm, WordDocument1.PageSetup.PageWidth := 600;WordDocument1.PageSetup.PageHeight := 850;WordDocument1.PageSetup.LeftMargin := 50;WordDocument1.PageSetup.RightMargin := 50;//设置标题内容和字体WordApplication1.Selection.Font.Size := 15;WordApplication1.Selection.Font.Name := '黑体';WordApplication1.Selection.Font.Color := clBlack;WordApplication1.Selection.Font.Bold := 1;WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;WordApplication1.Selection.TypeText('故障记录');//设置正文内容和字体 WordApplication1.Selection.Font.Size := 11;WordApplication1.Selection.Font.Name := '宋体';WordApplication1.Selection.Font.Color := clBlack;WordApplication1.Selection.Font.Bold := 0;WordApplication1.Selection.TypeParagraph;WordApplication1.Selection.TypeParagraph;WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphJustify;//以下插入表格 oleUnit := wdLine;oleCount := 3; MyTable := WordApplication1.Selection.Tables.Add(WordApplication1.Selection.Range,3,2,EmptyParam,EmptyParam);MyTable.Cell(1,1).Height := 20;MyTable.Cell(1,1).Range.Text := '试验日期:' +form4.adoQuery1.FieldByName('试验时间').AsString;MyTable.Cell(1,2).Range.Text := '试验人员:' +form4.adoQuery1.FieldByName('试验人员1').AsString+'/'+form4.adoQuery1.FieldByName('试验人员2').AsString;MyTable.Cell(2,1).Height := 20;MyTable.Cell(3,1).Height := 20;MyTable.Cell(4,1).Height := 20;MyTable.Cell(2,1).Range.Text := '最大加速度:';MyTable.Cell(2,2).Range.Text := iplot1.Channel[2].DataCursorYText;MyTable.Cell(3,1).Range.Text := '最大拉力:';MyTable.Cell(3,2).Range.Text := iplot1.Channel[0].DataCursorYText;//下移2行添加新的表格,因为上下的列数不一样WordApplication1.Selection.MoveDown(oleUnit,oleCount,EmptyParam);MyTable := WordApplication1.Selection.Tables.Add(WordApplication1.Selection.Range,6,1,EmptyParam,EmptyParam);MyTable.Cell(4,1).Height := 20;MyTable.Cell(5,1).Height := 20;MyTable.Cell(6,1).Height := 20;MyTable.Cell(7,1).Height := 20;MyTable.Cell(8,1).Height := 20;MyTable.Cell(9,1).Height := 20;MyTable.Cell(4,1).Range.Text := '采样频率:'+'20000';MyTable.Cell(5,1).Range.Text := '加速度传感器1信息:'+form4.adoQuery1.FieldByName('加速度传感器1编号').AsString; MyTable.Cell(6,1).Range.Text := '加速度传感器2信息:'+form4.adoQuery1.FieldByName('加速度传感器2编号').AsString;MyTable.Cell(7,1).Range.Text := '加速度传感器3信息:'+form4.adoQuery1.FieldByName('加速度传感器3编号').AsString;MyTable.Cell(8,1).Range.Text := '力传感器1信息:'+form4.adoQuery1.FieldByName('力传感器1编号').AsString;MyTable.Cell(9,1).Range.Text := '力传感器1信息:'+form4.adoQuery1.FieldByName('力传感器1编号').AsString;end;
该代码在按钮响应函数中写入,其中需包含
uses WordXP
WordApplication1:表示WordApplication控件
WordDocument1:表示WordDocument控件 还需要WordFont控件描述字体。