Delphi向Word导出数据

最近客户要求将数据导出到word,查找了一些源代码后做出来了.先将方法共享出来.希望对大家有用.

procedure TFrmWeekAnalysisQry.BtnExportToExcelClick(Sender: TObject);
var wordApp,WordDoc,WrdSelection:variant;
  strAdd:string;
  i,j,iRangeEnd,iStart,iEnd : integer;
  wdPar,wdRange:OleVariant;
  oShape, oChart,myCol: OleVariant;
  SumJHTonCount, SumWCTonCount, PJTS, PJZCSJ, YSSR, DuoJing: Currency;
  SumJHCarCount, SumWCCarCount: Integer;
  SumDJ,SumFDJ: Currency;//多经 非多经汇总
begin
  wordApp := CreateOleObject('Word.Application');
  wordApp.Visible := true;
  wordDoc:=WordApp.Documents.Add();
  wordDoc.select;
  wrdSelection := WordApp.selection;
  strAdd:='%s年第%d周战略装车点生产经营分析';
  strAdd:= Format(strAdd, [FormatDateTime('YYYY', Date), WeekofYear(Date)]);
  wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
  wrdSelection.Font.bold := true;
  wrdSelection.Font.Size := 15;
  //wrdSelection.Font.UnderLine := 1;  下划线
  wrdSelection.TypeText(strAdd);
  wordApp.selection.TypeParagraph;//换行
  wrdSelection.Font.bold := false;
  wrdSelection.Font.Size := 10;
  wrdSelection.Font.bold := false;
  wrdSelection.TypeText(FormatDateTime('MM月DD日', DateBegin.Date)+'-'+FormatDateTime('MM月DD日', DateEnd.Date));
  wordApp.selection.TypeParagraph;//换行
  wrdSelection.TypeText('西安铁路局货运处');
  wordApp.selection.TypeParagraph;//换行
  wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphLeft;
  wrdSelection.Font.bold := true;
  wrdSelection.TypeText('1、战略装车点主要运营指标完成情况:');
  //从新设置字体
  wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphLeft;
  wrdSelection.Font.bold := false;
  wrdSelection.Font.Size := 10;
  wrdSelection.Font.UnderLine := 0;
  wdPar:=WordApp.ActiveDocument.Paragraphs.Add;
  wdRange:=wdPar.Range;
  wdRange := wordApp.ActiveDocument.Content;
  wdRange.Collapse(wdCollapseEnd);
  wordDoc.Tables.Add(wdRange,cdsMain.RecordCount+ 2,11);
  //合并单元格不成功
  {iStart:=WordDoc.Tables.Item(1).Cell(1,1).Range.Start;
  //myCol:= WordDoc.Tables.Item(1).Cell(1,2);
  //myCol:= WordDoc.Tables.Item(1).Columns.Item(2);
  //iEnd:=myCol.Cells.Item(myCol.Cells.Count).Range.End;
  //iEnd:= WordDoc.Tables.Item(1).Cell(2,1).Range.Start;
  iEnd:= iStart+ 1;
  wdRange:=WordDoc.Range;
  wdRange.Start:=iStart;
  wdRange.End :=iEnd;
  wdRange.Cells.Merge;  }
  //插入数据
  wordDoc.Tables.Item(1).Cell(1,1).Range.Text:= '序号';
  wordDoc.Tables.Item(1).Cell(1,2).Range.Text:= '车站';
  wordDoc.Tables.Item(1).Cell(1,3).Range.Text:= '品类';
  wordDoc.Tables.Item(1).Cell(1,4).Range.Text:= '周计划车数';
  wordDoc.Tables.Item(1).Cell(1,5).Range.Text:= '周计划万吨';
  wordDoc.Tables.Item(1).Cell(1,6).Range.Text:= '周完成车数';
  wordDoc.Tables.Item(1).Cell(1,7).Range.Text:= '周完成万吨';
  wordDoc.Tables.Item(1).Cell(1,8).Range.Text:= '平均停时(小时)';
  wordDoc.Tables.Item(1).Cell(1,9).Range.Text:= '平均装车时间(小时)';
  wordDoc.Tables.Item(1).Cell(1,10).Range.Text:= '运输收入(万元)';
  wordDoc.Tables.Item(1).Cell(1,11).Range.Text:= '多经毛利测算(万元)';
  cdsMain.First;
  SumJHTonCount:= 0;
  SumWCTonCount:= 0;
  PJTS:= 0;
  PJZCSJ:= 0;
  YSSR:= 0;
  SumJHCarCount:= 0;
  SumWCCarCount:= 0;
  DuoJing:= 0;
  for i:= 2 to cdsMain.RecordCount+ 1 do
  begin
    wordDoc.Tables.Item(1).Cell(i,1).Range.Text:= cdsMain.FieldByName('ROWNUM').AsString;
    wordDoc.Tables.Item(1).Cell(i,2).Range.Text:= cdsMain.FieldByName('UPNAME').AsString;
    wordDoc.Tables.Item(1).Cell(i,3).Range.Text:= cdsMain.FieldByName('PLNAME').AsString;
    wordDoc.Tables.Item(1).Cell(i,4).Range.Text:= cdsMain.FieldByName('PLANCARCOUNT').AsString;
    wordDoc.Tables.Item(1).Cell(i,5).Range.Text:= cdsMain.FieldByName('PLANTONCOUNT').AsString;
    wordDoc.Tables.Item(1).Cell(i,6).Range.Text:= cdsMain.FieldByName('CARCOUNT').AsString;
    wordDoc.Tables.Item(1).Cell(i,7).Range.Text:= cdsMain.FieldByName('TONCOUNT').AsString;
    wordDoc.Tables.Item(1).Cell(i,8).Range.Text:= cdsMain.FieldByName('AVGSTOP').AsString;
    wordDoc.Tables.Item(1).Cell(i,9).Range.Text:= cdsMain.FieldByName('AVGLOAD').AsString;
    wordDoc.Tables.Item(1).Cell(i,10).Range.Text:= cdsMain.FieldByName('TRANSINCOME').AsString;
    wordDoc.Tables.Item(1).Cell(i,11).Range.Text:= cdsMain.FieldByName('DUOJING').AsString;
    SumJHTonCount:= SumJHTonCount+ cdsMain.FieldByName('PLANTONCOUNT').AsCurrency;
    SumWCTonCount:= SumWCTonCount+ cdsMain.FieldByName('TONCOUNT').AsCurrency;
    PJTS:= PJTS+ cdsMain.FieldByName('AVGSTOP').AsCurrency;
    PJZCSJ:= PJZCSJ+ cdsMain.FieldByName('AVGLOAD').AsCurrency;
    YSSR:= YSSR+ cdsMain.FieldByName('TRANSINCOME').AsCurrency;
    SumJHCarCount:= SumJHCarCount+ cdsMain.FieldByName('PLANCARCOUNT').AsInteger;
    SumWCCarCount:= SumWCCarCount+ cdsMain.FieldByName('CARCOUNT').AsInteger;
    DuoJing:= DuoJing+ cdsMain.FieldByName('DUOJING').AsCurrency;
    cdsMain.Next;
  end;
  Inc(i);
  wordDoc.Tables.Item(1).Cell(i,2).Range.Text:= '合计';
  wordDoc.Tables.Item(1).Cell(i,3).Range.Text:= '';
  wordDoc.Tables.Item(1).Cell(i,4).Range.Text:= IntToStr(SumJHCarCount);
  wordDoc.Tables.Item(1).Cell(i,5).Range.Text:= CurrToStr(SumJHTonCount);
  wordDoc.Tables.Item(1).Cell(i,6).Range.Text:= IntToStr(SumWCCarCount);
  wordDoc.Tables.Item(1).Cell(i,7).Range.Text:= CurrToStr(SumWCTonCount);
  if cdsMain.RecordCount> 0 then
  begin
    wordDoc.Tables.Item(1).Cell(i,8).Range.Text:= CurrToStr(PJTS/ cdsMain.RecordCount);
    wordDoc.Tables.Item(1).Cell(i,9).Range.Text:= CurrToStr(PJZCSJ/ cdsMain.RecordCount);
  end;
  wordDoc.Tables.Item(1).Cell(i,10).Range.Text:= CurrToStr(YSSR);
  wordDoc.Tables.Item(1).Cell(i,10).Range.Text:= CurrToStr(DuoJing);

  wordApp.Selection.EndKey(wdStory,EmptyParam);
  wdPar := wordApp.ActiveDocument.Paragraphs.Add;
  wdRange := wordApp.activeDocument.Content;
  wdRange.Collapse(wdCollapseEnd);
  if SumJHTonCount<= 0 then SumJHTonCount:= SumWCTonCount;
  //if EdtQJHF.Value<= 0 then EdtQJHF.Value:= SumWCTonCount;
  //if SumJHCarCount<= 0 then SumJHTonCount:= SumWCCarCount;
  wrdSelection.TypeText('(1)全局28个战略装车点共装车'+ IntToStr(SumWCCarCount)+'辆,完成计划的'+ GetPercent(SumWCCarCount , SumJHCarCount)+';发送货物'
  +CurrToStr(SumWCTonCount)+'万吨,完成计划的'+GetPercent(SumWCTonCount , SumJHTonCount)+',占全局货发总量的'
  +GetPercent(SumWCTonCount , EdtQJHF.Value)+ '。');
  wordApp.selection.TypeParagraph;//换行
  wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
  wrdSelection.TypeText('图-6  战略装车点装车数完成情况');


  wordApp.Selection.EndKey(wdStory,EmptyParam);
  wdPar := wordApp.ActiveDocument.Paragraphs.Add;
  wdRange := wordApp.activeDocument.Content;
  wdRange.Collapse(wdCollapseEnd);

  oShape:= wordApp.Selection.InlineShapes.AddOleObject(ClassType:='MSGraph.Chart.8',FileName:='',LinkToFile :=False,DisplayAsIcon :=False);
  oChart:= oShape.OleFormat.Object;
  oChart.Application.DataSheet.Cells.Clear;
  oChart.ChartArea.Font.Size:= 8;
  oChart.Application.Update;
  oChart.ChartType:= 51;// 1是面积图  4是线形图  5是饼图  51是柱状图
  //添加行标题
  oChart.Application.DataSheet.Cells[2,1].Value:= '计划';
  oChart.Application.DataSheet.Cells[3,1].Value:= '实际完成';
  //添加列标题
  oChart.Application.DataSheet.Cells[1,2].Value:= '上周';
  oChart.Application.DataSheet.Cells[1,3].Value:= '本周';

  oChart.Application.DataSheet.Cells[2, 2].Value:= LastPlanCarCount;
  oChart.Application.DataSheet.Cells[2, 3].Value:= SumJHCarCount;
  oChart.Application.DataSheet.Cells[3, 2].Value:= LastImplCarCount;
  oChart.Application.DataSheet.Cells[3, 3].Value:= SumWCCarCount;
  oChart.Application.Update;
  oChart.Application.Quit;

  wordApp.Selection.EndKey(wdStory,EmptyParam);
  wdPar := wordApp.ActiveDocument.Paragraphs.Add;
  wdRange := wordApp.activeDocument.Content;
  wdRange.Collapse(wdCollapseEnd);
  wrdSelection.TypeText('图-7  战略装车点货物发送量完成情况');
  wordApp.Selection.EndKey(wdStory,EmptyParam);
  wdPar := wordApp.ActiveDocument.Paragraphs.Add;
  wdRange := wordApp.activeDocument.Content;
  wdRange.Collapse(wdCollapseEnd);

  oShape:= wordApp.Selection.InlineShapes.AddOleObject(ClassType:='MSGraph.Chart.8',FileName:='',LinkToFile :=False,DisplayAsIcon :=False);
  oChart:= oShape.OleFormat.Object;
  oChart.Application.DataSheet.Cells.Clear;
  oChart.ChartArea.Font.Size:= 8;
  oChart.Application.Update;
  oChart.ChartType:= 51;// 1是面积图  4是线形图  5是饼图  51是柱状图
  //添加行标题
  oChart.Application.DataSheet.Cells[2,1].Value:= '计划';
  oChart.Application.DataSheet.Cells[3,1].Value:= '实际完成';
  //添加列标题
  oChart.Application.DataSheet.Cells[1,2].Value:= '上周';
  oChart.Application.DataSheet.Cells[1,3].Value:= '本周';

  oChart.Application.DataSheet.Cells[2, 2].Value:= LastPlanTonCount;
  oChart.Application.DataSheet.Cells[2, 3].Value:= SumJHTonCount;
  oChart.Application.DataSheet.Cells[3, 2].Value:= LastImplTonCount;
  oChart.Application.DataSheet.Cells[3, 3].Value:= SumWCTonCount;
  oChart.Application.Update;
  oChart.Application.Quit;

  wordApp.Selection.EndKey(wdStory,EmptyParam);
  wdPar := wordApp.ActiveDocument.Paragraphs.Add;
  wdRange := wordApp.activeDocument.Content;
  wdRange.Collapse(wdCollapseEnd);
  wrdSelection.TypeText('图-8  战略装车量占货物发送量情况');
  wordApp.Selection.EndKey(wdStory,EmptyParam);
  wdPar := wordApp.ActiveDocument.Paragraphs.Add;
  wdRange := wordApp.activeDocument.Content;
  wdRange.Collapse(wdCollapseEnd);

  oShape:= wordApp.Selection.InlineShapes.AddOleObject(ClassType:='MSGraph.Chart.8',FileName:='',LinkToFile :=False,DisplayAsIcon :=False);
  oChart:= oShape.OleFormat.Object;
  oChart.Application.DataSheet.Cells.Clear;
  oChart.ChartArea.Font.Size:= 8;
  oChart.Application.Update;
  oChart.ChartType:= 5;// 1是面积图  4是线形图  5是饼图  51是柱状图
  //添加行标题
  oChart.Application.DataSheet.Cells[2,1].Value:= '发送万吨';
  //添加列标题
  oChart.Application.DataSheet.Cells[1,2].Value:= '战略装车点';
  oChart.Application.DataSheet.Cells[1,3].Value:= '其他装车';
  oChart.Application.DataSheet.Cells[2, 2].Value:= SumWCTonCount;
  oChart.Application.DataSheet.Cells[2, 3].Value:= EdtQJHF.Value;
  oChart.Application.Update;
  oChart.Application.Quit;

  wordApp.Selection.EndKey(wdStory,EmptyParam);
  wdPar := wordApp.ActiveDocument.Paragraphs.Add;
  wdRange := wordApp.activeDocument.Content;
  wdRange.Collapse(wdCollapseEnd);
  wrdSelection.TypeText('图-9  战略装车量分品类完成情况');
  wordApp.Selection.EndKey(wdStory,EmptyParam);
  wdPar := wordApp.ActiveDocument.Paragraphs.Add;
  wdRange := wordApp.activeDocument.Content;
  wdRange.Collapse(wdCollapseEnd);

  oShape:= wordApp.Selection.InlineShapes.AddOleObject(ClassType:='MSGraph.Chart.8',FileName:='',LinkToFile :=False,DisplayAsIcon :=False);
  oChart:= oShape.OleFormat.Object;
  oChart.Application.DataSheet.Cells.Clear;
  oChart.ChartArea.Font.Size:= 8;
  oChart.Application.Update;
  oChart.ChartType:= 5;// 1是面积图  4是线形图  5是饼图  51是柱状图
  //添加行标题
  oChart.Application.DataSheet.Cells[2,1].Value:= '发送万吨';
  //添加列标题
  cdsGroupByPL.First;
  for i:= 2 to cdsGroupByPL.RecordCount+ 1 do
  begin
    oChart.Application.DataSheet.Cells[1,i].Value:= Trim(cdsGroupByPL.FieldByName('PLNAME').AsString);
    oChart.Application.DataSheet.Cells[2, i].Value:= cdsGroupByPL.FieldByName('TONCOUNT').AsCurrency;
    cdsGroupByPL.Next;
  end;
  oChart.Application.Update;
  oChart.Application.Quit;

  wordApp.Selection.EndKey(wdStory,EmptyParam);
  wdPar := wordApp.ActiveDocument.Paragraphs.Add;
  wdRange := wordApp.activeDocument.Content;
  wdRange.Collapse(wdCollapseEnd);
  wrdSelection.TypeText('  (2)货车停留时间平均为'+ GetDiv(PJTS,cdsMain.RecordCount)+'小时,较考核目标超'+GetOverloadTime(PJTS,cdsMain.RecordCount,5)+'小时,'
                       +'较全局平均停时减少2.8小时;平均装车作业时间'+ GetDiv(PJZCSJ, cdsMain.RecordCount)+'小时,较考核目标超'+GetOverloadTime(PJZCSJ,cdsMain.RecordCount,2)+'小时。');
  wordApp.selection.TypeParagraph;//换行
  wrdSelection.TypeText('  (3)路局多经参与经营的12个战略装车点,本周总计完成'+ IntToStr(SumWCCarCount)
                       +'车,比上周'+ IntToStr(Trunc(LastImplCarCount))+'车多装'+ IntToStr(Trunc(SumWCCarCount- LastImplCarCount))+'车,增加了'
                       +GetPercent(SumJHCarCount- LastImplCarCount, LastImplCarCount)+',日均装车'+GetDiv(SumWCTonCount, Trunc(DateEnd.Date-DateBegin.Date+ 1))
                       +'车。占全局战略装车点发送总量的'+ GetPercent(SumWCCarCount, EdtQJHF.Value)+'。');
  wordApp.selection.TypeParagraph;//换行
  wrdSelection.TypeText('  3、毛利分析:本周实现毛利'+ CurrToStr(DuoJing)+'万元,和上周基本持平。');
  SumDJ:= 0;
  SumFDJ:= 0;
  cdsMain.First;
  with cdsMain do
  begin
    DisableControls;
    while not Eof do
    begin
      if FieldByName('DJCY').AsInteger= 0 then
        SumDJ:= SumDJ+ FieldByName('TONCOUNT').AsCurrency
      else
        SumFDJ:= SumFDJ+ FieldByName('TONCOUNT').AsCurrency;
      cdsMain.Next;
    end;
    EnableControls;
  end;
  wordApp.selection.TypeParagraph;//换行
  wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
  wrdSelection.TypeText('图-10  多经参与战略装车情况');
  wordApp.Selection.EndKey(wdStory,EmptyParam);
  wdPar := wordApp.ActiveDocument.Paragraphs.Add;
  wdRange := wordApp.activeDocument.Content;
  wdRange.Collapse(wdCollapseEnd);

  oShape:= wordApp.Selection.InlineShapes.AddOleObject(ClassType:='MSGraph.Chart.8',FileName:='',LinkToFile :=False,DisplayAsIcon :=False);
  oChart:= oShape.OleFormat.Object;
  oChart.Application.DataSheet.Cells.Clear;
  oChart.ChartArea.Font.Size:= 8;
  oChart.Application.Update;
  oChart.ChartType:= 5;// 1是面积图  4是线形图  5是饼图  51是柱状图
  //添加行标题
  oChart.Application.DataSheet.Cells[2,1].Value:= '发送吨';
  //添加列标题
  oChart.Application.DataSheet.Cells[1,2].Value:= '多经参与经营发送吨';
  oChart.Application.DataSheet.Cells[1,3].Value:= '其他战略装车点发送吨';
  oChart.Application.DataSheet.Cells[2, 2].Value:= SumDJ;
  oChart.Application.DataSheet.Cells[2, 3].Value:= SumFDJ;
  oChart.Application.Update;
  oChart.Application.Quit;

  wordApp.selection.TypeParagraph;//换行
  wrdSelection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
  wrdSelection.TypeText('图-11  多经参与战略装车点经营收入(万元)');
  wordApp.Selection.EndKey(wdStory,EmptyParam);
  wdPar := wordApp.ActiveDocument.Paragraphs.Add;
  wdRange := wordApp.activeDocument.Content;
  wdRange.Collapse(wdCollapseEnd);

  oShape:= wordApp.Selection.InlineShapes.AddOleObject(ClassType:='MSGraph.Chart.8',FileName:='',LinkToFile :=False,DisplayAsIcon :=False);
  oChart:= oShape.OleFormat.Object;
  oChart.Application.DataSheet.Cells.Clear;
  oChart.ChartArea.Font.Size:= 8;
  oChart.Application.Update;
  oChart.ChartType:= 51;// 1是面积图  4是线形图  5是饼图  51是柱状图
  //添加行标题
  oChart.Application.DataSheet.Cells[2,1].Value:= '收入(万元)';
  //添加列标题
  oChart.Application.DataSheet.Cells[1,2].Value:= '本周';
  oChart.Application.DataSheet.Cells[1,3].Value:= '上周';
  oChart.Application.DataSheet.Cells[2, 2].Value:= 319.14;
  oChart.Application.DataSheet.Cells[2, 3].Value:= 314.96;
  oChart.Application.Update;
  oChart.Application.Quit;

  wordApp.Selection.EndKey(wdStory,EmptyParam);
  wdPar := wordApp.ActiveDocument.Paragraphs.Add;
  wdRange := wordApp.activeDocument.Content;
  wdRange.Collapse(wdCollapseEnd);
  wrdSelection.TypeText('  (4)装车分析:与上周一样,全局仅有黄陵、安口南两个战略装车点到达日均装运一列的要求,'
                       +'占全局战略装车点总数的7%。周装车少于100车的有梅家坪、白水江、燕子砭、咸阳西4个点。');
  wordApp.selection.TypeParagraph;//换行
  wrdSelection.TypeText('2.存在问题及分析:');
  wordApp.selection.TypeParagraph;//换行
  wrdSelection.TypeText('  (1)全局共有14个装车点为完成周装车计划。完成比例在50%以下的有桑树坪、合阳、白水江、燕子砭4个战略装车点,占总数的14%');
  wordApp.selection.TypeParagraph;//换行
  wrdSelection.TypeText('  主要原因有四个方面。一是空出来源紧张,不能满足日均装车需求。装运大宗货源煤炭、石油需要的敞车、罐车缺口较大。'
                       +'如牛家梁在请求车5418车的情况下,仅承认了916车;柞水、官渡、三桥也存在此类原因。本周共产生坏车及过期车2104量,'
                       +'修复使用1745车。三是停限装原因,战略装车点请求车兑现率仅为41%。四是货源依旧不足。铁矿石、焦炭市场不好影响装车。');
  wordApp.selection.TypeParagraph;//换行
  wrdSelection.TypeText('  (2)一是装车速度较慢,影响装车时间,造成平均停时较大,如合阳点专用线装车为漏斗漏煤,装车速度较快,平煤采取刮板刮平后,'
                       +'人工平顶,速度较慢,引起停时较大。二是坏车修复时间长,也是引起停时较大的一个重要原因。');
  wordApp.selection.TypeParagraph;//换行
  wrdSelection.TypeText('3.战略装车点下周重点工作:');
  wordApp.selection.TypeParagraph;//换行
  wrdSelection.TypeText('  (1)加大货源组织工作。广泛宣传战略装车点优势,公布资源受理电话、去向、运输品类和开行时间,在货运计划、配空车'
                       +'等方面予以优惠政策,重点做好新建战略装车点瑶曲、安口南的货源组织工作,开发战略装车点新货源,保证装车上量。');
  wordApp.selection.TypeParagraph;//换行
  wrdSelection.TypeText('  (2)提高承认车兑现率。对组织不力造成承认车落空的进行认真分析,纳入考核并追究管理责任。调度、运输、货运部门要加强协作,'
                       +'落实战略装车点计划、承认、配空、挂运四优先制度。');
  wordApp.selection.TypeParagraph;//换行
  wrdSelection.TypeText('  (3)大力压缩等待时间。与专用线加强协调,增强战略装车点装车、平顶、加固车门人员力量,压缩撞车后的整理等待时间。');
end;

你可能感兴趣的:(Delphi向Word导出数据)