Delphi 生成excel中饼图的实例代码

所属分类: 软件编程 / Delphi 阅读数: 150
收藏 0 赞 0 分享
复制代码 代码如下:

var
  i,j,m,n,count1:integer;
  str:string;
  Jdate:tdatetime;
  channellist,potBstrtime,potEstrtime,Jchannelname:string;
  Rres:boolean;
  JSHR:double;
  Excelid,achart1,cell1,cell2,cell3,cell4,cellMiddle,Range1,Range2,series1:variant;
  ExRowsValue,ExRowsName,ExClos:Integer;
  ImagePage,dataPage:integer;
  leftPostion,topPostion,widthPostion,heightPostion:integer;
begin
  //创建OLE对象
  try
    Excelid:=CreateOleObject( 'Excel.Application' );
  except
    on Exception do raise exception.Create('无法创建Xls文件,请确认是否安装EXCEL')
  end;
  Excelid.Visible := false; //Excel显示设置
  Excelid.WorkBooks.Add;  //生成新的excel文件
  //不用excel以前的页
  Excelid.worksheets.add;
  dataPage:=Excelid.Worksheets.count;   //用作数据页
  Excelid.worksheets.add;
  ImagePage:=Excelid.Worksheets.count;
  //showmessage(inttostr(dataPage));
  //showmessage(inttostr(ImagePage)+'sdfg');
  Jdate:=DateTimePicker1.Date;
  count1:=0;
  ExRowsValue:=1; //存储值
  ExRowsName:=2;  //存储名称
  //取得频率编号
  for i:=0 to CheckListBoxFQ.Items.Count-1 do
  begin
    if CheckListBoxFQ.Checked[i] then
    begin
     channellist:= QCaky.FChannelList[i]+','+channellist;
    end;
  end;
  delete(channellist,length(channellist),1);
  leftPostion:=20;
  topPostion:=50;
  widthPostion:=450;
  heightPostion:=230;
  for i:=0 to CheckListBoxPOTT.Items.Count-1 do
  begin
    if CheckListBoxPOTT.Checked[i] then
    begin
      //动态生成excel页数
      //Excelid.worksheets.add;
      //sheetCount:=sheetCount+1;
      //Excelid.worksheets[sheetCount].name:='图表页'+inttostr(sheetCount);
      //在这里生成图表
      if CheckListBoxPOTT.Items[i]='全天' then
      begin
        potBstrtime:='';
        potEstrtime:='';
      end
      else
      begin
        potBstrtime:=QCaky.FPOTbeginList[i];
        potEstrtime:=QCaky.FPOTendList[i];
      end;
      Rres:=QCaky.getFQShr(Jdate,FormMain.QAreacode,channellist,potBstrtime,potEstrtime);  //取得占有率
      ExClos:=1;
      while not QCaky.FADOQueryFQ.Eof do
      begin
        Excelid.worksheets[dataPage].Activate;
        Jchannelname:=QCaky.FADOQueryFQ.fieldbyname('channelname').AsString;
        JSHR:=QCaky.FADOQueryFQ.fieldbyname('allshr').AsFloat;
        Excelid.worksheets[dataPage].Cells[ExRowsName,ExClos].Value :=Jchannelname;
        Excelid.worksheets[dataPage].Cells[ExRowsValue,ExClos].Value :=JSHR;
        inc(ExClos);
        QCaky.FADOQueryFQ.Next;
      end;
       {添加一个二维图(20,50,450,230)表示(左,顶,宽,高)}
      //achart1 := Excelid.worksheets[ImagePage].chartobjects.add(20,50,450,230);
      achart1 := Excelid.worksheets[ImagePage].chartobjects.add(leftPostion,topPostion,widthPostion,heightPostion);
      achart1.chart.charttype := xl3DPie ;  //生成何种类型的图********在此生成的是饼图
      //设置x轴、y轴数据
      cell1 :=  Excelid.worksheets[dataPage].Cells[ExRowsName,1];
      cell2 :=  Excelid.worksheets[dataPage].Cells[ExRowsName,ExClos];
      cell3 :=  Excelid.worksheets[dataPage].Cells[ExRowsValue,1];
      cell4 :=  Excelid.worksheets[dataPage].Cells[ExRowsValue,ExClos];
      Range1 := Excelid.worksheets[dataPage].Range[cell1, cell2]; //设定Chart类别坐标轴(x轴)的取值区域
      Range2 := Excelid.worksheets[dataPage].Range[cell3, cell4]; //设定Chart数值坐标轴(y轴)的取值区域
      series1:= achart1.chart.seriescollection ;
      //series1.add (Range2,xlColumns,false);  //设置y轴的数据
      series1.add (Range2,true);          //可以使用,能够生成饼图
      achart1.chart.seriescollection[1].name:='nihao';
      //设置饼图的形式
      achart1.chart.seriescollection[1].ApplyDataLabels(xlDataLabelsShowLabelAndPercent,true,true,true);
      achart1.Chart.Axes[xlCategory].CategoryNames := Range1;  //设置x轴数据
      //series1.add (Range2,xlColumns,false);  //设置y轴的数据
      //achart1.Chart.Axes[xlCategory].CategoryNames := Range1;  //设置x轴数据
      //删除右侧的标题
      achart1.Chart.Legend.delete;
      //添加图表标题
      achart1.Chart.HasTitle:=True;
      achart1.Chart.ChartTitle.Characters.Text:='收视率走势图';
      achart1.Chart.HasLegend:=true;
      achart1.Chart.Legend.Position := xlBottom;
      //图表左标说明
      //achart1.Chart.Axes(xlValue,xlPrimary).HasTitle := True;
      //achart1.Chart.Axes(xlValue,xlPrimary).AxisTitle.Characters.Text := '收视率(%)';
      //图表下标说明
      //achart1.Chart.Axes(xlCategory,xlPrimary).HasTitle:=True;
      //achart1.Chart.Axes(xlCategory,xlPrimary).AxisTitle.Characters.Text:='时间';
      ExRowsName:=ExRowsName+2;
      ExRowsValue:=ExRowsValue+2;
    end;  //如果时段设置被选中
    leftPostion:=20;
    topPostion:=topPostion+600;
    //showmessage(inttostr(dataPage));
  end;
  Excelid.Visible := true;
