Delphi之word报表

先上图:

delphi实现代码如下:

procedure TForm1.ToolButton12Click(Sender: TObject);
var 
myTable: Table;
oleCount,oleUnit: Olevariant; 
begin 
//以下打开word新的文档
try
WordApplication1.Connect;
WordApplication1.Caption := '打印故障记录';
WordApplication1.Visible := true;
except
MessageDlg('也许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控件描述字体。

转载于:https://www.cnblogs.com/kevin-tyc/p/3326926.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.所需的三个控件: ChooseWA: TWordApplication; ChooseWD: TWordDocument; ChooseWF: TWordFont; 2.检查计算机是否安装了WORD try ChooseWA.Connect; except MessageBox(handle,'无法链接,请确认电脑上是否安装Word XP/2003及以上版本','连接出错', MB_Ok or MB_ICONERROR); Abort; end; 3.关闭WORD拼写检查 //因为Word进行拼写检查需要很多时间,所以首先关闭检查 ChooseWA.Options.CheckSpellingAsYouType := False; ChooseWA.Options.CheckGrammarAsYouType := False; 4.新建一个文档并设置文档的标题 var NewDocument: _Document; ItemIndex: OleVariant; ItemIndex := 1; NewDocument := ChooseWA.Documents.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam); ChooseWD.ConnectTo(NewDocument); ChooseWD.Windows.Item(ItemIndex).Caption := '我新建的第一个文档';//此文档的第一个窗口的标题,试卷 名称 5.写入数据 ChooseWD.Range.InsertAfter('第一行数据'+#13);//#13代表换行 6.设置字体格式 procedure SetFont(aBold,aItalic,aShadow,aSize:integer); begin ChooseWF.ConnectTo(ChooseWD.Sentences.Get_Last.Font); ChooseWF.Name := '宋体'; ChooseWF.Bold := aBold; ChooseWF.Italic := aItalic; ChooseWF.Shadow := aShadow; ChooseWF.Size := aSize; end; 如:SetFont(1,0,0,22);//设置字体为22号 7.向WORD中写入表格 (1)插入表格: ChooseWD.Tables.Add(ChooseWD.Words.Last, RowNum, ColNum,EmptyParam,EmptyParam);//RowNum为行数, ColNum为列数 (2)插入数据: ChooseWD.Tables.Item(1).Cell(1,1).Range.Text := '第一行第一列'; ChooseWD.Tables.Item(1).Cell(2,1).Range.Text := '第二行第一列'; 8.向WORD写入图片 var Img: TImage; MyFormat: Word; AData: Cardinal; APalette: HPALETTE; Img.Picture.LoadFromFile('文件路径');//从文件夹中导入图片至控件 Img.Picture.SaveToClipboardFormat(MyFormat,AData,APalette);//将图片转存到剪贴板中 Clipboard.SetAsHandle(MyFormat,AData);//将剪贴板中的图片复制出来,注意添加Clipbrd单元 ChooseWD.Sentences.Last.Paste;//在WORD中粘贴图片 9.在界面中显示WORD文档 ChooseWA.Visible:=true; 10.断开与WORD的链接 ChooseWA.Disconnect; ChooseWD.Disconnect; Chart1.SaveToBitmapFile(‘文件名.bmp’);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值