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

所属分类: 软件编程 / Delphi 阅读数: 53
收藏 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之Pascal语言中的关键字及保留字汇总

这篇文章主要介绍了Delphi之Pascal语言中的关键字及保留字汇总,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi实现图片滚动切换的完整实例代码

这篇文章主要介绍了Delphi实现图片滚动切换的完整实例代码,通过本实例,读者可以进一步掌握Delphi控件及图形处理的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi实现图像文本旋转特效完整实例代码

这篇文章主要介绍了Delphi实现图像文本旋转特效完整实例代码,对于帮助读者进一步理解Delphi图形及文字特效的处理有一定的借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi实现限定软件使用时间的方法

这篇文章主要介绍了Delphi实现限定软件使用时间的方法,商业软件开发中非常实用的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

在Delphi实现在数据库中存取图像的图文演示无错

最近打算学习Delphi实现在数据库中存取图像,网上的好多Delphi实现在数据库中存取图像都是错误的,所以我把图片给弄好了。
收藏 0 赞 0 分享

delphi建立、读取、存贮INI文件的方法《三》

控制INI文件几法要利用.INI文件做程序有关数据的存储工作,就需要能读和写.INI文件,所以列了如下方法给大家参考:
收藏 0 赞 0 分享

delphi建立、读取、存贮INI文件的方法《二》

内容: 要利用.INI文件做程序有关数据的存储工作,就需要能读和写.INI 文件,所以列了如下方法给大家参考:
收藏 0 赞 0 分享

Delphi建立、读取、存贮INI文件的方法《一》

内容: INI文件在系统配置及应用程序参数保存与设置方面,具有很重要的作用,所以可视化的编程一族,如VB、VC、VFP、Delphi等都提供了读写INI文件的方法,其中Delphi中操作INI文件,最为简洁,这是因为Delphi6.0提供了一个TInifile类,使我们可以非常
收藏 0 赞 0 分享

插件管理框架 for Delphi(一)

插件管理框架 for Delphi许多软件采用“插件”(PlugIns)来扩展其功能,比如PhotoShop所支持的各类滤镜就是插件;我们所熟知的Winamp,也有许多皮肤以及可视效果插件。再有微软的windows media player,以及QQ,冲浪软件Opera……采用插
收藏 0 赞 0 分享

插件管理框架 for Delphi(二)

插件管理框架2.2 实现代码
收藏 0 赞 0 分享
查看更多