一个简单的word报表的例子,在我学习的过程中很多高手毫不藏私的给我帮助,现在我也把自己的一个简单的例子拿出来给那些还不会的朋友,希望大家有有关资料的能够都拿出来共享。说明:WordApp是Server下的TWordApplication组件,WordDoc是TWordDocument组件,
dtmFaultFrom是一个DateTimePicker组件的名字,其他以dtm开头的组件都是var
myTable: Table;
oleCount,oleUnit: Olevariant;
begin
//以下打开word新的文档
try
WordApp.Connect;
WordApp.Caption := '打印故障记录';
WordApp.Visible := true;
except
MessageBox(Self.Handle,'您可能没有正确安装MicroSoft Word 2000!','提示',0);
end;
WordDoc.ConnectTo(WordApp.Documents.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam));
WordDoc.Activate; //设置页面大小和左右间距
//600磅=21cm,850磅=29.7cm,是标准A4纸张大小,每磅0.035cm,
WordDoc.PageSetup.PageWidth := 600;
WordDoc.PageSetup.PageHeight := 850;
WordDOc.PageSetup.LeftMargin := 50;
WordDoc.PageSetup.RightMargin := 50; //设置标题内容和字体
WordApp.Selection.Font.Size := 15;
WordApp.Selection.Font.Name := '黑体';
WordApp.Selection.Font.Color := clBlack;
WordApp.Selection.Font.Bold := 1;
WordApp.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
WordApp.Selection.TypeText('故障记录'); //设置正文内容和字体
WordApp.Selection.Font.Size := 11;
WordApp.Selection.Font.Name := '宋体';
WordApp.Selection.Font.Color := clBlack;
WordApp.Selection.Font.Bold := 0;
WordApp.Selection.TypeParagraph;
WordApp.Selection.TypeParagraph;
WordApp.Selection.ParagraphFormat.Alignment := wdAlignParagraphJustify; //以下插入表格
oleUnit := wdLine;
oleCount := 2;
MyTable := WordApp.Selection.Tables.Add(WordApp.Selection.Range,1,4,EmptyParam,EmptyParam);
MyTable.Cell(1,1).Height := 20;
MyTable.Cell(1,1).Range.Text := '设备名称: '+'光端机';
MyTable.Cell(1,2).Range.Text := '设备编号: '+'GDJ031102';
MyTable.Cell(1,3).Range.Text := '设备类型: '+'西门子R78';
MyTable.Cell(1,4).Range.Text := '所属站点: '+'XX供电局';
//下移2行添加新的表格,因为上下的列数不一样
WordApp.Selection.MoveDown(oleUnit,oleCount,EmptyParam);
MyTable := WordApp.Selection.Tables.Add(WordApp.Selection.Range,7,1,EmptyParam,EmptyParam);
//新插入的表格和上面一个表格是连在一起的,所以word认为是一个表格
MyTable.Cell(2,1).Height := 20;
MyTable.Cell(3,1).Height := 20;
MyTable.Cell(2,1).Range.Text := '故障时间从: '+FormatDateTime('yyyy年mm月dd日',dtmFaultFrom.Date)
+' '+FormatDateTime('hh时mm分ss秒',dtmFaultFromTime.Time)+' 至 '
+FormatDateTime('yyyy年mm月dd日',dtmFaultTo.Date)+' '+FormatDateTime('hh时mm分ss秒',dtmFaultToTime.Time);
MyTable.Cell(3,1).Range.Text := '检修时间从: '+FormatDateTime('yyyy年mm月dd日',dtmCheckFrom.Date)
+' '+FormatDateTime('hh时mm分ss秒',dtmCheckFromTime.Time)+' 至 '
+FormatDateTime('yyyy年mm月dd日',dtmCheckTo.Date)+' '+FormatDateTime('hh时mm分ss秒',dtmCheckToTime.Time);
MyTable.Cell(4,1).Range.Text := '故障现象:'#13+'设备不能正常工作';
MyTable.Cell(5,1).Range.Text := '故障原因:'#13+'尚未检修出来';
MyTable.Cell(6,1).Range.Text := '检修情况:'#13+'努力中';
MyTable.Cell(7,1).Height := 20;
MyTable.Cell(8,1).Height := 20;
MyTable.Cell(7,1).Range.Text := '检修班组:'#9+'有线班';
MyTable.Cell(8,1).Range.Text := '检修人员:'#9+'小张,小王等人';
end;
dtmFaultFrom是一个DateTimePicker组件的名字,其他以dtm开头的组件都是var
myTable: Table;
oleCount,oleUnit: Olevariant;
begin
//以下打开word新的文档
try
WordApp.Connect;
WordApp.Caption := '打印故障记录';
WordApp.Visible := true;
except
MessageBox(Self.Handle,'您可能没有正确安装MicroSoft Word 2000!','提示',0);
end;
WordDoc.ConnectTo(WordApp.Documents.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam));
WordDoc.Activate; //设置页面大小和左右间距
//600磅=21cm,850磅=29.7cm,是标准A4纸张大小,每磅0.035cm,
WordDoc.PageSetup.PageWidth := 600;
WordDoc.PageSetup.PageHeight := 850;
WordDOc.PageSetup.LeftMargin := 50;
WordDoc.PageSetup.RightMargin := 50; //设置标题内容和字体
WordApp.Selection.Font.Size := 15;
WordApp.Selection.Font.Name := '黑体';
WordApp.Selection.Font.Color := clBlack;
WordApp.Selection.Font.Bold := 1;
WordApp.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
WordApp.Selection.TypeText('故障记录'); //设置正文内容和字体
WordApp.Selection.Font.Size := 11;
WordApp.Selection.Font.Name := '宋体';
WordApp.Selection.Font.Color := clBlack;
WordApp.Selection.Font.Bold := 0;
WordApp.Selection.TypeParagraph;
WordApp.Selection.TypeParagraph;
WordApp.Selection.ParagraphFormat.Alignment := wdAlignParagraphJustify; //以下插入表格
oleUnit := wdLine;
oleCount := 2;
MyTable := WordApp.Selection.Tables.Add(WordApp.Selection.Range,1,4,EmptyParam,EmptyParam);
MyTable.Cell(1,1).Height := 20;
MyTable.Cell(1,1).Range.Text := '设备名称: '+'光端机';
MyTable.Cell(1,2).Range.Text := '设备编号: '+'GDJ031102';
MyTable.Cell(1,3).Range.Text := '设备类型: '+'西门子R78';
MyTable.Cell(1,4).Range.Text := '所属站点: '+'XX供电局';
//下移2行添加新的表格,因为上下的列数不一样
WordApp.Selection.MoveDown(oleUnit,oleCount,EmptyParam);
MyTable := WordApp.Selection.Tables.Add(WordApp.Selection.Range,7,1,EmptyParam,EmptyParam);
//新插入的表格和上面一个表格是连在一起的,所以word认为是一个表格
MyTable.Cell(2,1).Height := 20;
MyTable.Cell(3,1).Height := 20;
MyTable.Cell(2,1).Range.Text := '故障时间从: '+FormatDateTime('yyyy年mm月dd日',dtmFaultFrom.Date)
+' '+FormatDateTime('hh时mm分ss秒',dtmFaultFromTime.Time)+' 至 '
+FormatDateTime('yyyy年mm月dd日',dtmFaultTo.Date)+' '+FormatDateTime('hh时mm分ss秒',dtmFaultToTime.Time);
MyTable.Cell(3,1).Range.Text := '检修时间从: '+FormatDateTime('yyyy年mm月dd日',dtmCheckFrom.Date)
+' '+FormatDateTime('hh时mm分ss秒',dtmCheckFromTime.Time)+' 至 '
+FormatDateTime('yyyy年mm月dd日',dtmCheckTo.Date)+' '+FormatDateTime('hh时mm分ss秒',dtmCheckToTime.Time);
MyTable.Cell(4,1).Range.Text := '故障现象:'#13+'设备不能正常工作';
MyTable.Cell(5,1).Range.Text := '故障原因:'#13+'尚未检修出来';
MyTable.Cell(6,1).Range.Text := '检修情况:'#13+'努力中';
MyTable.Cell(7,1).Height := 20;
MyTable.Cell(8,1).Height := 20;
MyTable.Cell(7,1).Range.Text := '检修班组:'#9+'有线班';
MyTable.Cell(8,1).Range.Text := '检修人员:'#9+'小张,小王等人';
end;
解决方案 »
- DBGridEh怎样关闭排序功能
- 多级动态右键菜单事件问题...[在线等啊]
- 如何设置一个运行程序窗口的大小
- ?????????? 关于 ListView 奇怪的问题 ?????????????
- 数据集能不能在Sql语句中当表名使用?
- 求援!!!我使用DBgrideh连了一个数据表,并显示,我想双击表中的一条记录,并使该记录的背景变成另一个指定颜色!怎么做?在线等!!!
- ★用Canvas.LineTo画的线为什么在被其它窗体遮挡后就看不见了?
- 请问最好的类似于OUTLOOK左边工具栏的控件是什么?
- 用WebBrowser提交表单,这样的文本框怎么赋值?
- 关于数据库打包的问题
- 关于form.close..
- 一个小小小小问题?
自己录制一下宏看看就知道拉
清楚直接