end;
更多精彩内容其他人还在看

Delphi 常见文件处理及注册表文件的使用实例

这篇文章主要介绍了Delphi 常见文件处理及注册表文件的使用实例的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi实现获取进程列表及相关信息的实例

这篇文章主要介绍了Delphi实现获取进程列表及相关信息的实例,希望通过本文大家能实现这样的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi 实现软件自动升级的功能

这篇文章主要介绍了Delphi 实现软件自动升级的功能的相关资料,希望通过本文能帮助到大家实现这样的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi使用大图标编译程序的方法

这篇文章主要介绍了Delphi使用大图标编译程序的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi中使用ISuperObject解析Json数据的实现代码

这篇文章主要介绍了Delphi中使用ISuperObject解析Json数据的实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi菜单组件TMainMenu使用方法详解

这篇文章主要为大家详细介绍了Delphi菜单组件TMainMenu的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

为什么继续选择DELPHI(即将逝去的Delphi前景在何方)

已经钻DELPHI很深了,当然现在DELPHI是过了最辉煌的时代。但为什么要继续下去,而不转向其它的?这是不是死脑筋
收藏 0 赞 0 分享

Delphi 调用外部程序并阻塞到外部程序中

这篇文章主要介绍了Delphi 调用外部程序并阻塞到外部程序中的相关知识,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

修改 Delphi 10.3.3 IDE 字体和字体大小

Delphi,是Windows平台下著名的快速应用程序开发工具(Rapid Application Development,简称RAD)。这篇文章主要介绍了修改 Delphi 10.3.3 IDE 字体和字体大小,需要的朋友可以参考下
收藏 0 赞 0 分享

解决delphi TAdoQuery组件的close方法导致”列名无效“错误的问题

今天小编就为大家分享一篇解决delphi TAdoQuery组件的close方法导致”列名无效“错误的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多