查看: 54|回复: 0

[教程] Delphi 生成excel中饼图的实例代码

[复制链接]

3

主题

0

回帖

0

积分

热心网友

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2011-5-1
发表于 2013-8-19 16:09:35 | 显示全部楼层 |阅读模式
复制代码 代码如下:

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 then
    begin
     channellist:= QCaky.FChannelList+','+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 then
    begin
      //动态生成excel页数
      //Excelid.worksheets.add;
      //sheetCount:=sheetCount+1;
      //Excelid.worksheets[sheetCount].name:='图表页'+inttostr(sheetCount);
      //在这里生成图表
      if CheckListBoxPOTT.Items='全天' then
      begin
        potBstrtime:='';
        potEstrtime:='';
      end
      else
      begin
        potBstrtime:=QCaky.FPOTbeginList;
        potEstrtime:=QCaky.FPOTendList;
      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;
您可能感兴趣的文章:
  • python开发的小球完全弹性碰撞游戏代码
  • Delphi实现毫秒级别的倒计时实例代码
  • 截取指定符号之间的字符串(随机读取)delphi实例代码
  • C# 调用Delphi dll 实例代码
  • Delphi修改操作注册表权限的代码
  • DELPHI7.0 获取硬盘、CPU、网卡序列号的代码
  • C#定义并实现单链表实例解析
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部