一个简单的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;